Skip to content

feat(account): make email optional for OAuth2 account linking - #13255

Merged
Meldiron merged 4 commits into
mainfrom
feat-oauth2-optional-email
Aug 24, 2026
Merged

Meldiron merged 4 commits into
mainfrom
feat-oauth2-optional-email

Conversation

@Meldiron

Copy link
Copy Markdown
Contributor

What does this PR do?

Makes email optional in the OAuth2 session/linking flow — only the provider's user ID remains required, matching how name is already optional. Also adds explicit protection so the same OAuth2 identity (provider + providerUid) cannot be connected to two accounts.

Email is now optional

  • Removed the USER_UNAUTHORIZED: 'OAuth provider failed to return email' failure when creating a new user.
  • When the provider returns no email, the flow skips: email conflict checks (identities/users with the same email), email parsing/canonicalization, the disposable/canonical/free/corporate email policies, email-based account matching, and the email messaging target.
  • New users are created with email = null (mirroring anonymous users) and emailVerification = false even if the provider claims verified: true.
  • Fixes false positives where providerEmail = '' queries could match other email-less identities, and a crash (GENERAL_INVALID_EMAIL via new Email('')) when an email-less user linked an email-less provider.
  • If the provider returns an email on a later login, the existing backfill behavior (uniqueness checks + policies) still applies unchanged.

Duplicate OAuth2 identity protection

The DB has always had a unique (provider, providerUid) index on identities, but nothing checked it at the application level. Linking an identity already connected to another user now fails cleanly with USER_ALREADY_EXISTS:

  • checked early in the logged-in linking flow (same shape as the existing email checks), before the user's current session is deleted;
  • with a Duplicate try/catch backstop at identity creation to cover races.

Test infrastructure

Adds a mock-no-email provider (excluded from SDK enums like the other mocks) plus testCreateOAuth2SessionWithoutEmail covering: account creation without email, re-login resolving to the same account, and a second user being blocked from linking the already-connected identity.

Test plan

  • New e2e test passes (27 assertions).
  • All 7 existing OAuth2 e2e tests pass (138 assertions): testCreateOAuth2AccountSession, testCreateOAuth2AccountSessionLoopback, testOAuth2TokenSessionProviderAccessToken, testCreateOidcOAuth2Token, testConvertAnonymousAccountOAuth2, testOAuthUnverifiedEmailCannotLinkToExistingAccount, testOAuthVerifiedEmailCanLinkToExistingAccount.
  • Pint, PHPStan (level 4), and Rector clean on all touched files.

Notes for reviewers

Cookie sessions still record factors: ['email', 'oauth2'] for email-less users — left unchanged deliberately since the oauth2 factor is what bypasses MFA; happy to make it conditional if preferred.

🤖 Generated with Claude Code

Only the OAuth2 provider user ID is now required, matching how name is
optional. When a provider returns no email, the flow skips email policy
enforcement, email-based account matching, email conflict checks, email
parsing, and the email messaging target; users are created with a null
email like anonymous accounts. This also fixes false positives where
empty-string providerEmail queries could match other email-less
identities, and a crash (GENERAL_INVALID_EMAIL) when an email-less user
linked an email-less provider.

Also adds explicit duplicate-identity protection: linking an OAuth2
identity (provider + providerUid) already connected to another user now
fails cleanly with USER_ALREADY_EXISTS instead of surfacing a raw
Duplicate from the unique index, checked early in the linking flow
before the current session is deleted, with a try/catch backstop at
identity creation for races.

Adds a mock-no-email provider (excluded from SDK enums like the other
mocks) and an e2e test covering account creation without email,
re-login resolving to the same account, and the duplicate-identity
protection.

Co-Authored-By: Claude Fable 5 <[email protected]>
@Meldiron
Meldiron marked this pull request as ready for review August 17, 2026 12:23

@Meldiron Meldiron left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

lgtm

@greptile-apps

