Skip to content

feat(core): implement getProviderTokens for stateful sessions#231

Merged
halvaradop merged 4 commits into
masterfrom
feat/implement-get-provider-tokens
Jul 23, 2026
Merged

feat(core): implement getProviderTokens for stateful sessions#231
halvaradop merged 4 commits into
masterfrom
feat/implement-get-provider-tokens

Conversation

@halvaradop

@halvaradop halvaradop commented Jul 18, 2026

Copy link
Copy Markdown
Member

Description

This pull request adds Stateful session support for the getProviderTokens() APIs.

The implementation extends both the server-side api.getProviderTokens() API and the GET /providers/:provider/tokens endpoint to support the experimental Stateful session strategy. The new functionality has been thoroughly tested to ensure consistent behavior across both Stateless and Stateful session strategies.

For more information about these APIs, see the official documentation:

Key Changes

  • Added Stateful session support for api.getProviderTokens().
  • Added Stateful session support for the GET /providers/:provider/tokens endpoint.
  • Added integration tests covering the Stateful implementation.
  • Verified consistent behavior across both session strategies.
  • Fixed inconsistencies discovered while implementing Stateful support.
  • Refactored shared provider token logic to improve maintainability.

Note

Expanding support for getProviderTokens() uncovered several inconsistencies between the existing Stateless implementation and the new Stateful implementation. As part of this work, the authentication flow was refactored to provide a more consistent behavior across both strategies. These improvements also affected related flows such as signUp(), signInCredentials(), and getProviderTokens().

Note

This PR is part of the ongoing effort to implement the Stateful session strategy. To keep reviews focused and manageable, the implementation has been split into a series of smaller pull requests, each covering a specific aspect of the feature.

Related PRs

@coderabbitai ignore

@vercel

vercel Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
auth Skipped Skipped Jul 23, 2026 4:40pm

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds provider-token retrieval to stateful and stateless session strategies, centralizes OAuth refresh handling, wires OAuth configuration through strategy creation, and delegates the API flow to the active session strategy.

Changes

Provider token retrieval

Layer / File(s) Summary
Session strategy contracts and OAuth wiring
packages/core/src/@types/..., packages/core/src/session/strategy.ts, packages/core/src/router/context.ts
Session strategies expose getProviderTokens, accept OAuth configuration, identify the session strategy mode, and receive configuration through strategy construction.
Shared validation and OAuth refresh
packages/core/src/shared/utils.ts, packages/core/src/shared/utils/api.ts, packages/core/src/shared/utils/refresh-tokens.ts
Database sessions use cookie-presence validation, and shared refresh logic validates configuration, sends OAuth refresh requests, and merges returned token fields.
Stateful and stateless strategy retrieval
packages/core/src/session/stateful.ts, packages/core/src/session/stateless.ts, packages/core/src/shared/logger.ts
Stateful retrieval loads session-backed OAuth accounts and persists refreshed tokens; stateless retrieval reads and updates provider-token cookies, with standardized errors and logging.
API delegation and validation
packages/core/src/api/getProviderTokens.ts, packages/core/src/api/signInCredentials.ts
The API delegates retrieval to the session strategy and returns its tokens, headers, status, and errors; credential sign-in logs caught errors.
Flow tests and mock setup
packages/core/test/...
Provider-token retrieval, refresh outcomes, validation failures, cleanup, and session-producing sign-in/sign-up mocks are covered or adjusted.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant getProviderTokens
  participant SessionStrategy
  participant OAuthProvider
  Client->>getProviderTokens: Request provider tokens
  getProviderTokens->>SessionStrategy: getProviderTokens(oauth, request)
  SessionStrategy->>OAuthProvider: Refresh when required
  OAuthProvider-->>SessionStrategy: Return refreshed tokens
  SessionStrategy-->>getProviderTokens: Return tokens, headers, or error
  getProviderTokens-->>Client: Return JSON response
Loading

Possibly related PRs

Suggested labels: feature

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title matches the main feature, though the PR also adds stateless provider-token support and related plumbing.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/implement-get-provider-tokens

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: 5

🤖 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 `@packages/core/src/`@types/session.ts:
- Around line 239-245: Restore the optional skipCSRFCheck?: boolean parameter in
the refreshSession interface declaration, matching the third argument used by
the stateful.ts and stateless.ts implementations and preserving its
default-false behavior.

In `@packages/core/src/session/stateful.ts`:
- Line 664: Update all response branches in
packages/core/src/session/stateful.ts around the session response handling to
stop passing request.headers and instead construct fresh response-only headers,
merging only intentional Set-Cookie values. In
packages/core/src/session/stateless.ts around lines 211-215, remove
request-header merging, use the secure cookie builder after refresh, and use
fresh secureApiHeaders in other branches.
- Around line 689-704: Update the OAuth account flow in the stateful
provider-token handler to scope getOAuthAccount to both oauthId and
sessionByToken.userId, or verify the returned account belongs to that user
before reading tokens. Ensure the same ownership constraint is applied to
updateOAuthTokens so another user’s account cannot be disclosed or overwritten,
updating the adapter contract if needed.
- Around line 667-680: Update the session validation in the provider-token
retrieval flow around getSessionByToken to reject sessions whose status is
inactive or whose expiresAt is in the past, in addition to missing sessions or
users. Reuse the same validity criteria and invalid-session response already
established by getSession, while preserving the existing provider-token success
path for active, unexpired sessions.

