Skip to content

OAuth support for MFA #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft

Conversation

shtrom
Copy link

@shtrom shtrom commented Nov 22, 2023

This will fix #2, and is built on top of #1.

This is a breaking change in the API (replacing gettoken with get_info), so likely will require something like a new major version. It's now backward-compatible.

The token can be provided on init, e.g.,

>>> import auroraplus
>>> api = auroraplus.api({'access_token': 'xxx', 'token_type': 'bearer'})
>>> api.get_info()
>>> api.serviceAgreementID
'nnn'
>>> api.getday()
>>> api.day['SummaryTotals']
{'DollarValueUsage': {'Other': 0.0, 'Total': 0.0, 'T140': 0.0, 'T93OFFPEAK': 0.0}, 'KilowattHourUsage': {'Total': 0.0, 'T140': 0.0, 'T93OFFPEAK': 0.0}, 'DayContainsSubstitutedUsage': False, 'HasDailyBillSegments': False}

or fetched semi-interactively (wip).

shtrom added 6 commits August 13, 2023 22:55
Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
This is the new way, needed to support MFA

Signed-off-by: Olivier Mehani <[email protected]>
@shtrom shtrom marked this pull request as draft November 22, 2023 13:34
@shtrom
Copy link
Author

shtrom commented Nov 23, 2023

I've re-added a layer of backward compat: if giving an empty username and just the access token string as the password (e.g., in HomeAssistant config), it may just work without code changes.

I also added and documented a methods to issue a new token. tl;dr:

>>> import auroraplus                                                                                                                                               
>>> api = auroraplus.api()                                                                                                                                          
>>> api.oauth_authorize()                                                                                                                                           
'https://customers.auroraenergy.com.au/auroracustomers1p.onmicrosoft.com/b2c_1a_sign_in//oauth2/v2.0/authorize?response_type=code&client_id=2ff9da64-8629-4a92-a4b6-850a3f02053d&redirect_uri=https%3A%2F%2Fmy.auroraenergy.com.au%2Flogin%2Fredirect&scope=openid+profile+offline_access&state=...&client_info=1'                          

Follow the URL above in a browser to authenticate with username+password and
MFA. This will redirect to an error page (Cradle Mountain). Copy the full URL,
of the error page and continue.

>>> api.oauth_redirect('https://my.auroraenergy.com.au/login/redirect?state=...')                                                                                   
{'id_token': 'ey...', 'access_token': 'ey...', 'token_type': 'bearer', ...}                                                                                         

The api object is now ready to use. The token returned in the last step can be saved for later use when re-initialising the api object without having to follow the OAuth flow to obtain a new authorisation.

@slothking87, wanna give this a go?

@slothking87
Copy link

Yep - I'll give it a go later tonight

@slothking87
Copy link

Hmmmm - Im probably missing a step here somewhere. Visiting that link just gives me a "sorry, but we are having trouble signing you in" message.
On the plus side - the install of the new version seemed to work fine this time - it throws this error which is to be expected I guess

