Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughA new feature enables organization switching during OAuth refresh-token exchanges via an optional Changes
Sequence DiagramsequenceDiagram
participant Client
participant TokenEndpoint as Token Endpoint
participant RefreshGrant as Refresh Grant Handler
participant OrgStore as Organizations/Data Layer
Client->>TokenEndpoint: POST /oauth/token (grant_type=refresh_token, organization=?)
TokenEndpoint->>RefreshGrant: Parse & validate request
RefreshGrant->>RefreshGrant: Determine effectiveOrganization (param or login session)
alt organization provided
RefreshGrant->>OrgStore: Lookup organization by id/name
OrgStore-->>RefreshGrant: Organization found / not found
alt found
RefreshGrant->>OrgStore: Verify user membership
OrgStore-->>RefreshGrant: Member / not member
alt member
RefreshGrant-->>TokenEndpoint: Grant result with resolved organization
else not member
RefreshGrant-->>TokenEndpoint: Error 403 access_denied
end
else not found
RefreshGrant-->>TokenEndpoint: Error 400 invalid_request
end
else no organization provided
RefreshGrant-->>TokenEndpoint: Grant result using login session org (or none)
end
TokenEndpoint->>Client: 200 OK (access_token, id_token, org claims as applicable)
Estimated Code Review Effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly Related Issues
Possibly Related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/authhero/src/authentication-flows/refresh-token.ts`:
- Line 16: The schema currently allows an empty organization string via
organization: z.string().optional(), which lets "" bypass the login-session org
fallback; update the schema to reject blank values by using
z.string().nonempty().optional() (or z.string().min(1).optional()) for the
organization field in refresh-token input schema so empty strings are treated as
invalid and the fallback logic (the code referencing organization with ??) will
work as intended.
- Around line 89-107: The code currently attaches an organization to the grant
result after only checking that the organization exists (effectiveOrganization →
ctx.env.data.organizations.get) which can let tokens be issued without verifying
the subject is a member; ensure membership is validated whenever an organization
is set by adding an unconditional membership check: after resolving
effectiveOrganization in refreshTokenGrant (the block that uses
ctx.env.data.organizations.get) call the memberships lookup used elsewhere
(e.g., ctx.env.data.memberships.get or the same membership check logic used in
packages/authhero/src/routes/auth-api/token.ts) to confirm the user
(loginSession.subject or grantResult.subject) belongs to that organization, and
throw a 400/401 JSONHTTPException if not; alternatively, if you prefer
centralizing the check, move the unconditional membership validation into the
token route just before createAuthTokens so createAuthTokens never receives an
organization unless membership has been verified.
🪄 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: defaults
Review profile: CHILL
Plan: Pro
Run ID: f069c154-4d17-4211-aa90-ca21fd662766
📒 Files selected for processing (9)
.changeset/giant-mice-dance.mdapps/docs/api/endpoints.mdapps/docs/auth0-comparison/index.mdapps/docs/entities/identity/organizations.mdapps/docs/entities/security/tokens.mdapps/docs/features/authentication-flows.mdpackages/authhero/src/authentication-flows/refresh-token.tspackages/authhero/src/routes/auth-api/token.tspackages/authhero/test/routes/auth-api/token.spec.ts
|
Implements #677 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/authhero/src/authentication-flows/refresh-token.ts (1)
109-119: Consider usingfetchAll()to handle pagination for users with >1000 organization memberships.The current hardcoded
per_page: 1000limit could miss memberships if a user belongs to more than 1000 organizations. Instead of a static limit, use the existingfetchAll()utility (available from the authhero package) to properly paginate through all results:const userOrgs = await fetchAll<Organization>( (params) => ctx.env.data.userOrganizations.listUserOrganizations(client.tenant.id, user.user_id, params), 'organizations' ); const isMember = userOrgs.some((org) => org.id === organization!.id);This pattern is also used elsewhere in the codebase (e.g.,
packages/multi-tenancy/src/routes/tenants.ts) for similar membership checks and properly handles pagination without a fixed limit.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/authhero/src/authentication-flows/refresh-token.ts` around lines 109 - 119, Replace the hardcoded paginated call to ctx.env.data.userOrganizations.list (which sets per_page: 1000) with the fetchAll helper to ensure all memberships are retrieved; specifically, call fetchAll with a function that invokes listUserOrganizations (passing client.tenant.id and user.user_id) to collect all organizations into userOrgs, then compute isMember by checking whether any organization in that returned array matches organization!.id (replace the existing userOrgs.userOrganizations.some(...) logic). Ensure you import/require fetchAll if not already available.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/authhero/src/authentication-flows/refresh-token.ts`:
- Around line 109-119: Replace the hardcoded paginated call to
ctx.env.data.userOrganizations.list (which sets per_page: 1000) with the
fetchAll helper to ensure all memberships are retrieved; specifically, call
fetchAll with a function that invokes listUserOrganizations (passing
client.tenant.id and user.user_id) to collect all organizations into userOrgs,
then compute isMember by checking whether any organization in that returned
array matches organization!.id (replace the existing
userOrgs.userOrganizations.some(...) logic). Ensure you import/require fetchAll
if not already available.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: a6d6797c-df0a-4143-ac6e-5f68ec2c0546
📒 Files selected for processing (2)
apps/docs/auth0-comparison/index.mdpackages/authhero/src/authentication-flows/refresh-token.ts
✅ Files skipped from review due to trivial changes (1)
- apps/docs/auth0-comparison/index.md
Summary by CodeRabbit
New Features
Documentation
Tests