Skip to content

Add OIDC token preference for determining session expiry - #1337

Merged
taranvohra merged 2 commits into
mainfrom
taran/rnd-13568-add-oidc-token-preference-for-determining-session-expiry
Oct 30, 2026
Merged

taranvohra merged 2 commits into
mainfrom
taran/rnd-13568-add-oidc-token-preference-for-determining-session-expiry

Conversation

@taranvohra

@taranvohra taranvohra commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Closes RND-13568

image

What

Site admins can choose which token returned by their authentication provider drives the expiry of the GitBook session token, via a new Session Expiry select in the OIDC configuration.

  • ID token (default) — the exp claim, exactly as today, so existing installations are unaffected.
  • Access token — the expires_in of the token response, counted from the moment the token is exchanged. Falls back to the minimum session duration when the provider omits it (expires_in is only RECOMMENDED by RFC 6749).

Session lifetimes are now clamped between 1 hour and 7 days regardless of the source.

Why cap both sources

The cap is about bounding how long a revoked visitor keeps access, and that reasoning does not depend on which token you read — capping only the access-token path would leave the ID-token path able to mint a multi-month GitBook session from a long-lived exp claim.

This is technically a behaviour change for anyone whose IdP issues ID tokens with exp beyond 7 days. In practice ID token lifetimes are minutes to hours, so I would expect no installation to be affected, but flagging it rather than burying it.

Token response type

OIDCTokenResponseData now matches RFC 6749 §5.1 and OIDC Core §3.1.3.3:

Field
id_token REQUIRED by OIDC Core §3.1.3.3
access_token REQUIRED by RFC 6749 §4.1.4
token_type REQUIRED by RFC 6749 §4.1.4 — widened from the 'Bearer' literal, the spec makes it case-insensitive
expires_in RECOMMENDED, so optional
refresh_token OPTIONAL
scope OPTIONAL, REQUIRED when the granted scope differs from the requested one

The runtime guards stay in place — the response is untrusted input whatever the spec requires.

Tests

The expiry logic moved to src/session-expiry.ts so it can be unit tested without pulling in the integration entrypoint, matching the existing pkce.ts / cookie.ts / logout-hint.ts split. 14 tests cover source selection, the floor and the cap, the missing-expires_in fallback, string expires_in (some providers send it that way), and junk values.

This adds "test": "bun test" and "types": ["bun"] to the integration, copying the webhook integration's setup.

Note

tsc --noEmit reports a pre-existing FetchVisitorAuthenticationEvent.cookies error on src/index.tsx, unrelated to this branch — it reproduces on main and comes from a local bun.lock change that is not part of this PR.

Site admins can now choose whether the GitBook session token expiry is
derived from the ID token or the access token returned by the
authentication provider.

- ID token (default): the `exp` claim, as before, so existing
  installations are unaffected.
- Access token: the `expires_in` of the token response, counted from the
  moment the token is exchanged, falling back to the minimum session
  duration when the provider omits it.

Session lifetimes are clamped between 1 hour and 7 days regardless of the
source, so a long-lived upstream token can no longer mint a GitBook
session that outlives a revoked visitor's access by weeks.

`OIDCTokenResponseData` is updated to match RFC 6749 section 5.1 and OIDC
Core section 3.1.3.3: `id_token`, `access_token` and `token_type` are
required, `expires_in`, `refresh_token` and `scope` optional. The runtime
guards stay, since the response is untrusted input.

The expiry logic moves to `src/session-expiry.ts` so it can be unit
tested without pulling in the integration entrypoint.

Ref: RND-13568
Copilot AI balanced review requested due to automatic review settings October 29, 2026 14:01
@changeset-bot

changeset-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7ff0947

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@gitbook/integration-oidc Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@pkg-pr-new

pkg-pr-new Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

bun add https://pkg.pr.new/GitbookIO/integrations/@gitbook/api@1337

commit: 7ff0947

`compilerOptions.types` replaces the list inherited from
@gitbook/tsconfig/integration.json rather than extending it, so adding
`bun` for `bun:test` dropped `@cloudflare/workers-types` and with it the
`Response.json<T>()` generic the token exchange relies on.
@taranvohra

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-29T15:12:49.692205Z 7ff0947 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@taranvohra
taranvohra merged commit 8c4923b into main Oct 30, 2026
17 checks passed
@taranvohra
taranvohra deleted the taran/rnd-13568-add-oidc-token-preference-for-determining-session-expiry branch October 30, 2026 13:42
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.

3 participants