Skip to content

Commit

Permalink
update auth update endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
devksingh4 committed Jan 22, 2025
1 parent b2a001f commit c4a0c83
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/api/functions/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DatabaseFetchError } from "../../common/errors/index.js";
import { allAppRoles, AppRoles } from "../../common/roles.js";
import { FastifyInstance } from "fastify";

export const AUTH_DECISION_CACHE_SECONDS = 60;
export const AUTH_DECISION_CACHE_SECONDS = 180;

export async function getUserRoles(
dynamoClient: DynamoDBClient,
Expand Down Expand Up @@ -72,11 +72,19 @@ export async function getGroupRoles(
},
});
const response = await dynamoClient.send(command);
if (!response || !response.Item) {
if (!response) {
throw new DatabaseFetchError({
message: "Could not get group roles for user",
});
}
if (!response.Item) {
fastifyApp.nodeCache.set(
`grouproles-${groupId}`,
[],
AUTH_DECISION_CACHE_SECONDS,
);
return [];
}
const items = unmarshall(response.Item) as { roles: AppRoles[] | ["all"] };
if (!("roles" in items)) {
fastifyApp.nodeCache.set(
Expand Down
28 changes: 14 additions & 14 deletions src/api/routes/iam.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FastifyPluginAsync } from "fastify";
import { AppRoles } from "../../common/roles.js";
import { allAppRoles, AppRoles } from "../../common/roles.js";
import { zodToJsonSchema } from "zod-to-json-schema";
import {
addToTenant,
Expand Down Expand Up @@ -34,6 +34,10 @@ import {
EntraGroupActions,
entraGroupMembershipListResponse,
} from "../../common/types/iam.js";
import {
AUTH_DECISION_CACHE_SECONDS,
getGroupRoles,
} from "api/functions/authorization.js";

const dynamoClient = new DynamoDBClient({
region: genericConfig.AwsRegion,
Expand Down Expand Up @@ -61,19 +65,10 @@ const iamRoutes: FastifyPluginAsync = async (fastify, _options) => {
},
},
async (request, reply) => {
const groupId = (request.params as Record<string, string>).groupId;
try {
const command = new GetItemCommand({
TableName: `${genericConfig.IAMTablePrefix}-grouproles`,
Key: { groupUuid: { S: groupId } },
});
const response = await dynamoClient.send(command);
if (!response.Item) {
throw new NotFoundError({
endpointName: `/api/v1/iam/groupRoles/${groupId}`,
});
}
reply.send(unmarshall(response.Item));
const groupId = (request.params as Record<string, string>).groupId;
const roles = await getGroupRoles(dynamoClient, fastify, groupId);
return reply.send(roles);
} catch (e: unknown) {
if (e instanceof BaseError) {
throw e;
Expand Down Expand Up @@ -125,9 +120,14 @@ const iamRoutes: FastifyPluginAsync = async (fastify, _options) => {
createdAt: timestamp,
}),
});

await dynamoClient.send(command);
fastify.nodeCache.set(
`grouproles-${groupId}`,
request.body.roles,
AUTH_DECISION_CACHE_SECONDS,
);
} catch (e: unknown) {
fastify.nodeCache.del(`grouproles-${groupId}`);
if (e instanceof BaseError) {
throw e;
}
Expand Down

0 comments on commit c4a0c83

Please sign in to comment.