| 
 | 1 | +import {  | 
 | 2 | +  DynamoDBClient,  | 
 | 3 | +  GetItemCommand,  | 
 | 4 | +  QueryCommand,  | 
 | 5 | +} from "@aws-sdk/client-dynamodb";  | 
 | 6 | +import { unmarshall } from "@aws-sdk/util-dynamodb";  | 
 | 7 | +import { genericConfig } from "../../common/config.js";  | 
 | 8 | +import { DatabaseFetchError } from "../../common/errors/index.js";  | 
 | 9 | +import { allAppRoles, AppRoles } from "../../common/roles.js";  | 
 | 10 | +import { FastifyInstance } from "fastify";  | 
 | 11 | + | 
 | 12 | +export const AUTH_DECISION_CACHE_SECONDS = 180;  | 
 | 13 | + | 
 | 14 | +export async function getUserRoles(  | 
 | 15 | +  dynamoClient: DynamoDBClient,  | 
 | 16 | +  fastifyApp: FastifyInstance,  | 
 | 17 | +  userId: string,  | 
 | 18 | +): Promise<AppRoles[]> {  | 
 | 19 | +  const cachedValue = fastifyApp.nodeCache.get(`userroles-${userId}`);  | 
 | 20 | +  if (cachedValue) {  | 
 | 21 | +    fastifyApp.log.info(`Returning cached auth decision for user ${userId}`);  | 
 | 22 | +    return cachedValue as AppRoles[];  | 
 | 23 | +  }  | 
 | 24 | +  const tableName = `${genericConfig["IAMTablePrefix"]}-userroles`;  | 
 | 25 | +  const command = new GetItemCommand({  | 
 | 26 | +    TableName: tableName,  | 
 | 27 | +    Key: {  | 
 | 28 | +      userEmail: { S: userId },  | 
 | 29 | +    },  | 
 | 30 | +  });  | 
 | 31 | +  const response = await dynamoClient.send(command);  | 
 | 32 | +  if (!response) {  | 
 | 33 | +    throw new DatabaseFetchError({  | 
 | 34 | +      message: "Could not get user roles",  | 
 | 35 | +    });  | 
 | 36 | +  }  | 
 | 37 | +  if (!response.Item) {  | 
 | 38 | +    return [];  | 
 | 39 | +  }  | 
 | 40 | +  const items = unmarshall(response.Item) as { roles: AppRoles[] | ["all"] };  | 
 | 41 | +  if (!("roles" in items)) {  | 
 | 42 | +    return [];  | 
 | 43 | +  }  | 
 | 44 | +  if (items["roles"][0] === "all") {  | 
 | 45 | +    fastifyApp.nodeCache.set(  | 
 | 46 | +      `userroles-${userId}`,  | 
 | 47 | +      allAppRoles,  | 
 | 48 | +      AUTH_DECISION_CACHE_SECONDS,  | 
 | 49 | +    );  | 
 | 50 | +    return allAppRoles;  | 
 | 51 | +  }  | 
 | 52 | +  fastifyApp.nodeCache.set(  | 
 | 53 | +    `userroles-${userId}`,  | 
 | 54 | +    items["roles"],  | 
 | 55 | +    AUTH_DECISION_CACHE_SECONDS,  | 
 | 56 | +  );  | 
 | 57 | +  return items["roles"] as AppRoles[];  | 
 | 58 | +}  | 
 | 59 | + | 
 | 60 | +export async function getGroupRoles(  | 
 | 61 | +  dynamoClient: DynamoDBClient,  | 
 | 62 | +  fastifyApp: FastifyInstance,  | 
 | 63 | +  groupId: string,  | 
 | 64 | +) {  | 
 | 65 | +  const cachedValue = fastifyApp.nodeCache.get(`grouproles-${groupId}`);  | 
 | 66 | +  if (cachedValue) {  | 
 | 67 | +    fastifyApp.log.info(`Returning cached auth decision for group ${groupId}`);  | 
 | 68 | +    return cachedValue as AppRoles[];  | 
 | 69 | +  }  | 
 | 70 | +  const tableName = `${genericConfig["IAMTablePrefix"]}-grouproles`;  | 
 | 71 | +  const command = new GetItemCommand({  | 
 | 72 | +    TableName: tableName,  | 
 | 73 | +    Key: {  | 
 | 74 | +      groupUuid: { S: groupId },  | 
 | 75 | +    },  | 
 | 76 | +  });  | 
 | 77 | +  const response = await dynamoClient.send(command);  | 
 | 78 | +  if (!response) {  | 
 | 79 | +    throw new DatabaseFetchError({  | 
 | 80 | +      message: "Could not get group roles for user",  | 
 | 81 | +    });  | 
 | 82 | +  }  | 
 | 83 | +  if (!response.Item) {  | 
 | 84 | +    fastifyApp.nodeCache.set(  | 
 | 85 | +      `grouproles-${groupId}`,  | 
 | 86 | +      [],  | 
 | 87 | +      AUTH_DECISION_CACHE_SECONDS,  | 
 | 88 | +    );  | 
 | 89 | +    return [];  | 
 | 90 | +  }  | 
 | 91 | +  const items = unmarshall(response.Item) as { roles: AppRoles[] | ["all"] };  | 
 | 92 | +  if (!("roles" in items)) {  | 
 | 93 | +    fastifyApp.nodeCache.set(  | 
 | 94 | +      `grouproles-${groupId}`,  | 
 | 95 | +      [],  | 
 | 96 | +      AUTH_DECISION_CACHE_SECONDS,  | 
 | 97 | +    );  | 
 | 98 | +    return [];  | 
 | 99 | +  }  | 
 | 100 | +  if (items["roles"][0] === "all") {  | 
 | 101 | +    fastifyApp.nodeCache.set(  | 
 | 102 | +      `grouproles-${groupId}`,  | 
 | 103 | +      allAppRoles,  | 
 | 104 | +      AUTH_DECISION_CACHE_SECONDS,  | 
 | 105 | +    );  | 
 | 106 | +    return allAppRoles;  | 
 | 107 | +  }  | 
 | 108 | +  fastifyApp.nodeCache.set(  | 
 | 109 | +    `grouproles-${groupId}`,  | 
 | 110 | +    items["roles"],  | 
 | 111 | +    AUTH_DECISION_CACHE_SECONDS,  | 
 | 112 | +  );  | 
 | 113 | +  return items["roles"] as AppRoles[];  | 
 | 114 | +}  | 
0 commit comments