2023-11-24 19:30:20.040 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up auroraplus platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 359, in _async_setup_platform
await asyncio.shield(task)
File "/config/custom_components/auroraplus/sensor.py", line 98, in async_setup_platform
AuroraPlus = await hass.async_add_executor_job(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/auroraplus/sensor.py", line 96, in aurora_init
session.getmonth()
File "/usr/local/lib/python3.11/site-packages/auroraplus/init.py", line 89, in getmonth
self.month = self.request("month", index)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/auroraplus/init.py", line 63, in request
+ self.serviceAgreementID
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'api' object has no attribute 'serviceAgreementID'

@shtrom
Copy link
Author

shtrom commented Nov 24, 2023 via email

@shtrom
Copy link
Author

shtrom commented Nov 24, 2023 via email

@afkiwers
Copy link

afkiwers commented Jan 3, 2024

I just had a play with it and it's working for me. Thanks a lot for fixing!

@slothking87
Copy link

Looks like there is a problem when upgrading HA to 2024.1.2 - I get an error that the minimum version is 2023.6.0

Rolling back now - but just flagging it as something that might need looking at

@shtrom
Copy link
Author

shtrom commented Jan 9, 2024 via email

@slothking87
Copy link

I did now - breaks with an upgrade, but fine after a downgrade

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/loader.py", line 842, in get_platform
cache[full_name] = self._import_platform(platform_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/loader.py", line 859, in _import_platform
return importlib.import_module(f"{self.pkg_path}.{platform_name}")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "", line 1204, in _gcd_import
File "", line 1176, in _find_and_load
File "", line 1147, in _find_and_load_unlocked
File "", line 690, in _load_unlocked
File "", line 940, in exec_module
File "", line 241, in _call_with_frames_removed
File "/config/custom_components/auroraplus/sensor.py", line 39, in
from homeassistant_historical_sensor import (
File "/usr/local/lib/python3.11/site-packages/homeassistant_historical_sensor/init.py", line 39, in
raise SystemError(msg)
SystemError: Running HomeAssistant 2024.1.2, Minimum required version >=2023.6.0

and

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/loader.py", line 842, in get_platform
cache[full_name] = self._import_platform(platform_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/loader.py", line 859, in _import_platform
return importlib.import_module(f"{self.pkg_path}.{platform_name}")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "", line 1204, in _gcd_import
File "", line 1176, in _find_and_load
File "", line 1147, in _find_and_load_unlocked
File "", line 690, in _load_unlocked
File "", line 940, in exec_module
File "", line 241, in _call_with_frames_removed
File "/config/custom_components/auroraplus/sensor.py", line 39, in
from homeassistant_historical_sensor import (
File "/usr/local/lib/python3.11/site-packages/homeassistant_historical_sensor/init.py", line 39, in
raise SystemError(msg)
SystemError: Running HomeAssistant 2024.1.2, Minimum required version >=2023.6.0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/config.py", line 1483, in async_process_component_config
platform = p_integration.get_platform(domain)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/loader.py", line 851, in get_platform
raise ImportError(
ImportError: Exception importing custom_components.auroraplus.sensor

and a kind little note

STATE_CLASS_TOTAL was used from auroraplus, this is a deprecated constant which will be removed in HA Core 2025.1. Use SensorStateClass.TOTAL instead, please create a bug report at https://github.com/LeighCurran/AuroraPlusHA/issues

@shtrom
Copy link
Author

shtrom commented Jan 10, 2024

Hum, I think this error is better followed up on https://github.com/LeighCurran/AuroraPlusHA#9, as it's to do with the HA integration, not the underlying lib.

shtrom referenced this pull request in shtrom/AuroraPlusHA Jan 10, 2024
Fixes

    Traceback (most recent call last):
    File /usr/src/homeassistant/homeassistant/loader.py, line 842, in get_platform
    cache[full_name] = self._import_platform(platform_name)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File /usr/src/homeassistant/homeassistant/loader.py, line 859, in _import_platform
    return importlib.import_module(f{self.pkg_path}.{platform_name})
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File /usr/local/lib/python3.11/importlib/init.py, line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File , line 1204, in _gcd_import
    File , line 1176, in _find_and_load
    File , line 1147, in _find_and_load_unlocked
    File , line 690, in _load_unlocked
    File , line 940, in exec_module
    File , line 241, in _call_with_frames_removed
    File /config/custom_components/auroraplus/sensor.py, line 39, in
    from homeassistant_historical_sensor import (
    File /usr/local/lib/python3.11/site-packages/homeassistant_historical_sensor/init.py, line 39, in
    raise SystemError(msg)
    SystemError: Running HomeAssistant 2024.1.2, Minimum required version >=2023.6.0

See [0], [1]

[0] ldotlopez/ha-historical-sensor#12
[1] https://github.com/LeighCurran/AuroraPlus/pull/3\#issuecomment-1884213483

Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
Actually add the token-getting script...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MFA support: Token request failed: Unauthorized
3 participants