Skip to content

Commit ca2a6b9

Browse files
committed
support checking roles
1 parent 39d0d57 commit ca2a6b9

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/plugins/auth.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ import {
1515
} from "../errors/index.js";
1616

1717
const CONFIG_SECRET_NAME = "infra-events-api-config" as const;
18+
const AzureRoleMapping: Record<RunEnvironment, Record<string, AppRoles[]>> = {
19+
prod: {
20+
"AutonomousWriters": [AppRoles.MANAGER]
21+
},
22+
dev: {
23+
"AutonomousWriters": [AppRoles.MANAGER]
24+
},
25+
}
26+
1827
const GroupRoleMapping: Record<RunEnvironment, Record<string, AppRoles[]>> = {
1928
prod: {
2029
"48591dbc-cdcb-4544-9f63-e6b92b067e33": [AppRoles.MANAGER], // Infra Chairs
@@ -63,6 +72,7 @@ export type AadToken = {
6372
unique_name: string;
6473
uti: string;
6574
ver: string;
75+
roles?: string[];
6676
};
6777
const smClient = new SecretsManagerClient({
6878
region: process.env.AWS_REGION || "us-east-1",
@@ -181,9 +191,22 @@ const authPlugin: FastifyPluginAsync = async (fastify, _options) => {
181191
}
182192
}
183193
} else {
184-
throw new UnauthenticatedError({
185-
message: "Could not find groups in token.",
186-
});
194+
if (verifiedTokenData.roles) {
195+
for (const group of verifiedTokenData.roles) {
196+
if (!AzureRoleMapping[fastify.runEnvironment][group]) {
197+
continue;
198+
}
199+
for (const role of AzureRoleMapping[fastify.runEnvironment][
200+
group
201+
]) {
202+
userRoles.add(role);
203+
}
204+
}
205+
} else {
206+
throw new UnauthenticatedError({
207+
message: "Could not find groups or roles in token.",
208+
});
209+
}
187210
}
188211
if (intersection(userRoles, expectedRoles).size === 0) {
189212
throw new UnauthorizedError({

0 commit comments

Comments
 (0)