Skip to content

Commit

Permalink
better functions and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aletya committed Jan 30, 2025
1 parent d6bb462 commit e95dd6b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
13 changes: 3 additions & 10 deletions src/services/staff/staff-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
CodeExpiredErrorSchema,
QRExpiredError,
QRExpiredErrorSchema,
// QRInvalidError,
QRInvalidErrorSchema,
ScanAttendeeRequestSchema,
ScanAttendeeSchema,
Expand All @@ -22,7 +21,7 @@ import { performCheckIn, PerformCheckInErrors } from "./staff-lib";
import specification, { Tag } from "../../middleware/specification";
import { SuccessResponseSchema } from "../../common/schemas";
import { EventNotFoundError, EventNotFoundErrorSchema } from "../event/event-schemas";
import { decryptQR } from "../user/user-lib";
import { decryptQRCode } from "../user/user-lib";

const staffRouter = Router();

Expand Down Expand Up @@ -106,18 +105,12 @@ staffRouter.put(
}),
async (req, res) => {
const { attendeeQRCode, eventId } = req.body;
const currentTime = Math.floor(Date.now() / Config.MILLISECONDS_PER_SECOND);

// Decrypt and validate token
const decodedPayload = decryptQR(attendeeQRCode);

// Validate expiration time
if (decodedPayload.exp < currentTime) {
const userId = decryptQRCode(attendeeQRCode);
if (!userId) {
return res.status(StatusCode.ClientErrorUnauthorized).send(QRExpiredError);
}

const userId = decodedPayload.userId;

// Perform check-in logic
const result = await performCheckIn(eventId, userId);
if (!result.success) {
Expand Down
22 changes: 22 additions & 0 deletions src/services/user/user-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,25 @@ export function generateQRCodeURI(userId: string): string {

return uri;
}

export function decryptQRCode(token: string): string | null {
const currentTime = Math.floor(Date.now() / Config.MILLISECONDS_PER_SECOND);

// Decrypt and validate token
const decrypted = decryptData(token, derivedAESKey);
const [userId, exp] = decrypted.split(":");

// Validate that userId and exp are present
if (!userId || !exp) {
return null;
}

const expNumber = parseInt(exp, 10);
// Validate expiration time
if (expNumber < currentTime) {
return null;
}

// Return the userId if not expired
return userId;
}
3 changes: 1 addition & 2 deletions src/services/user/user-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import {
import { UserIdSchema } from "../../common/schemas";
import { EventNotFoundError, EventNotFoundErrorSchema } from "../event/event-schemas";
import Models from "../../common/models";
import Config from "../../common/config";
import specification, { Tag } from "../../middleware/specification";
import { z } from "zod";
import { encryptQR, generateQRCodeURI } from "./user-lib";
import { generateQRCodeURI } from "./user-lib";

const userRouter = Router();

Expand Down

0 comments on commit e95dd6b

Please sign in to comment.