Repository navigation
feat(user-management): Add verifyAccessToken #1733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { UserManagementAccessToken } from './authenticate-with-session-cookie.interface'; | ||
|
|
||
| /** | ||
| * The claims of a verified WorkOS access token, as returned by | ||
| * `userManagement.verifyAccessToken()`. | ||
| * | ||
| * Combines the registered JWT claims WorkOS sets with the session claims in | ||
| * {@link UserManagementAccessToken}. Any other claims present on the token, | ||
| * such as custom claims configured for the environment, are available through | ||
| * the index signature. | ||
| */ | ||
| export interface AccessTokenClaims extends UserManagementAccessToken { | ||
| /** The ID of the user the token was issued to. */ | ||
| sub: string; | ||
| /** The issuer of the token. */ | ||
| iss: string; | ||
| /** When the token expires, in seconds since the Unix epoch. */ | ||
| exp: number; | ||
| /** When the token was issued, in seconds since the Unix epoch. */ | ||
| iat: number; | ||
| /** When the user last actively authenticated, in seconds since the Unix epoch. */ | ||
| auth_time?: number; | ||
| [claim: string]: unknown; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -192,6 +192,7 @@ import { serializeCreateWaitlistEntryOptions } from './serializers/create-waitli | |
| import { serializeListWaitlistEntriesOptions } from './serializers/list-waitlist-entries-options.serializer'; | ||
| import { CookieSession } from './session'; | ||
| import { getJose } from '../utils/jose'; | ||
| import { AccessTokenClaims } from './interfaces/access-token-claims.interface'; | ||
| import { Group, GroupResponse } from '../groups/interfaces'; | ||
| import { deserializeGroup } from '../groups/serializers'; | ||
| import { encodePathParameter } from '../common/utils/encode-path-parameter'; | ||
|
|
@@ -744,20 +745,35 @@ export class UserManagement { | |
| }; | ||
| } | ||
|
|
||
| private async isValidJwt(accessToken: string): Promise<boolean> { | ||
| /** | ||
| * Verify an access token and return its claims. | ||
| * | ||
| * Checks the token's signature against the environment's JWKS and its | ||
| * expiry, plus the issuer when one was configured on the client. Resolves to | ||
| * `null` when the token fails any of those checks or is not a JWT. Errors | ||
| * that are not JWT validation failures, such as a network error while | ||
| * fetching the JWKS, propagate. | ||
| * | ||
| * @param accessToken - The access token to verify, for example from an `Authorization: Bearer` header. | ||
| * @returns {Promise<AccessTokenClaims | null>} The token's claims, or `null` when the token is invalid. | ||
| * @throws {Error} When no client ID was provided when initializing WorkOS. | ||
| */ | ||
| async verifyAccessToken( | ||
| accessToken: string, | ||
| ): Promise<AccessTokenClaims | null> { | ||
| const jwks = await this.getJWKS(); | ||
| const { jwtVerify } = await getJose(); | ||
| if (!jwks) { | ||
| throw new Error('Must provide clientId to initialize JWKS'); | ||
| } | ||
|
|
||
| try { | ||
| await jwtVerify( | ||
| const { payload } = await jwtVerify<AccessTokenClaims>( | ||
| accessToken, | ||
| jwks, | ||
| this.issuer ? { issuer: this.issuer } : undefined, | ||
| ); | ||
| return true; | ||
| return payload; | ||
|
Comment on lines
+771
to
+776
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Add a required-claims check before returning, as Prompt To Fix With AIThis is a comment left during a code review.
Path: src/user-management/user-management.ts
Line: 771-776
Comment:
**Required claims go unchecked**
`verifyAccessToken` returns the payload as `AccessTokenClaims` without checking that `sub`, `sid`, `iss`, `exp`, and `iat` exist. The type argument does not check these fields at runtime. If a signed token for another purpose lacks them, callers receive missing values where TypeScript promises values.
Add a required-claims check before returning, as `hasRequiredAgentClaims` already does for agent tokens.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||
| } catch (e) { | ||
| // Only treat as invalid JWT if it's an actual JWT/JWS error from jose | ||
| // Network errors, crypto failures, etc. should propagate | ||
|
|
@@ -767,12 +783,16 @@ export class UserManagement { | |
| typeof e.code === 'string' && | ||
| (e.code.startsWith('ERR_JWT_') || e.code.startsWith('ERR_JWS_')) | ||
| ) { | ||
| return false; | ||
| return null; | ||
| } | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| private async isValidJwt(accessToken: string): Promise<boolean> { | ||
| return (await this.verifyAccessToken(accessToken)) !== null; | ||
| } | ||
|
|
||
| private async prepareAuthenticationResponse({ | ||
| authenticationResponse, | ||
| session, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example continues to
claims.subafterverifyAccessTokenreturnsnull. The comment insideif (!claims)does not stop execution, so the pasted example fails TypeScript's null check and throws for an invalid token. Use athroworreturnin that branch.Prompt To Fix With AI