python-o365: JWT is not well formed, there are no dots

I have a few people who use the Home Assistant O365 integration that are getting the errors as shown below (they are encapsulated in HA login, but you can see what is being surfaced by O365). I’ve only seen reports in the last few months, but I have no idea what is causing them. Unfortunately I don’t get the problem myself (or just don’t notice the error in my logs, so I don’t really know what triggers it).

Any ideas as to what is causing the problem? The integration sets up a FileSystemTokenBackend, which it uses I the Account setup call, beyond that it leaves token management to O365 (apart from reading the token once to check granted permissions). Code as below:

def _try_authentication(perms, credentials, main_resource):
    _LOGGER.debug("Setup token")
    token_backend = FileSystemTokenBackend(
        token_path=perms.token_path,
        token_filename=perms.token_filename,
    )
    _LOGGER.debug("Setup account")
    account = Account(
        credentials,
        token_backend=token_backend,
        timezone=CONST_UTC_TIMEZONE,
        main_resource=main_resource,
    )

    try:
        return account, account.is_authenticated

    except json.decoder.JSONDecodeError:
        return account, False
Logger: custom_components.o365.calendar
Quelle: custom_components/o365/calendar.py:655
Integration: Office 365 (Dokumentation, Probleme)
Erstmals aufgetreten: 08:48:54 (1 Vorkommnisse)
Zuletzt protokolliert: 08:48:54


Error getting calendar events - 401 Client Error: Unauthorized for url: https://graph.microsoft.com/v1.0/me/calendars/AQMkADAwATNiZmYAZC04M2ZhLTkwNDYtMDACLTAwCgBGAAADxUpIqt1PQ02qEe37a0cywQcAe9JyutQAp0a3sg19Hw9tFgAAAgEGAAAAe9JyutQAp0a3sg19Hw9tFgAF1RvVAwAAAA==/calendarView?%24top=999&startDateTime=2024-11-08T07%3A48%3A44.399251%2B00%3A00&endDateTime=2024-11-09T07%3A48%3A44.399274%2B00%3A00&%24select=sensitivity%2Cstart%2Ccategories%2Cbody%2CisAllDay%2Csubject%2Cend%2Clocation%2CseriesMasterId%2Cattendees%2CshowAs | Error Message: IDX14100: JWT is not well formed, there are no dots (.). The token needs to be in JWS or JWE Compact Serialization Format. (JWS): 'EncodedHeader.EncodedPayload.EncodedSignature'. (JWE): 'EncodedProtectedHeader.EncodedEncryptedKey.EncodedInitializationVector.EncodedCiphertext.EncodedAuthenticationTag'.
Logger: O365.connection
Quelle: /usr/local/lib/python3.12/site-packages/O365/connection.py:838
Erstmals aufgetreten: 08:48:54 (1 Vorkommnisse)
Zuletzt protokolliert: 08:48:54

Client Error: 401 Client Error: Unauthorized for url: https://graph.microsoft.com/v1.0/me/calendars/AQMkADAwATNiZmYAZC04M2ZhLTkwNDYtMDACLTAwCgBGAAADxUpIqt1PQ02qEe37a0cywQcAe9JyutQAp0a3sg19Hw9tFgAAAgEGAAAAe9JyutQAp0a3sg19Hw9tFgAF1RvVAwAAAA==/calendarView?%24top=999&startDateTime=2024-11-08T07%3A48%3A44.399251%2B00%3A00&endDateTime=2024-11-09T07%3A48%3A44.399274%2B00%3A00&%24select=sensitivity%2Cstart%2Ccategories%2Cbody%2CisAllDay%2Csubject%2Cend%2Clocation%2CseriesMasterId%2Cattendees%2CshowAs | Error Message: IDX14100: JWT is not well formed, there are no dots (.). The token needs to be in JWS or JWE Compact Serialization Format. (JWS): 'EncodedHeader.EncodedPayload.EncodedSignature'. (JWE): 'EncodedProtectedHeader.EncodedEncryptedKey.EncodedInitializationVector.EncodedCiphertext.EncodedAuthenticationTag'. | Error Code: The user has checked their access token. There are no dots inside. They’ve also checked it with jwt.io and get the following error:

Error: Looks like your JWT Header is not encoded correclty using base64url (https://tools.ietf.org/html/rfc4648#section-5). Note that padding ("=") must be omitted as per https://tools.ietf.org/html/rfc7515'section-2 The error does not occur permanently, but only occasionally. It then seems to heal itself, since the synchronization of tasks and calendar generally works.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 194 (188 by maintainers)

Most upvoted comments

The idea behind is that in 2 seconds the other instance has had enough time to update the token. That is why we added the comment about “assuming” the other thread or instance has refreshed the code.

I’d agree. I considered that max_tries would be doing nothing otherwise 😃

But can we move it to another issue? I think it’s not the same. The issue now appears to be race conditions on the token refresh.

I can re-open/open new if I need to

I’ll try and look at documentation tomorrow, if I don’t get any other bugs in tomorrow. I’m getting more interest in my HA stuff, which is generating more work for me.

Happy to, just ping when you have it ready. You can have my email if you prefer.

I’ve tried multiple times the last changes and everything works. Can you please test the latest changes. If everything works fine I an push this changes to master to release.

I’ve run this through my test suite, and it tests fine.

Hiya, I’ll do some checking of what calls I’m making and the parameters passed, and get back to you. Maybe later or tomorrow morning.

Thanks

retreive the state saved in auth_step_one - retreive should be retrieve as it’s asuming there is are no parallel connections running at once - should be assuming

You have written this in the readme It is highly recommended to add the "offline_access" permission. Otherwise the library will only have access to the user resources for 1 hour. See [Permissions and Scopes](#permissions-and-scopes).

I don’t think you can add offline_access, if you do, you will get this error:

  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/O365/connection.py", line 552, in get_authorization_url
    flow = self.msal_client.initiate_auth_code_flow(scopes=scopes, redirect_uri=redirect_uri)
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/msal/application.py", line 942, in initiate_auth_code_flow
    scope=self._decorate_scope(scopes),
          ~~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/msal/application.py", line 691, in _decorate_scope
                raise ValueError(
    ...<2 lines>...
    The reserved list: {}""".format(list(scope_set), list(reserved_scope)))
ValueError: You cannot use any scope value that is reserved.
Your input: ['Calendars.ReadWrite', 'User.Read', 'offline_access']
The reserved list: ['openid', 'profile', 'offline_access']

Soon!

I’ll implement “multi-account” authentication next, as until now 1 account instance can only hold 1 authentication, Now it can hold as many as you want

Done as well.

After the testing I will release asap

I’m away, may be able to test Sunday/Monday.