-
Notifications
You must be signed in to change notification settings - Fork 4
[ENG-2416] feat: add jwt provider session storage part 1 #1219
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
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,213 @@ | ||||||||||||||
| import { | ||||||||||||||
| createContext, | ||||||||||||||
| useCallback, | ||||||||||||||
| useContext, | ||||||||||||||
| useEffect, | ||||||||||||||
| useState, | ||||||||||||||
| } from "react"; | ||||||||||||||
| import { jwtVerify } from "jose"; | ||||||||||||||
|
|
||||||||||||||
| interface TokenCacheEntry { | ||||||||||||||
| token: string; | ||||||||||||||
| expiresAt: number; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const SESSION_STORAGE_PREFIX = "amp-labs_jwt_"; | ||||||||||||||
|
|
||||||||||||||
| const DEFAULT_TOKEN_EXPIRATION_TIME = 10 * 60 * 1000; // 10 minutes | ||||||||||||||
|
|
||||||||||||||
| const createCacheKey = (consumerRef: string, groupRef: string) => | ||||||||||||||
| `${consumerRef}:${groupRef}`; | ||||||||||||||
|
|
||||||||||||||
| const getSessionStorageKey = (cacheKey: string) => | ||||||||||||||
| `${SESSION_STORAGE_PREFIX}${cacheKey}`; | ||||||||||||||
|
|
||||||||||||||
| interface JwtTokenContextValue { | ||||||||||||||
| getToken?: (consumerRef: string, groupRef: string) => Promise<string>; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const JwtTokenContext = createContext<JwtTokenContextValue | null>(null); | ||||||||||||||
|
|
||||||||||||||
| interface JwtTokenProviderProps { | ||||||||||||||
| getTokenCallback: | ||||||||||||||
| | ((consumerRef: string, groupRef: string) => Promise<string>) | ||||||||||||||
| | null; | ||||||||||||||
| children: React.ReactNode; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Extract JWT token expiration time | ||||||||||||||
| */ | ||||||||||||||
| const getTokenExpirationTime = async ( | ||||||||||||||
| token: string, | ||||||||||||||
| ): Promise<number | null> => { | ||||||||||||||
| try { | ||||||||||||||
| const decoded = await jwtVerify(token, new Uint8Array(0), { | ||||||||||||||
| algorithms: [], // Skip signature verification | ||||||||||||||
| }); | ||||||||||||||
| const payload = decoded.payload; | ||||||||||||||
|
Comment on lines
+45
to
+48
|
||||||||||||||
| const decoded = await jwtVerify(token, new Uint8Array(0), { | |
| algorithms: [], // Skip signature verification | |
| }); | |
| const payload = decoded.payload; | |
| const payload = decodeJwt(token); |
Copilot
AI
Jul 31, 2025
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 tokenCache.delete() operation inside the else block modifies the state directly rather than using setTokenCache, which could lead to inconsistent state and React rendering issues.
| tokenCache.delete(cacheKey); | |
| setTokenCache((prev) => { | |
| const newCache = new Map(prev); | |
| newCache.delete(cacheKey); | |
| return newCache; | |
| }); |
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.
Using an empty Uint8Array and empty algorithms array completely bypasses JWT signature verification, which is a serious security vulnerability. This allows any malformed or malicious JWT to be accepted as valid.