greptile-apps Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR allows OAuth providers without email addresses while preserving identity uniqueness and email-target consistency.

  • Skips email parsing, matching, and policy checks when providers return no email.
  • Creates email-less users and supports later verified-email backfill with a messaging target.
  • Rejects duplicate provider identities and cleans up users created by losing identity-insertion races.
  • Adds a no-email mock provider and end-to-end coverage for creation, relogin, and duplicate linking.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
app/controllers/api/account.php Updates OAuth account creation, linking, email backfill, duplicate-identity handling, and race cleanup; the previously reported failures are addressed.
app/config/oAuthProviders.php Registers the mock no-email OAuth provider for test use.
app/controllers/mock.php Extends the mock OAuth flow to support a provider response without an email.
src/Appwrite/Auth/OAuth2/MockNoEmail.php Implements the email-less OAuth provider fixture.
src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Get.php Keeps the test-only no-email provider excluded from the public provider enumeration.
tests/e2e/Services/Account/AccountCustomClientTest.php Covers email-less account creation, relogin resolution, and rejection of linking an identity already owned by another account.

Reviews (4): Last reviewed commit: "Merge branch 'main' into feat-oauth2-opt..." | Re-trigger Greptile

Comment thread app/controllers/api/account.php Resolved
@github-actions

github-actions Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → feat-oauth2-optional-email (after).

Metric Before After Change
🚀 Requests/sec 302.08 305.82 ⚪ +1.2%
⏱️ Latency P50 51.1 ms 49.84 ms ⚪ -2.5%
⏱️ Latency P95 155.93 ms 154.62 ms ⚪ -0.8%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 49.84 154.62 18,981 305.82 -1.31
Account 78.49 198.97 999 16.66 +0.55
TablesDB 47.78 151.83 10,323 168.07 +2.14
Storage 45.67 143.48 4,995 83.01 -1.54
Functions 65.3 165.47 2,664 44.95 -5.37

Top API waits (after)

API request Max wait (ms)
tokens.get 1,016.88
storage.files.list 524.73
tablesdb.rows.update 459.74
tablesdb.rows.delete 452.47
functions.variables.create 446.58

Clean up the user (and email target) created within the same request
when identity creation loses the (provider, providerUid) unique index
race, so concurrent first logins with the same OAuth2 identity no
longer leave an orphan account.

Create the email messaging target when a later OAuth2 login backfills
the email of a previously email-less user, so messaging can resolve the
recipient; tolerate an existing target with the same identifier via the
unique index.

Extends the e2e test to cover the backfill path: an email-less OAuth2
user links a provider that returns an email and gains both the account
email and the messaging target.

Co-Authored-By: Claude Fable 5 <[email protected]>
@Meldiron

Copy link
Copy Markdown
Contributor Author

Addressed both Greptile P1 findings in 613d37f:

  • Identity race leaves orphan users — when identity creation loses the (provider, providerUid) unique-index race, the request now deletes the user and email target it just created before redirecting with USER_ALREADY_EXISTS. (This orphan predates the PR — previously the loser hit an uncaught Duplicate after persisting the user.)
  • Email backfill omits messaging target — backfilling the email of a previously email-less user now also creates the email messaging target, tolerating an existing target with the same identifier via the unique index. This gap also predates the PR for anonymous users linking OAuth2, and is covered now.

The e2e test was extended to cover the backfill path (email-less OAuth2 user links mock-unverified, gains the account email + messaging target — 35 assertions), and all 7 existing OAuth2 tests still pass (138 assertions).

Comment thread app/controllers/api/account.php Resolved
Meldiron and others added 2 commits August 24, 2026 12:03
Reconcile the canonical-email guard: keep this branch's !empty($email)
check for email-less OAuth providers alongside main's delivery-safe
canonicalization (!$canonicalize) exemption, and initialize
$canonicalize for the provider-without-email path.

Co-Authored-By: Claude Fable 5 <[email protected]>
@Meldiron
Meldiron merged commit 2772486 into main Aug 24, 2026
44 checks passed
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.

2 participants