Skip to content

Commit c4a0c83

Browse files
committed
update auth update endpoints
1 parent b2a001f commit c4a0c83

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/api/functions/authorization.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { DatabaseFetchError } from "../../common/errors/index.js";
99
import { allAppRoles, AppRoles } from "../../common/roles.js";
1010
import { FastifyInstance } from "fastify";
1111

12-
export const AUTH_DECISION_CACHE_SECONDS = 60;
12+
export const AUTH_DECISION_CACHE_SECONDS = 180;
1313

1414
export async function getUserRoles(
1515
dynamoClient: DynamoDBClient,
@@ -72,11 +72,19 @@ export async function getGroupRoles(
7272
},
7373
});
7474
const response = await dynamoClient.send(command);
75-
if (!response || !response.Item) {
75+
if (!response) {
7676
throw new DatabaseFetchError({
7777
message: "Could not get group roles for user",
7878
});
7979
}
80+
if (!response.Item) {
81+
fastifyApp.nodeCache.set(
82+
`grouproles-${groupId}`,
83+
[],
84+
AUTH_DECISION_CACHE_SECONDS,
85+
);
86+
return [];
87+
}
8088
const items = unmarshall(response.Item) as { roles: AppRoles[] | ["all"] };
8189
if (!("roles" in items)) {
8290
fastifyApp.nodeCache.set(

src/api/routes/iam.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FastifyPluginAsync } from "fastify";
2-
import { AppRoles } from "../../common/roles.js";
2+
import { allAppRoles, AppRoles } from "../../common/roles.js";
33
import { zodToJsonSchema } from "zod-to-json-schema";
44
import {
55
addToTenant,
@@ -34,6 +34,10 @@ import {
3434
EntraGroupActions,
3535
entraGroupMembershipListResponse,
3636
} from "../../common/types/iam.js";
37+
import {
38+
AUTH_DECISION_CACHE_SECONDS,
39+
getGroupRoles,
40+
} from "api/functions/authorization.js";
3741

3842
const dynamoClient = new DynamoDBClient({
3943
region: genericConfig.AwsRegion,
@@ -61,19 +65,10 @@ const iamRoutes: FastifyPluginAsync = async (fastify, _options) => {
6165
},
6266
},
6367
async (request, reply) => {
64-
const groupId = (request.params as Record<string, string>).groupId;
6568
try {
66-
const command = new GetItemCommand({
67-
TableName: `${genericConfig.IAMTablePrefix}-grouproles`,
68-
Key: { groupUuid: { S: groupId } },
69-
});
70-
const response = await dynamoClient.send(command);
71-
if (!response.Item) {
72-
throw new NotFoundError({
73-
endpointName: `/api/v1/iam/groupRoles/${groupId}`,
74-
});
75-
}
76-
reply.send(unmarshall(response.Item));
69+
const groupId = (request.params as Record<string, string>).groupId;
70+
const roles = await getGroupRoles(dynamoClient, fastify, groupId);
71+
return reply.send(roles);
7772
} catch (e: unknown) {
7873
if (e instanceof BaseError) {
7974
throw e;
@@ -125,9 +120,14 @@ const iamRoutes: FastifyPluginAsync = async (fastify, _options) => {
125120
createdAt: timestamp,
126121
}),
127122
});
128-
129123
await dynamoClient.send(command);
124+
fastify.nodeCache.set(
125+
`grouproles-${groupId}`,
126+
request.body.roles,
127+
AUTH_DECISION_CACHE_SECONDS,
128+
);
130129
} catch (e: unknown) {
130+
fastify.nodeCache.del(`grouproles-${groupId}`);
131131
if (e instanceof BaseError) {
132132
throw e;
133133
}

0 commit comments

Comments
 (0)