Skip to content

chore(web): make session and OAuth token lifetimes configurable#1162

Merged
brendan-kellam merged 3 commits intomainfrom
brendan/configurable-session-lifetimes-SOU-946
Apr 30, 2026
Merged

chore(web): make session and OAuth token lifetimes configurable#1162
brendan-kellam merged 3 commits intomainfrom
brendan/configurable-session-lifetimes-SOU-946

Conversation

@brendan-kellam
Copy link
Copy Markdown
Contributor

@brendan-kellam brendan-kellam commented Apr 30, 2026

Fixes SOU-946

Summary

  • Adds two env vars for the Auth.js JWT browser session lifetime and exposes them on the NextAuth session config in packages/web/src/auth.ts:
    • AUTH_SESSION_MAX_AGE_SECONDS — default 2592000 (30 days)
    • AUTH_SESSION_UPDATE_AGE_SECONDS — default 86400 (1 day)
  • Adds three env vars for the OAuth flow TTLs and reads them directly in packages/web/src/ee/features/oauth/server.ts and packages/web/src/app/api/(server)/ee/oauth/token/route.ts:
    • OAUTH_AUTHORIZATION_CODE_TTL_SECONDS — default 600 (10 minutes)
    • OAUTH_ACCESS_TOKEN_TTL_SECONDS — default 3600 (1 hour)
    • OAUTH_REFRESH_TOKEN_TTL_SECONDS — default 7776000 (90 days)
  • Defaults match the existing hard-coded values, so this is a no-op for any deployment that doesn't set the new vars.
  • Documents all five new vars in docs/docs/configuration/environment-variables.mdx.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Authentication sessions and OAuth token lifetimes are now configurable via environment variables, letting administrators customize session expiration, session refresh frequency, and authorization-code, access-token, and refresh-token lifetimes. Defaults preserve existing behavior.
  • Documentation

    • Added and updated environment-variable reference docs describing the five new session and OAuth configuration options, their defaults, and expected behaviors.

