From aa46768667055f75be39fd8de898dcc2d5b55bce Mon Sep 17 00:00:00 2001 From: ImJustChew Date: Sat, 1 Jun 2024 22:36:56 +0800 Subject: [PATCH] feat: Update encryption functions to use async/await --- src/lib/headless_ais.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/headless_ais.ts b/src/lib/headless_ais.ts index 67a6b59c..347cd9ac 100644 --- a/src/lib/headless_ais.ts +++ b/src/lib/headless_ais.ts @@ -7,7 +7,7 @@ import iconv from 'iconv-lite'; import supabase_server from "@/config/supabase_server"; import crypto from 'crypto'; -const encrypt = (text: string) => { +export const encrypt = async (text: string) => { const iv = crypto.randomBytes(16); const key = Buffer.from(process.env.NTHU_HEADLESS_AIS_ENCRYPTION_KEY!, 'hex'); const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); @@ -16,7 +16,7 @@ const encrypt = (text: string) => { return encryptedPassword; } -const decrypt = (encryptedPassword: string) => { +export const decrypt = async (encryptedPassword: string) => { const key = Buffer.from(process.env.NTHU_HEADLESS_AIS_ENCRYPTION_KEY!, 'hex'); // Split the IV and the encrypted text @@ -26,7 +26,7 @@ const decrypt = (encryptedPassword: string) => { const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv); let decrypted = decipher.update(encryptedText, 'base64', 'utf8'); decrypted += decipher.final('utf8'); - return decrypted; + return await decrypted; } @@ -198,7 +198,7 @@ export const signInToCCXP = async (studentid: string, password: string): SignInT await cookies().set('accessToken', token, { path: '/', maxAge: 60 * 60 * 24, sameSite: 'strict', secure: true }); // Encrypt user password - const encryptedPassword = encrypt(password); + const encryptedPassword = await encrypt(password); return { ...result, encryptedPassword }; } catch (err) { @@ -212,7 +212,7 @@ type RefreshUserSessionResponse = Promise<{ ACIXSTORE: string } | { error: { mes export const refreshUserSession = async (studentid: string, encryptedPassword: string): RefreshUserSessionResponse => { console.log('Refreshing User Session') // Decrypt password - const password = decrypt(encryptedPassword); + const password = await decrypt(encryptedPassword); const res = await signInToCCXP(studentid, password); if('error' in res && res.error) {