|
1 | 1 | import { |
2 | 2 | checkPaidMembershipFromTable, |
3 | 3 | checkPaidMembershipFromRedis, |
| 4 | + checkExternalMembership, |
| 5 | + MEMBER_CACHE_SECONDS, |
| 6 | + checkPaidMembershipFromEntra, |
| 7 | + setPaidMembershipInTable, |
4 | 8 | } from "api/functions/membership.js"; |
5 | 9 | import { FastifyPluginAsync } from "fastify"; |
6 | | -import { ValidationError } from "common/errors/index.js"; |
| 10 | +import { |
| 11 | + InternalServerError, |
| 12 | + UnauthorizedError, |
| 13 | + ValidationError, |
| 14 | +} from "common/errors/index.js"; |
7 | 15 | import rateLimiter from "api/plugins/rateLimiter.js"; |
8 | 16 | import { createCheckoutSession } from "api/functions/stripe.js"; |
9 | 17 | import { FastifyZodOpenApiTypeProvider } from "fastify-zod-openapi"; |
10 | 18 | import * as z from "zod/v4"; |
11 | | -import { notAuthenticatedError, withTags } from "api/components/index.js"; |
| 19 | +import { |
| 20 | + illinoisNetId, |
| 21 | + notAuthenticatedError, |
| 22 | + withRoles, |
| 23 | + withTags, |
| 24 | +} from "api/components/index.js"; |
12 | 25 | import { verifyUiucAccessToken, saveHashedUserUin } from "api/functions/uin.js"; |
| 26 | +import { getKey, setKey } from "api/functions/redisCache.js"; |
| 27 | +import { getEntraIdToken } from "api/functions/entraId.js"; |
| 28 | +import { genericConfig, roleArns } from "common/config.js"; |
| 29 | +import { getRoleCredentials } from "api/functions/sts.js"; |
| 30 | +import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager"; |
| 31 | +import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; |
| 32 | +import { AppRoles } from "common/roles.js"; |
13 | 33 |
|
14 | 34 | const membershipV2Plugin: FastifyPluginAsync = async (fastify, _options) => { |
| 35 | + const getAuthorizedClients = async () => { |
| 36 | + if (roleArns.Entra) { |
| 37 | + fastify.log.info( |
| 38 | + `Attempting to assume Entra role ${roleArns.Entra} to get the Entra token...`, |
| 39 | + ); |
| 40 | + const credentials = await getRoleCredentials(roleArns.Entra); |
| 41 | + const clients = { |
| 42 | + smClient: new SecretsManagerClient({ |
| 43 | + region: genericConfig.AwsRegion, |
| 44 | + credentials, |
| 45 | + }), |
| 46 | + dynamoClient: new DynamoDBClient({ |
| 47 | + region: genericConfig.AwsRegion, |
| 48 | + credentials, |
| 49 | + }), |
| 50 | + redisClient: fastify.redisClient, |
| 51 | + }; |
| 52 | + fastify.log.info( |
| 53 | + `Assumed Entra role ${roleArns.Entra} to get the Entra token.`, |
| 54 | + ); |
| 55 | + return clients; |
| 56 | + } |
| 57 | + fastify.log.debug( |
| 58 | + "Did not assume Entra role as no env variable was present", |
| 59 | + ); |
| 60 | + return { |
| 61 | + smClient: fastify.secretsManagerClient, |
| 62 | + dynamoClient: fastify.dynamoClient, |
| 63 | + redisClient: fastify.redisClient, |
| 64 | + }; |
| 65 | + }; |
15 | 66 | const limitedRoutes: FastifyPluginAsync = async (fastify) => { |
16 | 67 | await fastify.register(rateLimiter, { |
17 | 68 | limit: 15, |
@@ -109,6 +160,158 @@ const membershipV2Plugin: FastifyPluginAsync = async (fastify, _options) => { |
109 | 160 | ); |
110 | 161 | }, |
111 | 162 | ); |
| 163 | + fastify.withTypeProvider<FastifyZodOpenApiTypeProvider>().get( |
| 164 | + "/:netId", |
| 165 | + { |
| 166 | + schema: withRoles( |
| 167 | + [ |
| 168 | + AppRoles.VIEW_INTERNAL_MEMBERSHIP_LIST, |
| 169 | + AppRoles.VIEW_EXTERNAL_MEMBERSHIP_LIST, |
| 170 | + ], |
| 171 | + withTags(["Membership"], { |
| 172 | + params: z.object({ netId: illinoisNetId }), |
| 173 | + querystring: z.object({ |
| 174 | + list: z.string().min(1).optional().meta({ |
| 175 | + example: "built", |
| 176 | + description: |
| 177 | + "Membership list to check from (defaults to ACM Paid Member list).", |
| 178 | + }), |
| 179 | + }), |
| 180 | + summary: |
| 181 | + "Check ACM @ UIUC paid membership (or partner organization membership) status.", |
| 182 | + response: { |
| 183 | + 200: { |
| 184 | + description: "List membership status.", |
| 185 | + content: { |
| 186 | + "application/json": { |
| 187 | + schema: z |
| 188 | + .object({ |
| 189 | + netId: illinoisNetId, |
| 190 | + list: z.optional(z.string().min(1)), |
| 191 | + isPaidMember: z.boolean(), |
| 192 | + }) |
| 193 | + .meta({ |
| 194 | + example: { |
| 195 | + netId: "rjjones", |
| 196 | + list: "built", |
| 197 | + isPaidMember: false, |
| 198 | + }, |
| 199 | + }), |
| 200 | + }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + }, |
| 204 | + }), |
| 205 | + ), |
| 206 | + onRequest: async (request, reply) => { |
| 207 | + await fastify.authorizeFromSchema(request, reply); |
| 208 | + if (!request.userRoles) { |
| 209 | + throw new InternalServerError({}); |
| 210 | + } |
| 211 | + const list = request.query.list || "acmpaid"; |
| 212 | + if ( |
| 213 | + list === "acmpaid" && |
| 214 | + !request.userRoles.has(AppRoles.VIEW_INTERNAL_MEMBERSHIP_LIST) |
| 215 | + ) { |
| 216 | + throw new UnauthorizedError({}); |
| 217 | + } |
| 218 | + if ( |
| 219 | + list !== "acmpaid" && |
| 220 | + !request.userRoles.has(AppRoles.VIEW_EXTERNAL_MEMBERSHIP_LIST) |
| 221 | + ) { |
| 222 | + throw new UnauthorizedError({}); |
| 223 | + } |
| 224 | + }, |
| 225 | + }, |
| 226 | + async (request, reply) => { |
| 227 | + const netId = request.params.netId.toLowerCase(); |
| 228 | + const list = request.query.list || "acmpaid"; |
| 229 | + const cacheKey = `membership:${netId}:${list}`; |
| 230 | + const result = await getKey<{ isMember: boolean }>({ |
| 231 | + redisClient: fastify.redisClient, |
| 232 | + key: cacheKey, |
| 233 | + logger: request.log, |
| 234 | + }); |
| 235 | + if (result) { |
| 236 | + return reply.header("X-ACM-Data-Source", "cache").send({ |
| 237 | + netId, |
| 238 | + list: list === "acmpaid" ? undefined : list, |
| 239 | + isPaidMember: result.isMember, |
| 240 | + }); |
| 241 | + } |
| 242 | + if (list !== "acmpaid") { |
| 243 | + const isMember = await checkExternalMembership( |
| 244 | + netId, |
| 245 | + list, |
| 246 | + fastify.dynamoClient, |
| 247 | + ); |
| 248 | + await setKey({ |
| 249 | + redisClient: fastify.redisClient, |
| 250 | + key: cacheKey, |
| 251 | + data: JSON.stringify({ isMember }), |
| 252 | + expiresIn: MEMBER_CACHE_SECONDS, |
| 253 | + logger: request.log, |
| 254 | + }); |
| 255 | + return reply.header("X-ACM-Data-Source", "dynamo").send({ |
| 256 | + netId, |
| 257 | + list, |
| 258 | + isPaidMember: isMember, |
| 259 | + }); |
| 260 | + } |
| 261 | + const isDynamoMember = await checkPaidMembershipFromTable( |
| 262 | + netId, |
| 263 | + fastify.dynamoClient, |
| 264 | + ); |
| 265 | + if (isDynamoMember) { |
| 266 | + await setKey({ |
| 267 | + redisClient: fastify.redisClient, |
| 268 | + key: cacheKey, |
| 269 | + data: JSON.stringify({ isMember: true }), |
| 270 | + expiresIn: MEMBER_CACHE_SECONDS, |
| 271 | + logger: request.log, |
| 272 | + }); |
| 273 | + return reply |
| 274 | + .header("X-ACM-Data-Source", "dynamo") |
| 275 | + .send({ netId, isPaidMember: true }); |
| 276 | + } |
| 277 | + const entraIdToken = await getEntraIdToken({ |
| 278 | + clients: await getAuthorizedClients(), |
| 279 | + clientId: fastify.environmentConfig.AadValidClientId, |
| 280 | + secretName: genericConfig.EntraSecretName, |
| 281 | + logger: request.log, |
| 282 | + }); |
| 283 | + const paidMemberGroup = fastify.environmentConfig.PaidMemberGroupId; |
| 284 | + const isAadMember = await checkPaidMembershipFromEntra( |
| 285 | + netId, |
| 286 | + entraIdToken, |
| 287 | + paidMemberGroup, |
| 288 | + ); |
| 289 | + if (isAadMember) { |
| 290 | + await setKey({ |
| 291 | + redisClient: fastify.redisClient, |
| 292 | + key: cacheKey, |
| 293 | + data: JSON.stringify({ isMember: true }), |
| 294 | + expiresIn: MEMBER_CACHE_SECONDS, |
| 295 | + logger: request.log, |
| 296 | + }); |
| 297 | + reply |
| 298 | + .header("X-ACM-Data-Source", "aad") |
| 299 | + .send({ netId, isPaidMember: true }); |
| 300 | + await setPaidMembershipInTable(netId, fastify.dynamoClient); |
| 301 | + return; |
| 302 | + } |
| 303 | + await setKey({ |
| 304 | + redisClient: fastify.redisClient, |
| 305 | + key: cacheKey, |
| 306 | + data: JSON.stringify({ isMember: false }), |
| 307 | + expiresIn: MEMBER_CACHE_SECONDS, |
| 308 | + logger: request.log, |
| 309 | + }); |
| 310 | + return reply |
| 311 | + .header("X-ACM-Data-Source", "aad") |
| 312 | + .send({ netId, isPaidMember: false }); |
| 313 | + }, |
| 314 | + ); |
112 | 315 | }; |
113 | 316 | fastify.register(limitedRoutes); |
114 | 317 | }; |
|
0 commit comments