mutt-oauth2¶
This is an update of Alexander Perlis’ script and conversion to a package. Instead of using GPG for token storage, this package uses Keyring.
Installation¶
Commands¶
mutt-oauth2¶
Obtain and print a valid OAuth2 access token.
Usage
mutt-oauth2 [OPTIONS]
Options
- -a, --authorize¶
Manually authorise new tokens.
- -d, --debug¶
Enable debug logging.
- -t, --test¶
Test authentication.
- -u, --username <username>¶
Keyring username.
Usage¶
Start by calling mutt-oauth2 -a. Be sure to have your client ID and and client secret available.
Scopes required¶
Provider |
Scopes |
|---|---|
Gmail |
Gmail API |
Microsoft |
offline_access IMAP.AccessAsUser.All POP.AccessAsUser.All SMTP.Send |
To support other accounts, use the --username argument with a unique string such as the account
email address.
Test the script with the --test argument.
mutt configuration¶
Add the following to muttrc:
set imap_authenticators="oauthbearer:xoauth2"
set imap_oauth_refresh_command="/path/to/mutt-oauth2"
set smtp_authenticators=${imap_authenticators}
set smtp_oauth_refresh_command=${imap_oauth_refresh_command}
Library¶
Constants.
- mutt_oauth2.constants.KEYRING_SERVICE_NAME¶
Keyring service name.
Registration information for OAuth2 providers.
- mutt_oauth2.registrations.registrations¶
Registrations.
Utilities.
- exception mutt_oauth2.utils.OAuth2Error¶
Generic OAuth2 error.
-
class mutt_oauth2.utils.SavedToken(access_token_expiration: datetime | None, client_id: str, client_secret: str | None, email: str, registration: Registration, access_token: str =
'', refresh_token: str ='', tenant: str | None =None)¶ Data class for OAuth2 token information.
- async device_poll(device_code: str, session: AsyncSession) Any¶
Poll the device code endpoint for the access token.
- Parameters:¶
- device_code : str¶
Device code from
get_device_code().- session : niquests.AsyncSession¶
HTTP session.
- Returns:¶
Token response, or an error payload while authorisation is pending.
- Return type:¶
Any
- Raises:¶
OAuth2Error – If polling fails with a terminal error.
- async exchange_auth_for_access(auth_code: str, verifier: str, redirect_uri: str, session: AsyncSession) Any¶
Exchange the authorisation code for an access token.
- Parameters:¶
- Returns:¶
Token response data from the authorisation server.
- Return type:¶
Any
- Raises:¶
OAuth2Error – If the token exchange fails.
- static from_keyring(username: str) SavedToken | None¶
Create an instance using the Keyring.
- async get_device_code(session: AsyncSession) Any¶
Get the device code.
- Parameters:¶
- session : niquests.AsyncSession¶
HTTP session.
- Returns:¶
Device authorisation response from the server.
- Return type:¶
Any
- Raises:¶
OAuth2Error – If the device code request fails.
- async refresh(username: str, session: AsyncSession) None¶
Refresh the access token using the refresh token.
- Parameters:¶
- username : str¶
Keyring username.
- session : niquests.AsyncSession¶
HTTP session.
- Raises:¶
OAuth2Error – If the token refresh fails.
- registration : Registration¶
Registration.
- mutt_oauth2.utils.get_localhost_redirect_uri() tuple[int, str]¶
Find an available port and return a localhost URI.
-
async mutt_oauth2.utils.try_auth(token: SavedToken, *, debug: bool =
False) None¶ Try to authenticate using the passed-in token.
- Parameters:¶
- token : SavedToken¶
Token to test.
- debug : bool¶
Enable debug output on protocol connections.
- Raises:¶
RuntimeError – If any authentication test fails.