Skip to content

Commit 0229296

Browse files
committed
Build a generic sync identity that also gives first name and last name
1 parent a9e3544 commit 0229296

File tree

5 files changed

+68
-9
lines changed

5 files changed

+68
-9
lines changed

onetime/uinHash-migration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def migrate_uin_hashes():
4848

4949
# Construct the primary key and update parameters for the destination table
5050
destination_pk_id = f"{net_id}{DESTINATION_ID_SUFFIX}"
51-
update_expression = "SET uinHash = :uh"
52-
expression_attribute_values = {":uh": uin_hash}
51+
update_expression = "SET uinHash = :uh, netId = :ne"
52+
expression_attribute_values = {":uh": uin_hash, ":ne": net_id}
5353

5454
# Update the item in the destination DynamoDB table
5555
try:

src/api/functions/sync.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {
2+
UpdateItemCommand,
3+
type DynamoDBClient,
4+
} from "@aws-sdk/client-dynamodb";
5+
import { genericConfig } from "common/config.js";
6+
7+
export interface SyncFullProfileInputs {
8+
uinHash: string;
9+
netId: string;
10+
firstName: string;
11+
lastName: string;
12+
dynamoClient: DynamoDBClient;
13+
}
14+
15+
export async function syncFullProfile({
16+
uinHash,
17+
netId,
18+
firstName,
19+
lastName,
20+
dynamoClient,
21+
}: SyncFullProfileInputs) {
22+
return dynamoClient.send(
23+
new UpdateItemCommand({
24+
TableName: genericConfig.UserInfoTable,
25+
Key: {
26+
id: { S: `${netId}@illinois.edu` },
27+
},
28+
UpdateExpression:
29+
"SET #uinHash = :uinHash, #netId = :netId, #updatedAt = :updatedAt, #firstName = :firstName, #lastName = :lastName",
30+
ExpressionAttributeNames: {
31+
"#uinHash": "uinHash",
32+
"#netId": "netId",
33+
"#updatedAt": "updatedAt",
34+
"#firstName": "firstName",
35+
"#lastName": "lastName",
36+
},
37+
ExpressionAttributeValues: {
38+
":uinHash": { S: uinHash },
39+
":netId": { S: netId },
40+
":firstName": { S: firstName },
41+
":lastName": { S: lastName },
42+
":updatedAt": { S: new Date().toISOString() },
43+
},
44+
}),
45+
);
46+
}

src/api/functions/uin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export async function saveHashedUserUin({
178178
},
179179
ExpressionAttributeValues: {
180180
":uinHash": { S: uinHash },
181+
":netId": { S: netId },
181182
":updatedAt": { S: new Date().toISOString() },
182183
},
183184
}),

src/api/routes/syncIdentity.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import rateLimiter from "api/plugins/rateLimiter.js";
88
import { FastifyZodOpenApiTypeProvider } from "fastify-zod-openapi";
99
import * as z from "zod/v4";
1010
import { notAuthenticatedError, withTags } from "api/components/index.js";
11-
import { verifyUiucAccessToken, saveHashedUserUin } from "api/functions/uin.js";
11+
import { verifyUiucAccessToken, getHashedUserUin } from "api/functions/uin.js";
1212
import { getRoleCredentials } from "api/functions/sts.js";
1313
import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager";
1414
import { genericConfig, roleArns } from "common/config.js";
@@ -18,6 +18,7 @@ import {
1818
patchUserProfile,
1919
resolveEmailToOid,
2020
} from "api/functions/entraId.js";
21+
import { syncFullProfile } from "api/functions/sync.js";
2122

2223
const syncIdentityPlugin: FastifyPluginAsync = async (fastify, _options) => {
2324
const getAuthorizedClients = async () => {
@@ -98,11 +99,16 @@ const syncIdentityPlugin: FastifyPluginAsync = async (fastify, _options) => {
9899
message: "ID token could not be parsed.",
99100
});
100101
}
101-
await saveHashedUserUin({
102+
const uinHash = await getHashedUserUin({
102103
uiucAccessToken: accessToken,
103104
pepper: fastify.secretConfig.UIN_HASHING_SECRET_PEPPER,
104-
dynamoClient: fastify.dynamoClient,
105+
});
106+
await syncFullProfile({
107+
uinHash,
108+
firstName: givenName,
109+
lastName: surname,
105110
netId,
111+
dynamoClient: fastify.dynamoClient,
106112
});
107113
let isPaidMember = await checkPaidMembershipFromRedis(
108114
netId,

src/api/routes/v2/membership.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
withRoles,
2323
withTags,
2424
} from "api/components/index.js";
25-
import { verifyUiucAccessToken, saveHashedUserUin } from "api/functions/uin.js";
25+
import { verifyUiucAccessToken, getHashedUserUin } from "api/functions/uin.js";
2626
import { getKey, setKey } from "api/functions/redisCache.js";
2727
import { getEntraIdToken } from "api/functions/entraId.js";
2828
import { genericConfig, roleArns } from "common/config.js";
@@ -31,6 +31,7 @@ import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager";
3131
import { BatchGetItemCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb";
3232
import { AppRoles } from "common/roles.js";
3333
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
34+
import { syncFullProfile } from "api/functions/sync.js";
3435

3536
const membershipV2Plugin: FastifyPluginAsync = async (fastify, _options) => {
3637
const getAuthorizedClients = async () => {
@@ -115,11 +116,16 @@ const membershipV2Plugin: FastifyPluginAsync = async (fastify, _options) => {
115116
});
116117
}
117118
request.log.debug("Saving user hashed UIN!");
118-
const saveHashPromise = saveHashedUserUin({
119+
const uinHash = await getHashedUserUin({
119120
uiucAccessToken: accessToken,
120121
pepper: fastify.secretConfig.UIN_HASHING_SECRET_PEPPER,
121-
dynamoClient: fastify.dynamoClient,
122+
});
123+
const savePromise = syncFullProfile({
124+
uinHash,
125+
firstName: givenName,
126+
lastName: surname,
122127
netId,
128+
dynamoClient: fastify.dynamoClient,
123129
});
124130
let isPaidMember = await checkPaidMembershipFromRedis(
125131
netId,
@@ -132,7 +138,7 @@ const membershipV2Plugin: FastifyPluginAsync = async (fastify, _options) => {
132138
fastify.dynamoClient,
133139
);
134140
}
135-
await saveHashPromise;
141+
await savePromise;
136142
request.log.debug("Saved user hashed UIN!");
137143
if (isPaidMember) {
138144
throw new ValidationError({

0 commit comments

Comments
 (0)