maestral.oauth

This module is responsible for authorization and token store in the system keyring.

class maestral.oauth.OAuth2Session(config_name, app_key='2jmbq42w7vof78h')[source]

Bases: object

Provides Dropbox OAuth flow and key store interface

OAuth2Session provides OAuth 2 login and token store in the preferred system keyring. To authenticate with Dropbox, run get_auth_url() first and direct the user to visit that URL and retrieve an auth token. Verify the provided auth token with verify_auth_token() and save it in the system keyring together with the corresponding Dropbox ID by calling save_creds(). Supported keyring backends are, in order of preference:

  • MacOS Keychain

  • Any keyring implementing the SecretService Dbus specification

  • KWallet

  • Plain text storage

When the auth flow is completed, a short-lived access token and a long-lived refresh token are generated. They can be accessed through the properties access_token and refresh_token. Only the long-lived refresh token will be saved in the system keychain for future sessions, the Dropbox SDK will use it to generate short-lived access tokens as needed.

If the auth flow was previously completed before Dropbox migrated to short-lived tokens, the token_access_type will be ‘legacy’ and only a long-lived access token will be available.

Note

Once the token has been stored with a keyring backend, that backend will be saved in the config file and remembered until the user unlinks the account. This module will therefore never switch keyring backends while linked.

Warning

Unlike MacOS Keychain, Gnome Keyring and KWallet do not support app-specific access to passwords. If the user unlocks those keyrings, we and any other application in the same user session get access to all saved passwords.

Parameters
  • config_name (str) – Name of maestral config.

  • app_key (str) –

Variables
  • Success (int) – Exit code for successful auth.

  • InvalidToken (int) – Exit code for invalid token.

  • ConnectionFailed (int) – Exit code for connection errors.

Return type

None

Success = 0
InvalidToken = 1
ConnectionFailed = 2
default_token_access_type = 'offline'
property keyring: keyring.backend.KeyringBackend
property linked: bool

Returns True if we have full auth credentials, False otherwise.

property account_id: Optional[str]

Returns the account ID (read only). This call may block until the keyring is unlocked.

property token_access_type: Optional[str]

Returns the type of access token. If ‘legacy’, we have a long-lived access token. If ‘offline’, we have a short-lived access token with an expiry time and a long-lived refresh token to generate new access tokens. This call may block until the keyring is unlocked.

property access_token: Optional[str]

Returns the access token (read only). This will always be set for a ‘legacy’ token. For an ‘offline’ token, this will only be set if we completed the auth flow in the current session. In case of an ‘offline’ token, use the refresh token to retrieve a short-lived access token through the Dropbox API instead. This call may block until the keyring is unlocked.

property refresh_token: Optional[str]

Returns the refresh token (read only). This will only be set for an ‘offline’ token. This call may block until the keyring is unlocked.

property access_token_expiration: Optional[datetime.datetime]

Returns the expiry time for the short-lived access token. This will only be set for an ‘offline’ token and if we completed the flow during the current session.

load_token()[source]

Loads auth token from system keyring. This will be called automatically when accessing any of the properties linked, access_token, refresh_token or token_access_type. This call may block until the keyring is unlocked.

Raises

KeyringAccessError – If the system keyring is locked.

Return type

None

get_auth_url()[source]

Retrieves an auth URL to start the OAuth2 implicit grant flow.

Returns

Dropbox auth URL.

Return type

str

verify_auth_token(code)[source]

If the user approves the app, they will be presented with a single usage “authorization code”. Have the user copy/paste that authorization code into the app and then call this method to exchange it for a long-lived auth token.

Returns

Success, InvalidToken, or ConnectionFailed.

Parameters

code (str) –

Return type

int

save_creds()[source]

Saves the auth token to system keyring. Falls back to plain text storage if the user denies access to keyring. This should be called after verify_auth_token() returned successfully.

Return type

None

delete_creds()[source]

Deletes auth token from system keyring.

Return type

None