-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor: refactor the jwt code (#4322)
* ♻️ refactor: refactor the jwt code * clean * fix lint
- Loading branch information
Showing
11 changed files
with
113 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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,62 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
|
||
import { NON_HTTP_PREFIX } from '@/const/auth'; | ||
|
||
import { getJWTPayload } from './jwt'; | ||
|
||
let enableClerkMock = false; | ||
let enableNextAuthMock = false; | ||
|
||
vi.mock('@/const/auth', async (importOriginal) => { | ||
const data = await importOriginal(); | ||
|
||
return { | ||
...(data as any), | ||
get enableClerk() { | ||
return enableClerkMock; | ||
}, | ||
get enableNextAuth() { | ||
return enableNextAuthMock; | ||
}, | ||
}; | ||
}); | ||
|
||
vi.mock('@/config/app', () => ({ | ||
getAppConfig: vi.fn(), | ||
})); | ||
|
||
describe('getJWTPayload', () => { | ||
it('should parse JWT payload for non-HTTPS token', async () => { | ||
const token = `${NON_HTTP_PREFIX}.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ`; | ||
const payload = await getJWTPayload(token); | ||
expect(payload).toEqual({ | ||
sub: '1234567890', | ||
name: 'John Doe', | ||
iat: 1516239022, | ||
}); | ||
}); | ||
|
||
it('should verify and parse JWT payload for HTTPS token', async () => { | ||
const token = | ||
'eyJhbGciOiJIUzI1NiJ9.eyJhY2Nlc3NDb2RlIjoiIiwidXNlcklkIjoiMDAxMzYyYzMtNDhjNS00NjM1LWJkM2ItODM3YmZmZjU4ZmMwIiwiYXBpS2V5IjoiYWJjIiwiZW5kcG9pbnQiOiJhYmMiLCJpYXQiOjE3MTY4MDIyMjUsImV4cCI6MTAwMDAwMDAwMDE3MTY4MDIwMDB9.FF0FxsE8Cajs-_hv5GD0TNUDwvekAkI9l_LL_IOPdGQ'; | ||
const payload = await getJWTPayload(token); | ||
expect(payload).toEqual({ | ||
accessCode: '', | ||
apiKey: 'abc', | ||
endpoint: 'abc', | ||
exp: 10000000001716802000, | ||
iat: 1716802225, | ||
userId: '001362c3-48c5-4635-bd3b-837bfff58fc0', | ||
}); | ||
}); | ||
|
||
it('should not verify success and parse JWT payload for dated token', async () => { | ||
const token = | ||
'eyJhbGciOiJIUzI1NiJ9.eyJhY2Nlc3NDb2RlIjoiIiwidXNlcklkIjoiYWY3M2JhODktZjFhMy00YjliLWEwM2QtZGViZmZlMzE4NmQxIiwiYXBpS2V5IjoiYWJjIiwiZW5kcG9pbnQiOiJhYmMiLCJpYXQiOjE3MTY3OTk5ODAsImV4cCI6MTcxNjgwMDA4MH0.8AGFsLcwyrQG82kVUYOGFXHIwihm2n16ctyArKW9100'; | ||
try { | ||
await getJWTPayload(token); | ||
} catch (e) { | ||
expect(e).toEqual(new TypeError('"exp" claim timestamp check failed')); | ||
} | ||
}); | ||
}); |
This file contains 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,32 @@ | ||
import { importJWK, jwtVerify } from 'jose'; | ||
|
||
import { | ||
JWTPayload, | ||
JWT_SECRET_KEY, | ||
NON_HTTP_PREFIX, | ||
} from '@/const/auth'; | ||
|
||
export const getJWTPayload = async (token: string): Promise<JWTPayload> => { | ||
//如果是 HTTP 协议发起的请求,直接解析 token | ||
// 这是一个非常 hack 的解决方案,未来要找更好的解决方案来处理这个问题 | ||
// refs: https://github.com/lobehub/lobe-chat/pull/1238 | ||
if (token.startsWith(NON_HTTP_PREFIX)) { | ||
const jwtParts = token.split('.'); | ||
|
||
const payload = jwtParts[1]; | ||
|
||
return JSON.parse(atob(payload)); | ||
} | ||
|
||
const encoder = new TextEncoder(); | ||
const secretKey = await crypto.subtle.digest('SHA-256', encoder.encode(JWT_SECRET_KEY)); | ||
|
||
const jwkSecretKey = await importJWK( | ||
{ k: Buffer.from(secretKey).toString('base64'), kty: 'oct' }, | ||
'HS256', | ||
); | ||
|
||
const { payload } = await jwtVerify(token, jwkSecretKey); | ||
|
||
return payload as JWTPayload; | ||
}; |