Skip to content

Commit fab6f67

Browse files
committed
cleanup
1 parent 89fd2f4 commit fab6f67

File tree

1 file changed

+2
-28
lines changed

1 file changed

+2
-28
lines changed

src/common/crypto/seal.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ interface UnsealOptions {
1717
ttl?: number;
1818
}
1919

20-
/**
21-
* Parse an iron-session seal to extract the version
22-
*/
2320
function parseSeal(seal: string): {
2421
sealWithoutVersion: string;
2522
tokenVersion: number | null;
@@ -31,15 +28,6 @@ function parseSeal(seal: string): {
3128
return { sealWithoutVersion, tokenVersion };
3229
}
3330

34-
/**
35-
* Seal (encrypt) data in a format compatible with iron-session.
36-
*
37-
* @param data - The data to seal
38-
* @param options - Sealing options
39-
* @param options.password - Password for encryption (must be at least 32 characters)
40-
* @param options.ttl - Time to live in seconds (default: 0 = no expiration)
41-
* @returns The sealed string
42-
*/
4331
export async function sealData(
4432
data: unknown,
4533
{ password, ttl = 0 }: SealOptions,
@@ -51,30 +39,19 @@ export async function sealData(
5139

5240
const seal = await ironSeal(data, passwordObj, {
5341
...defaults,
54-
ttl: ttl * 1000, // Convert seconds to milliseconds
55-
encode: JSON.stringify, // Preserve v1 lenient serialization
42+
ttl: ttl * 1000,
43+
encode: JSON.stringify,
5644
});
5745

58-
// Add the version delimiter exactly like iron-session does
5946
return `${seal}${VERSION_DELIMITER}${CURRENT_MAJOR_VERSION}`;
6047
}
6148

62-
/**
63-
* Unseal (decrypt) data that was sealed with iron-session or sealData.
64-
*
65-
* @param encryptedData - The sealed string to decrypt
66-
* @param options - Unsealing options
67-
* @param options.password - Password for decryption
68-
* @param options.ttl - Time to live in seconds (default: 0 = no expiration check)
69-
* @returns The unsealed data, or empty object if unsealing fails
70-
*/
7149
export async function unsealData<T = unknown>(
7250
encryptedData: string,
7351
{ password, ttl = 0 }: UnsealOptions,
7452
): Promise<T> {
7553
const { sealWithoutVersion, tokenVersion } = parseSeal(encryptedData);
7654

77-
// Format password as a map like iron-session expects
7855
const passwordMap = { 1: password };
7956

8057
let data: unknown;
@@ -85,7 +62,6 @@ export async function unsealData<T = unknown>(
8562
ttl: ttl * 1000,
8663
})) ?? {};
8764
} catch (error) {
88-
// Match iron-session's behavior: return empty object for known errors
8965
if (
9066
error instanceof Error &&
9167
/^(Expired seal|Bad hmac value|Cannot find password|Incorrect number of sealed components)/.test(
@@ -97,11 +73,9 @@ export async function unsealData<T = unknown>(
9773
throw error;
9874
}
9975

100-
// Handle token version for backwards compatibility
10176
if (tokenVersion === 2) {
10277
return data as T;
10378
} else if (tokenVersion !== null) {
104-
// For older token versions, extract the persistent property
10579
const record = data as Record<string, unknown>;
10680
return (record.persistent ?? data) as T;
10781
}

0 commit comments

Comments
 (0)