In `@packages/core/src/shared/utils/refresh-tokens.ts`:
- Around line 27-34: Update the refresh-token request construction to omit
client-secret authentication entirely when provider.clientSecret is undefined,
preventing both createBasicAuthHeader and URLSearchParams from receiving an
undefined secret. In the same logic, replace the typeof provider.refreshToken
=== "object" checks with the existing isRefreshTokenObject guard for headers and
params access, preserving support for object-form refresh-token configuration.
🪄 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: 05149e4e-a060-4eae-ba9f-2061e9b1090f

📥 Commits

Reviewing files that changed from the base of the PR and between bf4732c and 143e617.

📒 Files selected for processing (9)
  • packages/core/src/@types/session.ts
  • packages/core/src/api/getProviderTokens.ts
  • packages/core/src/router/context.ts
  • packages/core/src/session/stateful.ts
  • packages/core/src/session/stateless.ts
  • packages/core/src/session/strategy.ts
  • packages/core/src/shared/logger.ts
  • packages/core/src/shared/utils/refresh-tokens.ts
  • packages/core/test/api/getProviderTokens.test.ts

Comment thread packages/core/src/@types/session.ts Outdated
Comment thread packages/core/src/session/stateful.ts Outdated
Comment thread packages/core/src/session/stateful.ts
Comment thread packages/core/src/session/stateful.ts Outdated
Comment thread packages/core/src/shared/utils/refresh-tokens.ts Outdated
@halvaradop
halvaradop force-pushed the feat/implement-get-provider-tokens branch from 143e617 to 07593e8 Compare July 23, 2026 16:04

@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: 3

🤖 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 `@packages/core/src/api/signInCredentials.ts`:
- Line 66: Remove the unconditional console.error call from the catch block in
signInCredentials; rely on the existing injected logger handling, including the
AUTH_CREDENTIALS_INVALID warning path, and keep behavior consistent with
getProviderTokens without emitting raw credential-related errors to stdout.

In `@packages/core/src/shared/utils.ts`:
- Around line 152-167: Update database-mode verifySession() to perform the
stateful session record and status validation instead of calling
verifyPresentSessionValue(), which only checks cookie presence. Reuse the
stateful strategy’s getSession() validation on the authorization and call paths,
preserving existing error handling and session-token behavior.

In `@packages/core/test/actions/providers/tokens/tokens/stateful.test.ts`:
- Around line 175-209: Rename the test case around the OAuth account absence
scenario to state that it returns 401, matching the existing response.status
assertion and the sibling session-not-found test. Do not change the test
behavior or assertions.
🪄 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 Plus

Run ID: f153da0f-ede7-440e-85b0-168b5e5f8a24

📥 Commits

Reviewing files that changed from the base of the PR and between 143e617 and 07593e8.

📒 Files selected for processing (20)
  • packages/core/src/@types/config.ts
  • packages/core/src/@types/session.ts
  • packages/core/src/api/getProviderTokens.ts
  • packages/core/src/api/signInCredentials.ts
  • packages/core/src/router/context.ts
  • packages/core/src/session/stateful.ts
  • packages/core/src/session/stateless.ts
  • packages/core/src/session/strategy.ts
  • packages/core/src/shared/logger.ts
  • packages/core/src/shared/utils.ts
  • packages/core/src/shared/utils/api.ts
  • packages/core/src/shared/utils/refresh-tokens.ts
  • packages/core/test/actions/providers/tokens/tokens/stateful.test.ts
  • packages/core/test/actions/signIn/signInCredentials/stateful.test.ts
  • packages/core/test/actions/signUp/stateful.test.ts
  • packages/core/test/api/getProviderTokens.test.ts
  • packages/core/test/api/stateful/getProviderTokens.test.ts
  • packages/core/test/api/stateful/signInCredentials.test.ts
  • packages/core/test/api/stateful/signUp.test.ts
  • packages/core/test/presets.ts
🚧 Files skipped from review as they are similar to previous changes (8)
  • packages/core/src/session/strategy.ts
  • packages/core/test/api/getProviderTokens.test.ts
  • packages/core/src/session/stateless.ts
  • packages/core/src/session/stateful.ts
  • packages/core/src/shared/utils/refresh-tokens.ts
  • packages/core/src/shared/logger.ts
  • packages/core/src/api/getProviderTokens.ts
  • packages/core/src/@types/session.ts

Comment thread packages/core/src/api/signInCredentials.ts Outdated
Comment thread packages/core/src/shared/utils.ts
Comment thread packages/core/test/actions/providers/tokens/tokens/stateful.test.ts Outdated
@halvaradop
halvaradop merged commit 7bcc069 into master Jul 23, 2026
7 checks passed
@halvaradop
halvaradop deleted the feat/implement-get-provider-tokens branch July 23, 2026 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant