Add back vc auth temporarily to unblock things
This commit is contained in:
parent
07c2e7d36a
commit
177b2cc210
20 changed files with 350 additions and 40 deletions
25
lib/cookies/decrypt-jwe.ts
Normal file
25
lib/cookies/decrypt-jwe.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { jwtDecrypt, base64url } from 'jose';
|
||||
|
||||
export async function decryptJWECookie<T extends string | object = any>(
|
||||
cookie: string,
|
||||
secret: string | undefined = process.env.JWE_SECRET
|
||||
): Promise<T | undefined> {
|
||||
if (!secret) {
|
||||
throw new Error('Missing JWE secret');
|
||||
}
|
||||
|
||||
if (typeof cookie !== 'string') return;
|
||||
|
||||
try {
|
||||
const { payload } = await jwtDecrypt(cookie, base64url.decode(secret));
|
||||
const decoded = payload as T;
|
||||
|
||||
// @ts-expect-error jwt field
|
||||
delete decoded.iat;
|
||||
// @ts-expect-error jwt field
|
||||
delete decoded.exp;
|
||||
return decoded;
|
||||
} catch {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
16
lib/cookies/encrypt-jwe.ts
Normal file
16
lib/cookies/encrypt-jwe.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { EncryptJWT, base64url } from 'jose';
|
||||
|
||||
export async function encryptJWECookie<T extends string | object = any>(
|
||||
payload: T,
|
||||
expirationTime: string,
|
||||
secret: string | undefined = process.env.JWE_SECRET
|
||||
): Promise<string> {
|
||||
if (!secret) {
|
||||
throw new Error('Missing JWE secret');
|
||||
}
|
||||
|
||||
return new EncryptJWT(payload as any)
|
||||
.setExpirationTime(expirationTime)
|
||||
.setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })
|
||||
.encrypt(base64url.decode(secret));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue