-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add JWT verification #875
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1c9ee63
feat(wip): add correct jwt verification
CasLubbers fb7b907
feat: add jwt verification
CasLubbers a149035
feat: add jwt verification
CasLubbers 51c1319
fix: tests
CasLubbers 2e81ab9
fix: add retry for waiting on tools server
CasLubbers f176c87
feat: add rate-limiter
CasLubbers 228b49d
fix: set high rate limits
CasLubbers 7c92bec
Merge remote-tracking branch 'origin/main' into APL-1372
svcAPLBot 3afc871
Merge remote-tracking branch 'origin/main' into APL-1372
svcAPLBot 7e0a00f
Merge remote-tracking branch 'origin/main' into APL-1372
svcAPLBot 11b44c2
Merge remote-tracking branch 'origin/main' into APL-1372
svcAPLBot e68dcc2
Update src/jwt-verification.ts
CasLubbers ea98155
fix: eslint error
CasLubbers 8da0af1
fix: only wait for JWKS if not dev or test environment
CasLubbers 055752c
fix: add clockTolerance
CasLubbers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { createRemoteJWKSet, JWTPayload, jwtVerify } from 'jose' | ||
| import Debug from 'debug' | ||
| import retry from 'async-retry' | ||
| import { | ||
| cleanEnv, | ||
| JWT_AUDIENCE, | ||
| SSO_ISSUER, | ||
| SSO_JWKS_URI, | ||
| STARTUP_RETRY_COUNT, | ||
| STARTUP_RETRY_INTERVAL_MS, | ||
| } from 'src/validators' | ||
|
|
||
| const debug = Debug('otomi:jwt') | ||
| const env = cleanEnv({ SSO_ISSUER, JWT_AUDIENCE, SSO_JWKS_URI, STARTUP_RETRY_COUNT, STARTUP_RETRY_INTERVAL_MS }) | ||
| const JWKS_URL = env.SSO_JWKS_URI | ||
|
|
||
| // Create remote JWKS - automatically caches and refreshes keys | ||
| const JWKS = createRemoteJWKSet(new URL(JWKS_URL)) | ||
|
|
||
| export interface AppJWTPayload extends JWTPayload { | ||
| name: string | ||
| email: string | ||
| sub: string | ||
| groups: string[] | ||
| roles: string[] | ||
| } | ||
|
|
||
| export async function verifyJwt(token: string): Promise<AppJWTPayload> { | ||
| // Remove "Bearer " prefix if present | ||
| const bearerToken = token.replace(/^Bearer\s+/i, '') | ||
|
|
||
| try { | ||
| const { payload } = await jwtVerify(bearerToken, JWKS, { | ||
| issuer: env.SSO_ISSUER, | ||
| audience: env.JWT_AUDIENCE, | ||
| }) | ||
| if (!payload.email || !payload.name || !payload.sub) { | ||
| throw new Error('JWT missing required claims') | ||
| } | ||
| debug('JWT verified successfully:', payload.sub) | ||
|
|
||
| return { | ||
| payload, | ||
| email: payload.email as string, | ||
| name: payload.name as string, | ||
| sub: payload.sub, | ||
| groups: (payload.groups as string[]) || [], | ||
| roles: (payload.roles as string[]) || [], | ||
| } | ||
| } catch (error: any) { | ||
| debug('JWT verification failed:', error.message) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export async function waitForJwksReady(): Promise<void> { | ||
| debug('Waiting for JWKS endpoint to become ready...') | ||
|
|
||
| await retry( | ||
| async () => { | ||
| try { | ||
| // Try a dummy verification with an invalid token | ||
| // We only care that JWKS fetch succeeds, not that the token is valid | ||
| const invalidJWT = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.invalid_signature' | ||
| await verifyJwt(invalidJWT) | ||
| } catch (err: any) { | ||
| // If we get a JWT verification error, JWKS successfully fetched and processed the token | ||
| // This means JWKS is working! (Even though token was rejected - that's expected) | ||
| const isJwtVerificationError = | ||
| err?.code?.startsWith('ERR_JW') || // jose library error codes (ERR_JWS_INVALID, ERR_JWT_INVALID, etc.) | ||
| err?.message?.includes('signature') || | ||
| err?.message?.includes('invalid') || | ||
| err?.message?.includes('expired') || | ||
| err?.message?.includes('claim') | ||
|
|
||
| if (isJwtVerificationError) { | ||
| debug('JWKS endpoint reachable (token verification worked) — continuing startup.') | ||
| return | ||
| } | ||
|
|
||
| debug(`JWKS not ready yet: ${err.message}, retrying...`) | ||
| throw err | ||
| } | ||
| }, | ||
| { | ||
| retries: env.STARTUP_RETRY_COUNT, | ||
| minTimeout: env.STARTUP_RETRY_INTERVAL_MS, | ||
| maxTimeout: env.STARTUP_RETRY_INTERVAL_MS, | ||
| }, | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.