fix(oauth): decode ID token instead of calling Graph API for Microsoft providers#3727
Conversation
PR SummaryMedium Risk Overview Adds a shared Written by Cursor Bugbot for commit b1bdc76. Configure here. |
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Autofix Details
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Email fallback order reversed compared to old code
- Changed email fallback order from 'preferred_username || email || upn' to 'email || preferred_username || upn' to prioritize actual SMTP mail address over UPN, matching old Graph API behavior.
Or push these changes by commenting:
@cursor push bfe50f7c2d
Preview (bfe50f7c2d)
diff --git a/apps/sim/lib/auth/auth.ts b/apps/sim/lib/auth/auth.ts
--- a/apps/sim/lib/auth/auth.ts
+++ b/apps/sim/lib/auth/auth.ts
@@ -106,7 +106,7 @@
return {
id: `${payload.oid || payload.sub}-${crypto.randomUUID()}`,
name: payload.name || 'Microsoft User',
- email: payload.preferred_username || payload.email || payload.upn,
+ email: payload.email || payload.preferred_username || payload.upn,
emailVerified: true,
createdAt: now,
updatedAt: now,This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.
Greptile SummaryThis PR fixes a 500 error on Microsoft OAuth callbacks for external tenant users (B2B guests) by replacing Graph API Key changes:
Previous review concerns (malformed JSON payload, missing email guard) were addressed in b1bdc76. The lack of JWKS signature verification was acknowledged as out of scope for this fix and is low-risk given tokens arrive directly from Microsoft's token endpoint over TLS. Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant User
participant Sim as Sim OAuth Callback
participant MS as Microsoft Identity Platform
participant Helper as getMicrosoftUserInfoFromIdToken()
User->>MS: Authorize (openid + profile + email scopes)
MS-->>Sim: Authorization code
Sim->>MS: Exchange code for tokens
MS-->>Sim: access_token + id_token (JWT)
Note over Sim: OLD FLOW (removed)
Sim-xMS: GET /me (Graph API) — 403 for external users ❌
Note over Sim: NEW FLOW
Sim->>Helper: tokens (containing id_token)
Helper->>Helper: Split JWT, base64-decode payload
Helper->>Helper: Extract oid/sub, name, email/preferred_username/upn
Helper-->>Sim: { id, name, email, emailVerified, ... }
Sim-->>User: OAuth complete ✅
Reviews (2): Last reviewed commit: "fix(oauth): address review comments - tr..." | Re-trigger Greptile |
… fallback order, guard undefined email
|
@cursor review |

Summary
getUserInfocalls Graph API/mewithoutUser.Readscope → 403 Forbidden for external users → unhandled throw in better-auth → 500/mefor all 8 Microsoft providersgetMicrosoftUserInfoFromIdToken()helper, replacing ~187 lines of duplicated Graph API fetch logic with ~39 linesType of Change
Testing
Tested manually — verified ID token contains all required claims (
oid,name,preferred_username) whenopenid,profile,emailscopes are requestedChecklist