|
| 1 | +import { getSecretValue } from "../plugins/auth.js"; |
| 2 | +import { genericConfig } from "../../common/config.js"; |
| 3 | +import { |
| 4 | + InternalServerError, |
| 5 | + UnauthorizedError, |
| 6 | +} from "../../common/errors/index.js"; |
| 7 | +import { FastifyInstance, FastifyRequest } from "fastify"; |
| 8 | +// these make sure that esbuild includes the files |
| 9 | +import icon from "../resources/MembershipPass.pkpass/icon.png"; |
| 10 | +import logo from "../resources/MembershipPass.pkpass/logo.png"; |
| 11 | +import strip from "../resources/MembershipPass.pkpass/strip.png"; |
| 12 | +import pass from "../resources/MembershipPass.pkpass/pass.js"; |
| 13 | +import { PKPass } from "passkit-generator"; |
| 14 | +import { promises as fs } from "fs"; |
| 15 | + |
| 16 | +function trim(s: string) { |
| 17 | + return (s || "").replace(/^\s+|\s+$/g, ""); |
| 18 | +} |
| 19 | + |
| 20 | +function convertName(name: string): string { |
| 21 | + if (!name.includes(",")) { |
| 22 | + return name; |
| 23 | + } |
| 24 | + return `${trim(name.split(",")[1])} ${name.split(",")[0]}`; |
| 25 | +} |
| 26 | + |
| 27 | +export async function issueAppleWalletMembershipCard( |
| 28 | + app: FastifyInstance, |
| 29 | + request: FastifyRequest, |
| 30 | + email: string, |
| 31 | + name: string, |
| 32 | +) { |
| 33 | + if (!email.endsWith("@illinois.edu")) { |
| 34 | + throw new UnauthorizedError({ |
| 35 | + message: |
| 36 | + "Cannot issue membership pass for emails not on the illinois.edu domain.", |
| 37 | + }); |
| 38 | + } |
| 39 | + const secretApiConfig = await getSecretValue( |
| 40 | + app.secretsManagerClient, |
| 41 | + genericConfig.ConfigSecretName, |
| 42 | + ); |
| 43 | + if (!secretApiConfig) { |
| 44 | + throw new InternalServerError({ |
| 45 | + message: "Could not retrieve signing data", |
| 46 | + }); |
| 47 | + } |
| 48 | + const signerCert = Buffer.from( |
| 49 | + secretApiConfig.acm_passkit_signerCert_base64, |
| 50 | + "base64", |
| 51 | + ).toString("utf-8"); |
| 52 | + const signerKey = Buffer.from( |
| 53 | + secretApiConfig.acm_passkit_signerKey_base64, |
| 54 | + "base64", |
| 55 | + ).toString("utf-8"); |
| 56 | + const wwdr = Buffer.from( |
| 57 | + secretApiConfig.apple_signing_cert_base64, |
| 58 | + "base64", |
| 59 | + ).toString("utf-8"); |
| 60 | + pass["passTypeIdentifier"] = app.environmentConfig["PasskitIdentifier"]; |
| 61 | + |
| 62 | + const pkpass = new PKPass( |
| 63 | + { |
| 64 | + "icon.png": await fs.readFile(icon), |
| 65 | + "logo.png": await fs.readFile(logo), |
| 66 | + "strip.png": await fs.readFile(strip), |
| 67 | + "pass.json": Buffer.from(JSON.stringify(pass)), |
| 68 | + }, |
| 69 | + { |
| 70 | + wwdr, |
| 71 | + signerCert, |
| 72 | + signerKey, |
| 73 | + }, |
| 74 | + { |
| 75 | + // logoText: app.runEnvironment === "dev" ? "INVALID Membership Pass" : "Membership Pass", |
| 76 | + serialNumber: app.environmentConfig["PasskitSerialNumber"], |
| 77 | + }, |
| 78 | + ); |
| 79 | + pkpass.setBarcodes({ |
| 80 | + altText: email.split("@")[0], |
| 81 | + format: "PKBarcodeFormatPDF417", |
| 82 | + message: app.runEnvironment === "dev" ? `INVALID${email}INVALID` : email, |
| 83 | + }); |
| 84 | + const iat = new Date().toLocaleDateString("en-US", { |
| 85 | + day: "2-digit", |
| 86 | + month: "2-digit", |
| 87 | + year: "numeric", |
| 88 | + }); |
| 89 | + if (name && name !== "") { |
| 90 | + pkpass.secondaryFields.push({ |
| 91 | + label: "Member Name", |
| 92 | + key: "name", |
| 93 | + value: convertName(name), |
| 94 | + }); |
| 95 | + } |
| 96 | + if (app.runEnvironment === "prod") { |
| 97 | + pkpass.backFields.push({ |
| 98 | + label: "Verification URL", |
| 99 | + key: "iss", |
| 100 | + value: `https://membership.acm.illinois.edu/verify/${email.split("@")[0]}`, |
| 101 | + }); |
| 102 | + } else { |
| 103 | + pkpass.backFields.push({ |
| 104 | + label: "TESTING ONLY Pass", |
| 105 | + key: "iss", |
| 106 | + value: `Do not honor!`, |
| 107 | + }); |
| 108 | + } |
| 109 | + pkpass.backFields.push({ label: "Pass Created On", key: "iat", value: iat }); |
| 110 | + pkpass.backFields.push({ label: "Membership ID", key: "id", value: email }); |
| 111 | + const buffer = pkpass.getAsBuffer(); |
| 112 | + request.log.info( |
| 113 | + { type: "audit", actor: email, target: email }, |
| 114 | + "Created membership verification pass", |
| 115 | + ); |
| 116 | + return buffer; |
| 117 | +} |
0 commit comments