Add PhoneAppOTP (TOTP) support for MFA authentication#445
Open
tuaris wants to merge 1 commit intomguessan:masterfrom
Open
Add PhoneAppOTP (TOTP) support for MFA authentication#445tuaris wants to merge 1 commit intomguessan:masterfrom
tuaris wants to merge 1 commit intomguessan:masterfrom
Conversation
Support PhoneAppOTP (SoftwareTokenBasedTOTP) as an MFA method in O365Authenticator, with optional automatic TOTP code generation. Changes: - Recognize PhoneAppOTP in the MFA method selection loop (highest priority) - Handle PhoneAppOTP in retrieveSmsCode() with interactive prompt - Handle PhoneAppOtpAuthFailedDuplicateCodeEntered in EndAuth polling loop - Add optional davmail.oauth.totpSecret property for automatic TOTP code generation using RFC 6238 (HMAC-SHA1) with standard Java crypto (no external dependencies) When davmail.oauth.totpSecret is set with a Base32-encoded TOTP secret, DavMail generates codes automatically, enabling fully headless daemon operation without stdin interaction. When not set, falls back to interactive prompt (stdin in server/headless mode, GUI dialog otherwise). This fixes authentication for accounts where PhoneAppOTP is the default MFA method. Previously DavMail silently skipped it and fell back to OneWaySMS, which can be rate-limited by Microsoft (BadReputation error).
289736c to
933c19e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Support PhoneAppOTP (SoftwareTokenBasedTOTP) as an MFA method in O365Authenticator, with optional automatic TOTP code generation.
EDIT: I just noticed there was an existing PR for this feature: #405. The difference with mine is the support for multiple users/accounts.
Changes
When davmail.oauth.totpSecret is set with a Base32-encoded TOTP secret, DavMail generates codes automatically, enabling fully headless daemon operation without stdin interaction. When not set, falls back to interactive prompt (stdin in server/headless mode, GUI dialog otherwise).
This fixes authentication for accounts where PhoneAppOTP is the default MFA method. Previously DavMail silently skipped it and fell back to OneWaySMS, which can be rate-limited by Microsoft (BadReputation error).
New configuration properties
Lookup order:
davmail.oauth.totpSecret.<username>→davmail.oauth.totpSecret→ interactive prompt.Usage
Single-user setup
Add the TOTP secret to
davmail.properties:davmail.oauth.totpSecret=YOUR_BASE32_TOTP_SECRETStart DavMail normally. When MFA is required, DavMail generates the TOTP code automatically.
Multi-user setup
Configure per-user secrets:
Each user's TOTP code is generated from their own secret. Users without a configured secret will be prompted interactively.
TOTP secret encryption
TOTP secrets follow the same encrypt-on-first-use pattern as OAuth refresh tokens, using
the existing
StringEncryptorclass (PBE with AES-128, keyed by the user's O365 password).On first authentication, if the TOTP secret is stored in plaintext, DavMail automatically
encrypts it and saves it back to the config file with an
{AES}prefix:On subsequent authentications, the
{AES}prefix is detected and the secret is decryptedusing the user's password before generating the TOTP code.
Note: The DavMail process must have write access to
davmail.propertiesforencrypt-on-first-use to work. Ensure the config file is owned by the user running DavMail.
Interactive mode (no secret configured)
If no
totpSecretis configured for a user, DavMail falls back to:Enter TOTP code:on stdinThis allows the first interactive login to cache a refresh token for subsequent daemon runs.
Important: OAuth client ID
For headless/server deployments, you will likely need to use the Microsoft Office client
ID (
d3590ed6-52b3-4102-aeff-aad2292ab01c) instead of DavMail's default client ID.DavMail's built-in client ID (
facd6cff-a294-4415-b59f-c5b01937d7bd) triggers anAzure AD consent prompt (
arrScopescheck) that cannot be completed without a browser.The Microsoft Office client ID is pre-consented in all Azure AD tenants and requires
the redirect URI
urn:ietf:wg:oauth:2.0:oob: