@@ -15,13 +15,17 @@ async function getUserRoleFromAuth0(userId: string) : Promise<string> {
1515 return app_metadata . role ;
1616}
1717
18- async function verifyJwtCached ( authHeader : string , cache : Memcached ) {
19- if ( ! authHeader ) {
18+ function getCacheKey ( authHeader : string ) {
19+ return authHeader . split ( " " ) [ 1 ] . substr ( 0 , 100 ) ;
20+ }
21+
22+ async function verifyJwtCached ( cacheKey : string , cache : Memcached ) {
23+ if ( ! cacheKey ) {
2024 return null ;
2125 } else {
22- let cachedData = await cache . get ( authHeader ) ;
26+ let cachedData = await cache . get ( cacheKey ) ;
2327 if ( cachedData . code === ResponseCode . EXISTS && ! ! cachedData . data ) {
24- let headerMetadata = cachedData . data [ authHeader ] ;
28+ let headerMetadata = cachedData . data [ cacheKey ] ;
2529 if ( ! ! headerMetadata && ! ! headerMetadata . value ) {
2630 return JSON . parse ( headerMetadata . value . toString ( ) ) ;
2731 } else {
@@ -46,10 +50,11 @@ async function verifyJwt(request: MinimalRequest, userCache: Memcached, getUserI
4650 if ( ! authHeader ) {
4751 return { userAppId : null , role : null , admin : false }
4852 } else {
49- let userData = await verifyJwtCached ( authHeader , userCache ) ;
53+ let cacheKey = getCacheKey ( authHeader ) ;
54+ let userData = await verifyJwtCached ( cacheKey , userCache ) ;
5055 if ( ! userData ) {
5156 userData = await verifyJwtFromAuth0 ( authHeader , getUserInfo , getUserRole )
52- await userCache . add ( authHeader , userData , { expires : LIFETIME_SECONDS } ) ;
57+ await userCache . add ( cacheKey , userData , { expires : LIFETIME_SECONDS } ) ;
5358 }
5459 return userData ;
5560 }
0 commit comments