Skip to content

fix(integration-github): fetch primary email from /user/emails#367

Merged
gvieira18 merged 3 commits into
4.xfrom
fix/github-private-email
Jun 25, 2026
Merged

fix(integration-github): fetch primary email from /user/emails#367
gvieira18 merged 3 commits into
4.xfrom
fix/github-private-email

Conversation

@gvieira18

@gvieira18 gvieira18 commented Jun 22, 2026

Copy link
Copy Markdown
Member

Summary

  • GitHub's /user endpoint only returns a public email, so accounts with a private email yield null. On "Sign in with GitHub" (OAuthIntent::Login), FindOrCreateUserByProvider can't match an existing user by email and forks a duplicate user (observed: a Discord-linked user got a second …-2 account on GitHub login).
  • Adds a GetUserEmails request (GET /user/emails, covered by the existing user:email scope) and falls back to it when /user returns null, selecting the single primary + verified address — requiring both flags so secondary verified emails (work/school/noreply) can't match the wrong account.

Closes #366

Test plan

  • pest --filter=GitHubOAuthClient — 4 cases: public email used as-is; private → primary-verified fallback; verified-but-not-primary → null; primary-but-not-verified → null.
  • Manual: with a private GitHub email, sign in with GitHub and confirm it links to the existing account (matching primary verified email) instead of creating a new user.

@gvieira18 gvieira18 requested a review from a team June 22, 2026 03:23
@gvieira18 gvieira18 added identity identity module mod:integration-github GitHub integration module labels Jun 22, 2026
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 92360ded-8cb4-48bd-b4d3-cc461a0e5726

📥 Commits

Reviewing files that changed from the base of the PR and between 897e838 and fdec0c4.

📒 Files selected for processing (5)
  • app-modules/identity/src/Auth/Exceptions/OAuthFlowException.php
  • app-modules/identity/tests/Feature/Auth/HandleOAuthCallbackActionTest.php
  • app-modules/integration-github/src/OAuth/GitHubOAuthClient.php
  • app-modules/integration-github/src/Transport/Requests/Users/GetUserEmails.php
  • app-modules/integration-github/tests/Feature/GitHubOAuthClientTest.php
🚧 Files skipped from review as they are similar to previous changes (2)
  • app-modules/integration-github/src/OAuth/GitHubOAuthClient.php
  • app-modules/integration-github/src/Transport/Requests/Users/GetUserEmails.php

📝 Walkthrough

Walkthrough

GitHubOAuthClient now resolves a fallback email from GET /user/emails when /user returns email: null, and throws OAuthFlowException::emailUnavailable('github') when no primary verified email is available. A new GetUserEmails request class is added. Feature tests cover GitHub email selection and HandleOAuthCallbackAction link versus login behavior with differing emails.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: fetching GitHub email from /user/emails when /user lacks it.
Description check ✅ Passed The description matches the changes and explains the private-email fallback and duplicate-user fix.
Linked Issues check ✅ Passed The changes implement #366 by falling back to /user/emails and selecting the primary verified email.
Out of Scope Changes check ✅ Passed All added code and tests support the GitHub email fallback and duplicate-user fix; no unrelated changes stand out.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@app-modules/integration-github/src/OAuth/GitHubOAuthClient.php`:
- Around line 82-84: The GitHubOAuthClient currently returns null when the
/user/emails API response fails (the if (!$response->ok()) check), allowing the
login flow to continue with incomplete identity data and risking duplicate
account creation. Replace this silent failure with explicit fail-closed behavior
by throwing an exception instead of returning null, ensuring the application
explicitly fails when email recovery fails rather than silently proceeding with
unresolved identity information.

In `@app-modules/integration-github/tests/Feature/GitHubOAuthClientTest.php`:
- Around line 56-93: The test file is missing coverage for the scenario when the
/user/emails endpoint returns a non-OK response. Add a new test case that mocks
a failed response from the GetUserEmails class (using MockResponse or similar to
simulate an error) in the githubOAuthClient helper, then verify the expected
behavior of the getAuthenticatedUser method in the GitHubOAuthAccessDTO context.
This ensures the resolvePrimaryEmail method properly handles and recovers from
failed email endpoint requests.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 31d4b997-b735-48ac-b6fa-c1e591aa4204

📥 Commits

Reviewing files that changed from the base of the PR and between bc6c29e and 897e838.

📒 Files selected for processing (3)
  • app-modules/integration-github/src/OAuth/GitHubOAuthClient.php
  • app-modules/integration-github/src/Transport/Requests/Users/GetUserEmails.php
  • app-modules/integration-github/tests/Feature/GitHubOAuthClientTest.php

Comment thread app-modules/integration-github/src/OAuth/GitHubOAuthClient.php
@gvieira18

Copy link
Copy Markdown
Member Author

Kept as a draft intentionally — the minimal fix here closes the realistic private-email case, but there are unresolved edge cases (no verified email / failed /user/emails / scope misconfig) still under discussion in #366.

cc @danielhe4rt — please validate the approach and weigh in on the open edge cases in this comment before we mark ready.

GitHub's /user endpoint only exposes a public email, so accounts with a
private email return null. OAuth login then can't match an existing user
by email and forks a duplicate. Fall back to /user/emails (covered by the
user:email scope) and use the single primary verified address.

Closes #366
When /user/emails yields no primary+verified address there is no
trustworthy email to correlate on, so abort the login instead of
silently forking a duplicate user.

Refs #366
Document the callback orchestrator: linking a second provider while
logged in attaches to the same user (email mismatch is a non-event),
while a logged-out login with a different email forks a new account.
@gvieira18 gvieira18 force-pushed the fix/github-private-email branch from 897e838 to fdec0c4 Compare June 24, 2026 16:52
@gvieira18 gvieira18 marked this pull request as ready for review June 24, 2026 16:53
@gvieira18 gvieira18 removed the request for review from danielhe4rt June 25, 2026 23:32
@gvieira18 gvieira18 merged commit 407b7c0 into 4.x Jun 25, 2026
11 checks passed
@gvieira18 gvieira18 deleted the fix/github-private-email branch June 25, 2026 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

identity identity module mod:integration-github GitHub integration module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(integration-github): private GitHub email forks a duplicate user on login

2 participants