Skip to content
This repository was archived by the owner on Nov 23, 2023. It is now read-only.
This repository was archived by the owner on Nov 23, 2023. It is now read-only.

No credentials created for Microsoft services when warning raised Warning: Scope has changed #5

@jaknel

Description

@jaknel

~/.config/oauth2token/microsoft/config.json

{
    "web": {
        "client_id": "someclientid",
        "client_secret":  "someclientsecret",
        "auth_uri": "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
        "token_uri": "https://login.microsoftonline.com/common/oauth2/v2.0/token"
    }
}

~/.config/oauth2token/microsoft/scopes.json

["https://outlook.office365.com/IMAP.AccessAsUser.All", "https://outlook.office365.com/POP.AccessAsUser.All", "https://outlook.office365.com/SMTP.Send", "offline_access"]

Running oauth2create microsoft <account_name> raises the following warning:

Warning: Scope has changed from "https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com`/POP.AccessAsUser.All https://outlook.office365.com/SMTP.Send offline_access" to "https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/POP.AccessAsUser.All https://outlook.office365.com/SMTP.Send".`

Note that the scope rewrite removes offline_access from the list of scopes. This warning interferes with properly configuring the account. If offline_access is omitted from scopes.json then the initial create procedure succeeds, however, refresh_token is not set on the returned credentials leading to: RefreshError: The credentials do not contain the necessary fields need to refresh the access token. You must specify refresh_token, token_uri, client_id, and client_secret. after the initial authorization expires and oauth2get attempts to refresh the token.

The fix I found is mentioned in oauthlib/oauthlib#562 Rewriting the scope is allowed by the RFC so the warning should not be interfering with the create process. The workaround is to run the command while setting an environment variable OAUTHLIB_RELAX_TOKEN_SCOPE.

OAUTHLIB_RELAX_TOKEN_SCOPE=True oauth2create microsoft <account_name>

This leads to an additional problem when oauth2get runs: RefreshError: Not all requested scopes were granted by the authorization server, missing scopes offline_access.

flow.credentials stores the original scopes as flow.oauth2session.scope instead of the rewritten scopes returned by the server available in flow.oauth2session.token['scope']

A change to create_user_credentials in oauth2token/token_mgmt.py fixes this

def create_user_credentials(app=None, user=None, **kwargs):
    flow = InstalledAppFlow.from_client_config(*get_json_config(app=app))

    flow.run_local_server(port=0)

    creds = flow.credentials
    creds._scopes = flow.oauth2session.token["scope"]

    pickle.dump(creds, get_credentials_file(app=app, user=user, override=True))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions