|
| 1 | +import { |
| 2 | + checkPaidMembershipFromTable, |
| 3 | + checkPaidMembershipFromRedis, |
| 4 | +} from "api/functions/membership.js"; |
| 5 | +import { FastifyPluginAsync } from "fastify"; |
| 6 | +import { ValidationError } from "common/errors/index.js"; |
| 7 | +import rateLimiter from "api/plugins/rateLimiter.js"; |
| 8 | +import { FastifyZodOpenApiTypeProvider } from "fastify-zod-openapi"; |
| 9 | +import * as z from "zod/v4"; |
| 10 | +import { notAuthenticatedError, withTags } from "api/components/index.js"; |
| 11 | +import { verifyUiucAccessToken, saveHashedUserUin } from "api/functions/uin.js"; |
| 12 | +import { getRoleCredentials } from "api/functions/sts.js"; |
| 13 | +import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager"; |
| 14 | +import { genericConfig, roleArns } from "common/config.js"; |
| 15 | +import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; |
| 16 | +import { |
| 17 | + getEntraIdToken, |
| 18 | + patchUserProfile, |
| 19 | + resolveEmailToOid, |
| 20 | +} from "api/functions/entraId.js"; |
| 21 | + |
| 22 | +const syncIdentityPlugin: FastifyPluginAsync = async (fastify, _options) => { |
| 23 | + const getAuthorizedClients = async () => { |
| 24 | + if (roleArns.Entra) { |
| 25 | + fastify.log.info( |
| 26 | + `Attempting to assume Entra role ${roleArns.Entra} to get the Entra token...`, |
| 27 | + ); |
| 28 | + const credentials = await getRoleCredentials(roleArns.Entra); |
| 29 | + const clients = { |
| 30 | + smClient: new SecretsManagerClient({ |
| 31 | + region: genericConfig.AwsRegion, |
| 32 | + credentials, |
| 33 | + }), |
| 34 | + dynamoClient: new DynamoDBClient({ |
| 35 | + region: genericConfig.AwsRegion, |
| 36 | + credentials, |
| 37 | + }), |
| 38 | + redisClient: fastify.redisClient, |
| 39 | + }; |
| 40 | + fastify.log.info( |
| 41 | + `Assumed Entra role ${roleArns.Entra} to get the Entra token.`, |
| 42 | + ); |
| 43 | + return clients; |
| 44 | + } |
| 45 | + fastify.log.debug( |
| 46 | + "Did not assume Entra role as no env variable was present", |
| 47 | + ); |
| 48 | + return { |
| 49 | + smClient: fastify.secretsManagerClient, |
| 50 | + dynamoClient: fastify.dynamoClient, |
| 51 | + redisClient: fastify.redisClient, |
| 52 | + }; |
| 53 | + }; |
| 54 | + const limitedRoutes: FastifyPluginAsync = async (fastify) => { |
| 55 | + await fastify.register(rateLimiter, { |
| 56 | + limit: 5, |
| 57 | + duration: 30, |
| 58 | + rateLimitIdentifier: "syncIdentityPlugin", |
| 59 | + }); |
| 60 | + fastify.withTypeProvider<FastifyZodOpenApiTypeProvider>().post( |
| 61 | + "/", |
| 62 | + { |
| 63 | + schema: withTags(["Generic"], { |
| 64 | + headers: z.object({ |
| 65 | + "x-uiuc-token": z.jwt().min(1).meta({ |
| 66 | + description: |
| 67 | + "An access token for the user in the UIUC Entra ID tenant.", |
| 68 | + }), |
| 69 | + }), |
| 70 | + summary: |
| 71 | + "Sync the Illinois NetID account with the ACM @ UIUC account.", |
| 72 | + response: { |
| 73 | + 201: { |
| 74 | + description: "The user has been synced.", |
| 75 | + content: { |
| 76 | + "application/json": { |
| 77 | + schema: z.null(), |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + 403: notAuthenticatedError, |
| 82 | + }, |
| 83 | + }), |
| 84 | + }, |
| 85 | + async (request, reply) => { |
| 86 | + const accessToken = request.headers["x-uiuc-token"]; |
| 87 | + const verifiedData = await verifyUiucAccessToken({ |
| 88 | + accessToken, |
| 89 | + logger: request.log, |
| 90 | + }); |
| 91 | + const { userPrincipalName: upn, givenName, surname } = verifiedData; |
| 92 | + const netId = upn.replace("@illinois.edu", ""); |
| 93 | + if (netId.includes("@")) { |
| 94 | + request.log.error( |
| 95 | + `Found UPN ${upn} which cannot be turned into NetID via simple replacement.`, |
| 96 | + ); |
| 97 | + throw new ValidationError({ |
| 98 | + message: "ID token could not be parsed.", |
| 99 | + }); |
| 100 | + } |
| 101 | + await saveHashedUserUin({ |
| 102 | + uiucAccessToken: accessToken, |
| 103 | + pepper: fastify.secretConfig.UIN_HASHING_SECRET_PEPPER, |
| 104 | + dynamoClient: fastify.dynamoClient, |
| 105 | + netId, |
| 106 | + }); |
| 107 | + let isPaidMember = await checkPaidMembershipFromRedis( |
| 108 | + netId, |
| 109 | + fastify.redisClient, |
| 110 | + request.log, |
| 111 | + ); |
| 112 | + if (isPaidMember === null) { |
| 113 | + isPaidMember = await checkPaidMembershipFromTable( |
| 114 | + netId, |
| 115 | + fastify.dynamoClient, |
| 116 | + ); |
| 117 | + } |
| 118 | + if (isPaidMember) { |
| 119 | + const username = `${netId}@illinois.edu`; |
| 120 | + request.log.info("User is paid member, syncing profile!"); |
| 121 | + const entraIdToken = await getEntraIdToken({ |
| 122 | + clients: await getAuthorizedClients(), |
| 123 | + clientId: fastify.environmentConfig.AadValidClientId, |
| 124 | + secretName: genericConfig.EntraSecretName, |
| 125 | + logger: request.log, |
| 126 | + }); |
| 127 | + const oid = await resolveEmailToOid(entraIdToken, username); |
| 128 | + await patchUserProfile(entraIdToken, username, oid, { |
| 129 | + displayName: `${givenName} ${surname}`, |
| 130 | + givenName, |
| 131 | + surname, |
| 132 | + mail: username, |
| 133 | + }); |
| 134 | + } |
| 135 | + return reply.status(201).send(); |
| 136 | + }, |
| 137 | + ); |
| 138 | + }; |
| 139 | + fastify.register(limitedRoutes); |
| 140 | +}; |
| 141 | + |
| 142 | +export default syncIdentityPlugin; |
0 commit comments