Skip to content

Commit

Permalink
more work on o365 oAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 committed Feb 16, 2024
1 parent 1693bd1 commit b76366a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
6 changes: 3 additions & 3 deletions custom_components/mail_and_packages/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,12 @@ async def async_step_o365(
user_input[CONF_METHOD] = "o365"
user_input.update(CONF_OUTLOOK_DEFAULTS)
self._data.update(user_input)
app = O365Auth(user_input)
app = O365Auth(self.hass, user_input)
self._problem = None
valid = False

try:
app.client()
await app.client()
valid = True
except TokenError:
_LOGGER.error("Problems obtaining oAuth token.")
Expand Down Expand Up @@ -605,7 +605,7 @@ async def async_step_init(self, user_input=None):
if user_input is not None:
self._data.update(user_input)

valid = await _test_login(
valid = await test_login(
user_input[CONF_HOST],
user_input[CONF_PORT],
user_input[CONF_USERNAME],
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mail_and_packages/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"port": 993,
}
CONF_O365_TENANT = "tenant"
CONF_O365_SCOPE = "https://outlook.office365.com/.default"
CONF_O365_SCOPE = ["https://outlook.office365.com/.default"]

# GMail Config
CONF_GMAIL_DEFAULTS = {
Expand Down
28 changes: 21 additions & 7 deletions custom_components/mail_and_packages/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import msal

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from .const import (
CONF_CLIENT_ID,
CONF_SECRET,
CONF_O365_TENANT,
CONF_O365_SCOPE,
CONF_TOKEN,
CONF_GMAIL_SCOPE,
)

_LOGGER = logging.getLogger(__name__)
Expand All @@ -24,8 +24,9 @@ def generate_auth_string(user, token) -> str:
class O365Auth:
"""Class for Mail and Packages Office365 handling."""

def __init__(self, config: ConfigEntry) -> None:
def __init__(self, hass: HomeAssistant, config: ConfigEntry) -> None:
"""Initialize."""
self.hass = hass
self.token = None
self.config = config
self._scope = CONF_O365_SCOPE
Expand All @@ -42,19 +43,32 @@ def _set_authority(self) -> None:
f"https://login.microsoftonline.com/{self.config[CONF_O365_TENANT]}"
)

def client(self) -> None:
async def client(self) -> None:
"""Setup client oauth."""
app = msal.ConfidentialClientApplication(
if not self._authority:
self._authority = (
f"https://login.microsoftonline.com/{self.config[CONF_O365_TENANT]}"
)
_LOGGER.debug("Authority: %s", self._authority)
app = await self.hass.async_add_executor_job(
msal.ConfidentialClientApplication,
self.config[CONF_CLIENT_ID],
self._authority,
self.config[CONF_SECRET],
self._authority,
)

result = app.acquire_token_silent(self._scope, account=None)
result = await self.hass.async_add_executor_job(
app.acquire_token_silent,
self._scope,
None,
)

if not result:
_LOGGER.debug("No token cached, getting new token.")
result = app.acquire_token_for_client(self._scope)
result = await self.hass.async_add_executor_job(
app.acquire_token_for_client,
self._scope,
)

if CONF_TOKEN in result:
self.token = result[CONF_TOKEN]
Expand Down

0 comments on commit b76366a

Please sign in to comment.