|
| 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 = 60; |
| 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 || !response.Item) { |
| 33 | + throw new DatabaseFetchError({ |
| 34 | + message: "Could not get user roles", |
| 35 | + }); |
| 36 | + } |
| 37 | + const items = unmarshall(response.Item) as { roles: AppRoles[] | ["all"] }; |
| 38 | + if (!("roles" in items)) { |
| 39 | + return []; |
| 40 | + } |
| 41 | + if (items["roles"][0] === "all") { |
| 42 | + fastifyApp.nodeCache.set( |
| 43 | + `userroles-${userId}`, |
| 44 | + allAppRoles, |
| 45 | + AUTH_DECISION_CACHE_SECONDS, |
| 46 | + ); |
| 47 | + return allAppRoles; |
| 48 | + } |
| 49 | + fastifyApp.nodeCache.set( |
| 50 | + `userroles-${userId}`, |
| 51 | + items["roles"], |
| 52 | + AUTH_DECISION_CACHE_SECONDS, |
| 53 | + ); |
| 54 | + return items["roles"] as AppRoles[]; |
| 55 | +} |
| 56 | + |
| 57 | +export async function getGroupRoles( |
| 58 | + dynamoClient: DynamoDBClient, |
| 59 | + fastifyApp: FastifyInstance, |
| 60 | + groupId: string, |
| 61 | +) { |
| 62 | + const cachedValue = fastifyApp.nodeCache.get(`grouproles-${groupId}`); |
| 63 | + if (cachedValue) { |
| 64 | + fastifyApp.log.info(`Returning cached auth decision for group ${groupId}`); |
| 65 | + return cachedValue as AppRoles[]; |
| 66 | + } |
| 67 | + const tableName = `${genericConfig["IAMTablePrefix"]}-grouproles`; |
| 68 | + const command = new GetItemCommand({ |
| 69 | + TableName: tableName, |
| 70 | + Key: { |
| 71 | + groupUuid: { S: groupId }, |
| 72 | + }, |
| 73 | + }); |
| 74 | + const response = await dynamoClient.send(command); |
| 75 | + if (!response || !response.Item) { |
| 76 | + throw new DatabaseFetchError({ |
| 77 | + message: "Could not get group roles for user", |
| 78 | + }); |
| 79 | + } |
| 80 | + const items = unmarshall(response.Item) as { roles: AppRoles[] | ["all"] }; |
| 81 | + if (!("roles" in items)) { |
| 82 | + fastifyApp.nodeCache.set( |
| 83 | + `grouproles-${groupId}`, |
| 84 | + [], |
| 85 | + AUTH_DECISION_CACHE_SECONDS, |
| 86 | + ); |
| 87 | + return []; |
| 88 | + } |
| 89 | + if (items["roles"][0] === "all") { |
| 90 | + fastifyApp.nodeCache.set( |
| 91 | + `grouproles-${groupId}`, |
| 92 | + allAppRoles, |
| 93 | + AUTH_DECISION_CACHE_SECONDS, |
| 94 | + ); |
| 95 | + return allAppRoles; |
| 96 | + } |
| 97 | + fastifyApp.nodeCache.set( |
| 98 | + `grouproles-${groupId}`, |
| 99 | + items["roles"], |
| 100 | + AUTH_DECISION_CACHE_SECONDS, |
| 101 | + ); |
| 102 | + return items["roles"] as AppRoles[]; |
| 103 | +} |
0 commit comments