From 39012ded3d6d1730faa71ebfe1f72491c6e192e9 Mon Sep 17 00:00:00 2001 From: Theodor Kvalsvik Lauritzen <19690242+theodorklauritzen@users.noreply.github.com> Date: Thu, 7 Nov 2024 20:04:42 +0100 Subject: [PATCH] fix: Admision trials --- src/actions/admission/create.ts | 5 +-- .../OmegaId/reader/OmegaIdReader.tsx | 6 ++-- .../admission/[admission]/registration.tsx | 14 ++++---- src/app/admin/admission/page.tsx | 2 +- src/jwt/parseJWTClient.ts | 34 ++++++++----------- src/services/admission/Types.ts | 7 ++++ src/services/admission/create.ts | 11 ++++-- 7 files changed, 43 insertions(+), 36 deletions(-) create mode 100644 src/services/admission/Types.ts diff --git a/src/actions/admission/create.ts b/src/actions/admission/create.ts index 8741c7b4f..fac7ef314 100644 --- a/src/actions/admission/create.ts +++ b/src/actions/admission/create.ts @@ -6,13 +6,14 @@ import { createAdmissionTrial } from '@/services/admission/create' import { createAdmissionTrialValidation } from '@/services/admission/validation' import { Session } from '@/auth/Session' import type { ActionReturn } from '@/actions/Types' -import type { Admission, AdmissionTrial } from '@prisma/client' +import type { Admission } from '@prisma/client' +import type { ExpandedAdmissionTrail } from '@/services/admission/Types' export async function createAdmissionTrialAction( admission: Admission, userId: FormData | number -): Promise> { +): Promise> { const session = await Session.fromNextAuth() const authRes = CreateAdmissionTrialAuther.dynamicFields({}).auth(session) diff --git a/src/app/_components/OmegaId/reader/OmegaIdReader.tsx b/src/app/_components/OmegaId/reader/OmegaIdReader.tsx index 14fd9998d..d1ad5fe22 100644 --- a/src/app/_components/OmegaId/reader/OmegaIdReader.tsx +++ b/src/app/_components/OmegaId/reader/OmegaIdReader.tsx @@ -27,7 +27,7 @@ export default function OmegaIdReader({ debounceThreshold, singleRead, }: { - successCallback: (user: OmegaId, token: string) => Promise<{ + successCallback: (user: number, token: string) => Promise<{ success: boolean, text: string, }>, @@ -72,7 +72,7 @@ export default function OmegaIdReader({ return } - const userId = parse.data.id + const userId = parse.data if (userId === lastReadUserId && Date.now() - lastReadTime < (debounceThreshold ?? 5000)) { lastReadTime = Date.now() @@ -84,7 +84,7 @@ export default function OmegaIdReader({ text: '...', }) - const results = await successCallback(parse.data, token) + const results = await successCallback(userId, token.data) if (results.success && (singleRead ?? false)) { html5QrcodeScanner.clear() diff --git a/src/app/admin/admission/[admission]/registration.tsx b/src/app/admin/admission/[admission]/registration.tsx index 021db5d85..c9b97c6c1 100644 --- a/src/app/admin/admission/[admission]/registration.tsx +++ b/src/app/admin/admission/[admission]/registration.tsx @@ -19,19 +19,17 @@ export default function RegisterAdmissiontrial({

Registrer med QR kode

{ - const results = await createAdmissionTrialAction(admission, user.id) + successCallback={async (userId) => { + const results = await createAdmissionTrialAction(admission, userId) let msg = results.success ? - `${user.firstname} er registrert` : + `${results.data.user.firstname} ${results.data.user.lastname} er registrert` : 'Kunne ikke regisrere bruker grunnet en ukjent feil.' if (!results.success && results.error) { - msg = `${user.firstname}: ${ - results.error - .map(e => e.message) - .reduce((acc, val) => `${acc}\n${val}`, '') - }` + msg = results.error + .map(e => e.message) + .reduce((acc, val) => `${acc}\n${val}`, '') } return { diff --git a/src/app/admin/admission/page.tsx b/src/app/admin/admission/page.tsx index 5c767ed32..8d98515bc 100644 --- a/src/app/admin/admission/page.tsx +++ b/src/app/admin/admission/page.tsx @@ -12,7 +12,7 @@ export default async function AdmissionTrials() {
    {AdmissionsArray.map(trial =>
  • - {AdmissionDisplayNames[trial]} + {AdmissionDisplayNames[trial]}
  • )}
diff --git a/src/jwt/parseJWTClient.ts b/src/jwt/parseJWTClient.ts index 74ac2bc12..dec03f0df 100644 --- a/src/jwt/parseJWTClient.ts +++ b/src/jwt/parseJWTClient.ts @@ -5,7 +5,6 @@ import { JWT_ISSUER } from '@/auth/ConfigVars' import { createActionError } from '@/actions/error' import type { OmegaJWTAudience } from '@/auth/Types' import type { ActionReturn } from '@/actions/Types' -import type { OmegaId } from '@/services/omegaid/Types' /** * Parses a JSON Web Token (JWT) and verifies its signature using the provided public key. @@ -16,11 +15,16 @@ import type { OmegaId } from '@/services/omegaid/Types' * @returns A promise that resolves to an `ActionReturn` object containing the parsed JWT payload if the JWT is valid, * or an error object if the JWT is invalid. */ -export async function parseJWT(token: string, publicKey: string, timeOffset: number): Promise> { +export async function parseJWT( + token: string, + publicKey: string, + timeOffset: number, + audience: OmegaJWTAudience +): Promise> { // TODO: This only works in safari and firefox :/// - function invalidJWT(message?: string): ActionReturn { - return createActionError('JWT INVALID', message || 'Ugyldig QR kode') + function invalidJWT(message?: string): ActionReturn { + return createActionError('JWT INVALID', message || 'Invalid JWT') } if (timeOffset < 0) { @@ -30,7 +34,7 @@ export async function parseJWT(token: string, publicKey: string, timeOffset: num const tokenS = token.split('.') if (tokenS.length !== 3) { - return invalidJWT('Ugyldig QR kode type') + return invalidJWT('Malformatted JWT') } const keyStripped = publicKey @@ -68,35 +72,25 @@ export async function parseJWT(token: string, publicKey: string, timeOffset: num try { const payload = readJWTPayload(token) - if (!( - typeof payload.usrnm === 'string' && - typeof payload.gn === 'string' && - typeof payload.sn === 'string' && - typeof payload.sub === 'number' - )) { - return invalidJWT('Invalid fields') + if (typeof payload.sub !== 'number') { + return invalidJWT('JWT is missing sub field') } if (new Date(payload.exp * 1000 + timeOffset) < new Date()) { - return invalidJWT('QR koden er utløpt') + return invalidJWT('JWT has expired') } if (payload.iss !== JWT_ISSUER) { return invalidJWT('Invalid issuer') } - if (payload.aud !== 'omegaid' satisfies OmegaJWTAudience) { + if (payload.aud !== audience) { return invalidJWT('Invalid audience') } return { success: true, - data: { - id: payload.sub, - username: payload.usrnm, - firstname: payload.gn, - lastname: payload.sn, - } + data: payload.sub } } catch { return invalidJWT('An unexpected error occured') diff --git a/src/services/admission/Types.ts b/src/services/admission/Types.ts new file mode 100644 index 000000000..097d1de8b --- /dev/null +++ b/src/services/admission/Types.ts @@ -0,0 +1,7 @@ +import type { AdmissionTrial } from '@prisma/client' +import type { UserFiltered } from '@/services/users/Types' + + +export type ExpandedAdmissionTrail = AdmissionTrial & { + user: UserFiltered +} diff --git a/src/services/admission/create.ts b/src/services/admission/create.ts index 58318f356..507a67ab8 100644 --- a/src/services/admission/create.ts +++ b/src/services/admission/create.ts @@ -4,12 +4,14 @@ import { readUserAdmissionTrials } from './read' import { prismaCall } from '@/services/prismaCall' import { updateUserOmegaMembershipGroup } from '@/services/groups/omegaMembershipGroups/update' import prisma from '@/prisma' -import { Admission, type AdmissionTrial } from '@prisma/client' +import { userFilterSelection } from '@/services/users/ConfigVars' +import { Admission } from '@prisma/client' import type { CreateAdmissionTrialType } from './validation' +import type { ExpandedAdmissionTrail } from './Types' export async function createAdmissionTrial( data: CreateAdmissionTrialType['Detailed'] -): Promise { +): Promise { const parse = createAdmissionTrialValidation.detailedValidate(data) const results = await prismaCall(() => prisma.admissionTrial.create({ @@ -25,6 +27,11 @@ export async function createAdmissionTrial( }, }, admission: parse.admission, + }, + include: { + user: { + select: userFilterSelection + } } }))