Skip to content

feat: support Microsoft Entra ID OAuth 2.0 for SMTP providers - #15810

Open
Harshit-2208 wants to merge 4 commits into
appwrite:mainfrom
Harshit-2208:feat/entra-id-smtp-oauth
Open

Harshit-2208 wants to merge 4 commits into
appwrite:mainfrom
Harshit-2208:feat/entra-id-smtp-oauth

Conversation

@Harshit-2208

Copy link
Copy Markdown

What does this PR do?

Adds support for Microsoft Entra ID (Azure AD) OAuth 2.0 (client_credentials flow + AUTH XOAUTH2) to Appwrite's SMTP email provider so organizations can send transactional emails through Microsoft 365 without relying on basic username/password SMTP authentication.

Summary of Changes

  • packages/messaging (Utopia\Messaging\Adapter\Email\SMTP):
    • Added optional $clientId, $clientSecret, and $tenantId parameters to SMTP::__construct().
    • Implemented fetchAccessToken() to request and cache short-lived OAuth 2.0 Bearer tokens from https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token using the https://outlook.office365.com/.default scope (with a 60-second expiry safety buffer).
    • Updated authenticators() to authenticate via Utopia\SMTP\Auth\XOAuth2 when OAuth credentials are configured, while preserving existing PLAIN/LOGIN authentication when using Basic Auth.
  • Messaging API & Provider Factory (src/Appwrite/):
    • Added optional clientId, clientSecret, and tenantId parameters to createSMTPProvider and updateSMTPProvider, validating that partial OAuth configurations are rejected with 400 general_argument_invalid.
    • Forwarded the OAuth credentials from stored provider documents to Utopia\Messaging\Adapter\Email\SMTP in Appwrite\Messaging\Provider::email().
    • Added clientSecret to Appwrite\Utopia\Response\Model\Provider::filter() so secrets are redacted from API responses.
    • Updated app/config/sdks/parameters.lock.json for the new optional parameters.
  • Tests:
    • Added unit tests in packages/messaging/tests/Adapter/Email/SMTPOAuthTest.php covering token acquisition, caching, expiry refresh, error handling, and PLAIN fallback.
    • Updated tests/unit/Utopia/Response/Model/ProviderTest.php to verify clientSecret redaction.
    • Added E2E tests in tests/e2e/Services/Messaging/MessagingBase.php for creating and updating SMTP providers with Entra ID OAuth 2.0 credentials.

Configuration Note for Microsoft 365 Administrators

To use this with Microsoft 365 (smtp.office365.com on port 587 with TLS):

  1. Register an application in Microsoft Entra ID and grant the SMTP.SendAsApp application permission (Office 365 Exchange Online).
  2. Register the application's Service Principal in Exchange Online (New-ServicePrincipal) and grant it SendAs or FullAccess permission on the target sender mailbox.
  3. Configure the Appwrite SMTP provider with the mailbox address as username, along with clientId, clientSecret, and tenantId.

Test Plan

  • SMTPOAuthTest: Verified OAuth token request format, AUTH XOAUTH2 handshake against ScriptedSmtpServer, token caching/refresh behavior, and Basic Auth fallback.
  • ProviderTest: Verified clientSecret is stripped from API responses while clientId and tenantId remain visible.
  • MessagingBase: Added E2E tests covering POST /v1/messaging/providers/smtp and PATCH /v1/messaging/providers/smtp/:providerId with valid and partial Entra ID OAuth credentials.

Related PRs and Issues

Closes #15482

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

@appwrite

appwrite Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Code review

Commit Status Tier Verdict Details
90fcf71 Reviewed Reviewed 🟢 S · Ready to merge 💬 Commented View review

The new commits resolve the outstanding OAuth-clearing and token-retry findings, and I found no additional issues in this increment.

Adds Microsoft Entra ID client-credentials authentication to SMTP providers, including access-token caching and XOAUTH2 authentication. Extends the SMTP provider APIs and adapter factory with OAuth credentials, redacts client secrets, and adds package, API, and response-model tests. The update API also supports clearing OAuth credentials to return to Basic Auth.

Latest changes: The latest commits allow explicitly clearing all OAuth credentials during an SMTP provider update and include HTTP status and OAuth error details in token-fetch exceptions, with regression tests for transient 503 responses.

Note

@Harshit-2208 does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve.

Walkthrough (9)
File Change
app/config/sdks/parameters.lock.json Locks the added OAuth parameters for the current and deprecated SMTP provider SDK methods.
packages/messaging/src/Adapter/Email/SMTP.php Fetches and caches Entra access tokens and selects XOAUTH2 when OAuth credentials are configured.
packages/messaging/tests/Adapter/Email/SMTPOAuthTest.php Covers token requests, caching, refresh, token errors, and Basic Auth fallback.
src/Appwrite/Messaging/Provider.php Passes stored OAuth credentials into the SMTP adapter.
src/Appwrite/Platform/Modules/Messaging/Http/Providers/Smtp/Create.php Accepts and validates complete OAuth credential sets when creating SMTP providers.
src/Appwrite/Platform/Modules/Messaging/Http/Providers/Smtp/Update.php Validates OAuth credential updates and supports clearing the OAuth fields.
src/Appwrite/Utopia/Response/Model/Provider.php Redacts clientSecret from provider API responses.
tests/e2e/Services/Messaging/MessagingBase.php Exercises SMTP OAuth provider creation, updates, credential clearing, and validation.
tests/unit/Utopia/Response/Model/ProviderTest.php Verifies clientSecret redaction while OAuth identifiers remain visible.
Fixed since the last review (4)
  • Allow OAuth credentials to be explicitly cleared · src/Appwrite/Platform/Modules/Messaging/Http/Providers/Smtp/Update.php:177
  • Preserve transient token-endpoint signals for delivery retries · packages/messaging/src/Adapter/Email/SMTP.php:434
  • Allow OAuth credentials to be removed when switching auth modes · src/Appwrite/Platform/Modules/Messaging/Http/Providers/Smtp/Update.php:177
  • Preserve the token endpoint status for retry classification · packages/messaging/src/Adapter/Email/SMTP.php:434

Merged main (7ba9704); no changes to this PR's code since 90fcf71

Tip

Comment @appwrite review to re-run, or mention @appwrite with a question.

@appwrite appwrite Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Tier B · See the inline comments. Summary

@appwrite appwrite Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Tier B · See the inline comments. Summary

@appwrite appwrite Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Add Support for Azure EntraID for SMTP

1 participant