|
1 |
| -import { FastifyPluginAsync } from "fastify"; |
| 1 | +import { FastifyInstance, FastifyPluginAsync } from "fastify"; |
2 | 2 | import { allAppRoles, AppRoles } from "../../common/roles.js";
|
3 | 3 | import { zodToJsonSchema } from "zod-to-json-schema";
|
4 | 4 | import {
|
@@ -38,48 +38,42 @@ import {
|
38 | 38 | AUTH_DECISION_CACHE_SECONDS,
|
39 | 39 | getGroupRoles,
|
40 | 40 | } from "../functions/authorization.js";
|
| 41 | +import { OrganizationList } from "common/orgs.js"; |
| 42 | +import { z } from "zod"; |
| 43 | + |
| 44 | +const OrganizationListEnum = z.enum(OrganizationList as [string, ...string[]]); |
| 45 | +export type Org = z.infer<typeof OrganizationListEnum>; |
| 46 | + |
| 47 | +type Member = { name: string; email: string }; |
| 48 | +type OrgMembersResponse = { org: Org; members: Member[] }; |
| 49 | + |
| 50 | +// const groupMappings = getRunEnvironmentConfig().KnownGroupMappings; |
| 51 | +// const groupOptions = Object.entries(groupMappings).map(([key, value]) => ({ |
| 52 | +// label: userGroupMappings[key as keyof KnownGroups] || key, |
| 53 | +// value: `${key}_${value}`, // to ensure that the same group for multiple roles still renders |
| 54 | +// })); |
41 | 55 |
|
42 | 56 | const sigleadRoutes: FastifyPluginAsync = async (fastify, _options) => {
|
43 | 57 | fastify.get<{
|
44 |
| - Querystring: { groupId: string }; |
45 |
| - }>( |
46 |
| - "/groups/:groupId/roles", |
47 |
| - { |
48 |
| - schema: { |
49 |
| - querystring: { |
50 |
| - type: "object", |
51 |
| - properties: { |
52 |
| - groupId: { |
53 |
| - type: "string", |
54 |
| - }, |
55 |
| - }, |
56 |
| - }, |
| 58 | + Reply: OrgMembersResponse[]; |
| 59 | + }>("/groups", async (request, reply) => { |
| 60 | + const entraIdToken = await getEntraIdToken( |
| 61 | + { |
| 62 | + smClient: fastify.secretsManagerClient, |
| 63 | + dynamoClient: fastify.dynamoClient, |
57 | 64 | },
|
58 |
| - onRequest: async (request, reply) => { |
59 |
| - await fastify.authorize(request, reply, [AppRoles.IAM_ADMIN]); |
60 |
| - }, |
61 |
| - }, |
62 |
| - async (request, reply) => { |
63 |
| - try { |
64 |
| - const groupId = (request.params as Record<string, string>).groupId; |
65 |
| - const roles = await getGroupRoles( |
66 |
| - fastify.dynamoClient, |
67 |
| - fastify, |
68 |
| - groupId, |
69 |
| - ); |
70 |
| - return reply.send(roles); |
71 |
| - } catch (e: unknown) { |
72 |
| - if (e instanceof BaseError) { |
73 |
| - throw e; |
74 |
| - } |
| 65 | + fastify.environmentConfig.AadValidClientId, |
| 66 | + ); |
| 67 | + |
| 68 | + const data = await Promise.all( |
| 69 | + OrganizationList.map(async (org) => { |
| 70 | + const members: Member[] = await listGroupMembers(entraIdToken, org); |
| 71 | + return { org, members } as OrgMembersResponse; |
| 72 | + }), |
| 73 | + ); |
75 | 74 |
|
76 |
| - request.log.error(e); |
77 |
| - throw new DatabaseFetchError({ |
78 |
| - message: "An error occurred finding the group role mapping.", |
79 |
| - }); |
80 |
| - } |
81 |
| - }, |
82 |
| - ); |
| 75 | + reply.status(200).send(data); |
| 76 | + }); |
83 | 77 |
|
84 | 78 | // fastify.patch<{ Body: ProfilePatchRequest }>(
|
85 | 79 | // "/profile",
|
|
0 commit comments