1- import crypto from "crypto" ;
21import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
32import type { OidcTokenData } from "../auth" ;
4- import { clearOidcProviderToken , getOidcProviderAccessToken } from "../auth" ;
3+ import {
4+ clearOidcProviderToken ,
5+ encrypt ,
6+ getOidcProviderAccessToken ,
7+ } from "../auth" ;
58
69// Mock next/headers
710const mockCookies = vi . hoisted ( ( ) => ( {
@@ -27,36 +30,6 @@ vi.mock("better-auth/plugins", () => ({
2730 genericOAuth : vi . fn ( ( ) => ( { } ) ) ,
2831} ) ) ;
2932
30- // Encryption helper using same logic as auth.ts
31- const ENCRYPTION_SALT = "oidc_token_salt" ;
32- const KEY_LENGTH = 32 ;
33- const IV_LENGTH = 12 ;
34-
35- // Cache derived key to match auth.ts behavior
36- const TEST_DERIVED_KEY = crypto . scryptSync (
37- process . env . BETTER_AUTH_SECRET as string ,
38- ENCRYPTION_SALT ,
39- KEY_LENGTH ,
40- ) ;
41-
42- function encryptTestData ( text : string ) : string {
43- const iv = crypto . randomBytes ( IV_LENGTH ) ;
44- const cipher = crypto . createCipheriv ( "aes-256-gcm" , TEST_DERIVED_KEY , iv ) ;
45-
46- const encrypted = Buffer . concat ( [
47- cipher . update ( text , "utf8" ) ,
48- cipher . final ( ) ,
49- ] ) ;
50-
51- const authTag = cipher . getAuthTag ( ) ;
52-
53- return [
54- iv . toString ( "hex" ) ,
55- authTag . toString ( "hex" ) ,
56- encrypted . toString ( "hex" ) ,
57- ] . join ( ":" ) ;
58- }
59-
6033describe ( "auth.ts" , ( ) => {
6134 let consoleErrorSpy : ReturnType < typeof vi . spyOn > ;
6235
@@ -78,8 +51,8 @@ describe("auth.ts", () => {
7851 expiresAt : Date . now ( ) + 3600000 ,
7952 } ) ;
8053
81- // Encrypt using helper
82- const encryptedPayload = encryptTestData ( originalData ) ;
54+ // Encrypt using exported function
55+ const encryptedPayload = encrypt ( originalData ) ;
8356
8457 // Verify format (iv:authTag:encrypted)
8558 const parts = encryptedPayload . split ( ":" ) ;
@@ -91,8 +64,8 @@ describe("auth.ts", () => {
9164 it ( "should create different ciphertext for same plaintext" , ( ) => {
9265 const data = "same plaintext" ;
9366
94- const encrypted1 = encryptTestData ( data ) ;
95- const encrypted2 = encryptTestData ( data ) ;
67+ const encrypted1 = encrypt ( data ) ;
68+ const encrypted2 = encrypt ( data ) ;
9669
9770 // Different IV should produce different ciphertext
9871 expect ( encrypted1 ) . not . toBe ( encrypted2 ) ;
@@ -124,9 +97,7 @@ describe("auth.ts", () => {
12497 expiresAt : Date . now ( ) - 1000 , // Expired 1 second ago
12598 } ;
12699
127- const encryptedPayload = encryptTestData (
128- JSON . stringify ( expiredTokenData ) ,
129- ) ;
100+ const encryptedPayload = encrypt ( JSON . stringify ( expiredTokenData ) ) ;
130101 mockCookies . get . mockReturnValue ( { value : encryptedPayload } ) ;
131102
132103 const token = await getOidcProviderAccessToken ( "user-123" ) ;
@@ -142,7 +113,7 @@ describe("auth.ts", () => {
142113 expiresAt : Date . now ( ) + 3600000 ,
143114 } ;
144115
145- const encryptedPayload = encryptTestData ( JSON . stringify ( tokenData ) ) ;
116+ const encryptedPayload = encrypt ( JSON . stringify ( tokenData ) ) ;
146117 mockCookies . get . mockReturnValue ( { value : encryptedPayload } ) ;
147118
148119 const token = await getOidcProviderAccessToken ( "user-123" ) ;
@@ -157,7 +128,7 @@ describe("auth.ts", () => {
157128 expiresAt : Date . now ( ) + 3600000 , // Valid for 1 hour
158129 } ;
159130
160- const encryptedPayload = encryptTestData ( JSON . stringify ( tokenData ) ) ;
131+ const encryptedPayload = encrypt ( JSON . stringify ( tokenData ) ) ;
161132 mockCookies . get . mockReturnValue ( { value : encryptedPayload } ) ;
162133
163134 const token = await getOidcProviderAccessToken ( "user-123" ) ;
@@ -172,7 +143,7 @@ describe("auth.ts", () => {
172143 // No userId, no expiresAt
173144 } ;
174145
175- const encryptedPayload = encryptTestData ( JSON . stringify ( invalidData ) ) ;
146+ const encryptedPayload = encrypt ( JSON . stringify ( invalidData ) ) ;
176147 mockCookies . get . mockReturnValue ( { value : encryptedPayload } ) ;
177148
178149 const token = await getOidcProviderAccessToken ( "user-123" ) ;
@@ -190,8 +161,9 @@ describe("auth.ts", () => {
190161 const token = await getOidcProviderAccessToken ( "user-123" ) ;
191162
192163 expect ( token ) . toBeNull ( ) ;
164+ expect ( mockCookies . delete ) . toHaveBeenCalledWith ( "oidc_token" ) ;
193165 expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
194- "[Auth] Error reading OIDC token from cookie :" ,
166+ "[Auth] Token decryption failed - possible tampering or key mismatch :" ,
195167 expect . any ( Error ) ,
196168 ) ;
197169 } ) ;
0 commit comments