@@ -13,13 +13,14 @@ import {
1313 UnauthenticatedError ,
1414 UnauthorizedError ,
1515} from "../../common/errors/index.js" ;
16- import { SecretConfig , SecretTesting } from "../../common/config.js" ;
1716import {
18- AUTH_DECISION_CACHE_SECONDS ,
19- getGroupRoles ,
20- getUserRoles ,
21- } from "../functions/authorization.js" ;
17+ SecretConfig ,
18+ SecretTesting ,
19+ GENERIC_CACHE_SECONDS ,
20+ } from "../../common/config.js" ;
21+ import { getGroupRoles , getUserRoles } from "../functions/authorization.js" ;
2222import { getApiKeyData , getApiKeyParts } from "api/functions/apiKey.js" ;
23+ import { getKey , setKey } from "api/functions/redisCache.js" ;
2324
2425export function intersection < T > ( setA : Set < T > , setB : Set < T > ) : Set < T > {
2526 const _intersection = new Set < T > ( ) ;
@@ -155,6 +156,8 @@ const authPlugin: FastifyPluginAsync = async (fastify, _options) => {
155156 validRoles : AppRoles [ ] ,
156157 disableApiKeyAuth : boolean ,
157158 ) : Promise < Set < AppRoles > > => {
159+ const { redisClient } = fastify ;
160+ const encryptionSecret = fastify . secretConfig . encryption_key ;
158161 const startTime = new Date ( ) . getTime ( ) ;
159162 try {
160163 if ( ! disableApiKeyAuth ) {
@@ -225,11 +228,13 @@ const authPlugin: FastifyPluginAsync = async (fastify, _options) => {
225228 header : decoded ?. header ,
226229 audience : `api://${ AadClientId } ` ,
227230 } ;
228- const cachedJwksSigningKey = await fastify . redisClient . get (
229- `jwksKey:${ header . kid } ` ,
230- ) ;
231+ const { redisClient } = fastify ;
232+ const cachedJwksSigningKey = await getKey < { key : string } > ( {
233+ redisClient,
234+ key : `jwksKey:${ header . kid } ` ,
235+ } ) ;
231236 if ( cachedJwksSigningKey ) {
232- signingKey = cachedJwksSigningKey ;
237+ signingKey = cachedJwksSigningKey . key ;
233238 request . log . debug ( "Got JWKS signing key from cache." ) ;
234239 } else {
235240 const client = jwksClient ( {
@@ -239,12 +244,12 @@ const authPlugin: FastifyPluginAsync = async (fastify, _options) => {
239244 signingKey = (
240245 await client . getSigningKey ( header . kid )
241246 ) . getPublicKey ( ) ;
242- await fastify . redisClient . set (
243- `jwksKey: ${ header . kid } ` ,
244- signingKey ,
245- "EX" ,
246- JWKS_CACHE_SECONDS ,
247- ) ;
247+ await setKey ( {
248+ redisClient ,
249+ key : `jwksKey: ${ header . kid } ` ,
250+ data : JSON . stringify ( { key : signingKey } ) ,
251+ expiresIn : JWKS_CACHE_SECONDS ,
252+ } ) ;
248253 request . log . debug ( "Got JWKS signing key from server." ) ;
249254 }
250255 }
@@ -263,11 +268,12 @@ const authPlugin: FastifyPluginAsync = async (fastify, _options) => {
263268 verifiedTokenData . upn ?. replace ( "acm.illinois.edu" , "illinois.edu" ) ||
264269 verifiedTokenData . sub ;
265270 const expectedRoles = new Set ( validRoles ) ;
266- const cachedRoles = await fastify . redisClient . get (
267- `authCache:${ request . username } :roles` ,
268- ) ;
271+ const cachedRoles = await getKey < string [ ] > ( {
272+ key : `authCache:${ request . username } :roles` ,
273+ redisClient,
274+ } ) ;
269275 if ( cachedRoles ) {
270- request . userRoles = new Set ( JSON . parse ( cachedRoles ) ) ;
276+ request . userRoles = new Set ( cachedRoles as AppRoles [ ] ) ;
271277 request . log . debug ( "Retrieved user roles from cache." ) ;
272278 } else {
273279 const userRoles = new Set ( [ ] as AppRoles [ ] ) ;
@@ -317,12 +323,12 @@ const authPlugin: FastifyPluginAsync = async (fastify, _options) => {
317323 }
318324 }
319325 request . userRoles = userRoles ;
320- fastify . redisClient . set (
321- `authCache:${ request . username } :roles` ,
322- JSON . stringify ( [ ...userRoles ] ) ,
323- "EX" ,
324- AUTH_DECISION_CACHE_SECONDS ,
325- ) ;
326+ setKey ( {
327+ key : `authCache:${ request . username } :roles` ,
328+ data : JSON . stringify ( [ ...userRoles ] ) ,
329+ redisClient ,
330+ expiresIn : GENERIC_CACHE_SECONDS ,
331+ } ) ;
326332 request . log . debug ( "Retrieved user roles from database." ) ;
327333 }
328334 if (
0 commit comments