- Auth.js JWT browser sessions now respect `AUTH_SESSION_MAX_AGE_SECONDS`
  and `AUTH_SESSION_UPDATE_AGE_SECONDS` (defaults: 30 days / 1 day,
  matching Auth.js's own defaults).
- OAuth flow TTLs (authorization code, access token, refresh token) now
  respect `OAUTH_AUTHORIZATION_CODE_TTL_SECONDS`,
  `OAUTH_ACCESS_TOKEN_TTL_SECONDS`, and `OAUTH_REFRESH_TOKEN_TTL_SECONDS`
  (defaults: 10 minutes / 1 hour / 90 days, matching the previously
  hard-coded values).

Defaults preserve today's behavior; operators who want shorter sessions
can lower these values without code changes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mintlify
Copy link
Copy Markdown

mintlify Bot commented Apr 30, 2026

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
sourcebot 🟢 Ready View Preview Apr 30, 2026, 3:50 AM

💡 Tip: Enable Workflows to automatically generate PRs for you.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 30, 2026

Walkthrough

Adds five environment variables to make authentication session lifetimes and OAuth token TTLs configurable; updates server env schema, NextAuth session config, OAuth server/token issuance to use the new env values, and documents the options in CHANGELOG and environment variables docs.

Changes

Cohort / File(s) Summary
Documentation
CHANGELOG.md, docs/docs/configuration/environment-variables.mdx
Documented five new environment variables for session expiry/update cadence and OAuth TTLs with explicit defaults and descriptions.
Environment Schema
packages/shared/src/env.server.ts
Added AUTH_SESSION_MAX_AGE_SECONDS, AUTH_SESSION_UPDATE_AGE_SECONDS, OAUTH_AUTHORIZATION_CODE_TTL_SECONDS, OAUTH_ACCESS_TOKEN_TTL_SECONDS, OAUTH_REFRESH_TOKEN_TTL_SECONDS to the server env schema with defaults.
NextAuth session config
packages/web/src/auth.ts
Plumbed maxAge and updateAge session values from environment variables into NextAuth session configuration.
OAuth server logic
packages/web/src/ee/features/oauth/server.ts, packages/web/src/app/api/(server)/ee/oauth/token/route.ts
Replaced hard-coded OAuth TTLs with env-driven TTLs (seconds → ms for expiresAt); removed exported ACCESS_TOKEN_TTL_SECONDS; token endpoint now uses env.OAUTH_ACCESS_TOKEN_TTL_SECONDS for expires_in.
Tests (mocks)
packages/web/src/ee/features/oauth/server.test.ts
Expanded shared @sourcebot/shared mock to include OAuth TTL env values for tests.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: making session and OAuth token lifetimes configurable through environment variables. It directly reflects the primary objective of the PR.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ 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 brendan/configurable-session-lifetimes-SOU-946

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 and usage tips.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions

This comment has been minimized.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/web/src/ee/features/oauth/server.ts (1)

6-12: ⚠️ Potential issue | 🟠 Major

Update test mock to include env from @sourcebot/shared.

The import of env at line 6 is used throughout the server functions (lines 41, 122, 131, 136, 187, 196, 201) to access TTL constants like env.OAUTH_ACCESS_TOKEN_TTL_SECONDS. The current mock at lines 12-18 in server.test.ts doesn't export env, causing tests to fail when these functions execute.

Current incomplete mock
vi.mock('@sourcebot/shared', () => ({
    hashSecret: vi.fn((s: string) => s),
    generateOAuthToken: vi.fn(() => ({ token: 'sboa_newtoken', hash: 'newtoken' })),
    generateOAuthRefreshToken: vi.fn(() => ({ token: 'sbor_newrefresh', hash: 'newrefresh' })),
    OAUTH_ACCESS_TOKEN_PREFIX: 'sboa_',
    OAUTH_REFRESH_TOKEN_PREFIX: 'sbor_',
}));

Add env with appropriate mock values, or spread the real module and override only the token generators.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/web/src/ee/features/oauth/server.ts` around lines 6 - 12, Tests fail
because the mock of `@sourcebot/shared` omits env used by functions like
generateOAuthToken/generateOAuthRefreshToken and constants
OAUTH_ACCESS_TOKEN_PREFIX/OAUTH_REFRESH_TOKEN_PREFIX; update the vi.mock in
server.test.ts to include an env export (e.g., env: {
OAUTH_ACCESS_TOKEN_TTL_SECONDS: <number>, OAUTH_REFRESH_TOKEN_TTL_SECONDS:
<number>, OAUTH_REFRESH_TOKEN_TTL_SECONDS_REMINDER: <number>, ... } matching the
keys used in server.ts) or instead import the real module and spread it while
overriding only hashSecret, generateOAuthToken, and generateOAuthRefreshToken;
ensure the mock exports env so calls to env.OAUTH_ACCESS_TOKEN_TTL_SECONDS and
similar resolve during tests.
🧹 Nitpick comments (1)
packages/web/src/app/api/(server)/ee/oauth/token/route.ts (1)

3-3: ⚡ Quick win

Use result.expiresIn instead of reading env again.

This keeps the route response aligned with the service return contract and avoids duplicated TTL sourcing in two layers.

Suggested simplification
-import { env, hasEntitlement } from '@sourcebot/shared';
+import { hasEntitlement } from '@sourcebot/shared';
...
-            expires_in: env.OAUTH_ACCESS_TOKEN_TTL_SECONDS,
+            expires_in: result.expiresIn,
...
-            expires_in: env.OAUTH_ACCESS_TOKEN_TTL_SECONDS,
+            expires_in: result.expiresIn,

Also applies to: 62-62, 94-94

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/web/src/app/api/`(server)/ee/oauth/token/route.ts at line 3, The
route currently re-reads TTL from env when building the response; instead use
the service return value result.expiresIn so the route response matches the
token service contract. Update any places that reference env.TOKEN_TTL /
env.OAUTH_TOKEN_TTL (used when constructing the JSON response around the result
object) to use result.expiresIn, and ensure the response payload uses
result.expiresIn consistently (e.g., where you set expiresIn or ttl fields after
calling the token generation service).
🤖 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/shared/src/env.server.ts`:
- Around line 147-175: The new TTL/session env vars
(AUTH_SESSION_MAX_AGE_SECONDS, AUTH_SESSION_UPDATE_AGE_SECONDS,
OAUTH_AUTHORIZATION_CODE_TTL_SECONDS, OAUTH_ACCESS_TOKEN_TTL_SECONDS,
OAUTH_REFRESH_TOKEN_TTL_SECONDS) need bounds and integer validation to prevent
negative or decimal values: update each numberSchema.default(...) to use integer
and min constraints (e.g., numberSchema.int().min(1) for values that must be
positive like AUTH_SESSION_MAX_AGE_SECONDS and the OAUTH_*_TTL_SECONDS values,
and numberSchema.int().min(0) for AUTH_SESSION_UPDATE_AGE_SECONDS since 0 is a
valid sentinel) so only non-negative (or strictly positive where required)
whole-second values are accepted.

---

Outside diff comments:
In `@packages/web/src/ee/features/oauth/server.ts`:
- Around line 6-12: Tests fail because the mock of `@sourcebot/shared` omits env
used by functions like generateOAuthToken/generateOAuthRefreshToken and
constants OAUTH_ACCESS_TOKEN_PREFIX/OAUTH_REFRESH_TOKEN_PREFIX; update the
vi.mock in server.test.ts to include an env export (e.g., env: {
OAUTH_ACCESS_TOKEN_TTL_SECONDS: <number>, OAUTH_REFRESH_TOKEN_TTL_SECONDS:
<number>, OAUTH_REFRESH_TOKEN_TTL_SECONDS_REMINDER: <number>, ... } matching the
keys used in server.ts) or instead import the real module and spread it while
overriding only hashSecret, generateOAuthToken, and generateOAuthRefreshToken;
ensure the mock exports env so calls to env.OAUTH_ACCESS_TOKEN_TTL_SECONDS and
similar resolve during tests.

---

Nitpick comments:
In `@packages/web/src/app/api/`(server)/ee/oauth/token/route.ts:
- Line 3: The route currently re-reads TTL from env when building the response;
instead use the service return value result.expiresIn so the route response
matches the token service contract. Update any places that reference
env.TOKEN_TTL / env.OAUTH_TOKEN_TTL (used when constructing the JSON response
around the result object) to use result.expiresIn, and ensure the response
payload uses result.expiresIn consistently (e.g., where you set expiresIn or ttl
fields after calling the token generation service).
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5e4b9a63-2c08-4d46-9425-190c68758563

📥 Commits

Reviewing files that changed from the base of the PR and between 7733ec9 and 1a4981f.

📒 Files selected for processing (6)
  • CHANGELOG.md
  • docs/docs/configuration/environment-variables.mdx
  • packages/shared/src/env.server.ts
  • packages/web/src/app/api/(server)/ee/oauth/token/route.ts
  • packages/web/src/auth.ts
  • packages/web/src/ee/features/oauth/server.ts

Comment thread packages/shared/src/env.server.ts
@brendan-kellam brendan-kellam merged commit d691e90 into main Apr 30, 2026
11 checks passed
@brendan-kellam brendan-kellam deleted the brendan/configurable-session-lifetimes-SOU-946 branch April 30, 2026 04:01
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.

1 participant