-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
170 lines (149 loc) · 5.88 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// deno-lint-ignore-file no-explicit-any
import type {
CookieInit, CookieList, CookieListItem, CookieStore, CookieStoreDeleteOptions, CookieStoreGetOptions,
} from 'https://ghuc.cc/qwtel/cookie-store-interface/index.d.ts';
export * from 'https://ghuc.cc/qwtel/cookie-store-interface/index.d.ts';
import { bufferSourceToUint8Array, concatBufferSources, splitBufferSource } from "https://ghuc.cc/qwtel/typed-array-utils/index.ts";
import { Base64Decoder, Base64Encoder } from "https://ghuc.cc/qwtel/base64-encoding/index.ts";
import { AggregateError } from "./aggregate-error.ts";
const EXT = '.enc';
const IV_LENGTH = 16; // bytes
const secretToUint8Array = (secret: string | BufferSource) => typeof secret === 'string'
? new TextEncoder().encode(secret)
: bufferSourceToUint8Array(secret);
export interface EncryptedCookieStoreOptions {
/**
* One or more crypto keys that were previously used to encrypt cookies.
* `EncryptedCookieStore` will try to decrypt cookies using these, but they are not used for encrypting new cookies.
*/
keyring?: readonly CryptoKey[],
}
export interface DeriveOptions {
secret: string | BufferSource | JsonWebKey
salt?: BufferSource
iterations?: number
format?: KeyFormat,
hash?: HashAlgorithmIdentifier;
hmacHash?: HashAlgorithmIdentifier;
length?: number,
}
/**
* # Encrypted Cookie Store
* A partial implementation of the [Cookie Store API](https://wicg.github.io/cookie-store)
* that transparently encrypts and decrypts cookies via AES-GCM.
*
* This is likely only useful in server-side implementations,
* but written in a platform-agnostic way.
*/
export class EncryptedCookieStore implements CookieStore {
/** A helper function to derive a crypto key from a passphrase */
static async deriveCryptoKey(opts: DeriveOptions): Promise<CryptoKey> {
if (!opts.secret) throw Error('Secret missing');
const passphraseKey = await (opts.format === 'jwk'
? crypto.subtle.importKey('jwk', opts.secret as JsonWebKey, 'PBKDF2', false, ['deriveKey'])
: crypto.subtle.importKey(
opts.format ?? 'raw',
secretToUint8Array(opts.secret as string | BufferSource),
'PBKDF2',
false,
['deriveKey', 'deriveBits']
)
);
const key = await crypto.subtle.deriveKey(
{
name: 'PBKDF2',
iterations: opts.iterations ?? 999,
hash: opts.hash ?? 'SHA-256',
salt: opts.salt
? bufferSourceToUint8Array(opts.salt)
: new Base64Decoder().decode('Gfw5ic5qS062JvoubvO+DA==')
},
passphraseKey,
{
name: 'AES-GCM',
length: opts.length ?? 256,
},
false,
['encrypt', 'decrypt'],
);
return key;
}
#store: CookieStore;
#keyring: readonly CryptoKey[];
#key: CryptoKey;
constructor(store: CookieStore, key: CryptoKey, opts: EncryptedCookieStoreOptions = {}) {
this.#store = store;
this.#key = key
this.#keyring = [key, ...opts.keyring ?? []];
}
get(name?: string): Promise<CookieListItem | null>;
get(options?: CookieStoreGetOptions): Promise<CookieListItem | null>;
async get(name?: string | CookieStoreGetOptions): Promise<CookieListItem | null> {
if (typeof name !== 'string') throw Error('Overload not implemented.');
const cookie = await this.#store.get(`${name}${EXT}`);
if (!cookie) return cookie;
// FIXME: empty values!
return this.#decrypt(cookie);
}
getAll(name?: string): Promise<CookieList>;
getAll(options?: CookieStoreGetOptions): Promise<CookieList>;
async getAll(options?: any) {
if (options != null) throw Error('Overload not implemented.');
const list: CookieList = [];
for (const cookie of await this.#store.getAll(options)) {
if (cookie.name.endsWith(EXT)) {
list.push(await this.#decrypt(cookie));
}
}
return list;
}
set(name: string, value: string): Promise<void>;
set(options: CookieInit): Promise<void>;
async set(options: string | CookieInit, value?: string) {
const [name, val] = typeof options === 'string'
? [options, value ?? '']
: [options.name, options.value ?? ''];
// FIXME: empty string!
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
const message = new TextEncoder().encode(val);
const cipher = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, this.#key, message);
const cipherB64 = new Base64Encoder({ url: true }).encode(concatBufferSources(iv, cipher));
return this.#store.set({
...typeof options === 'string' ? {} : options,
name: `${name}${EXT}`,
value: cipherB64,
});
}
delete(name: string): Promise<void>;
delete(options: CookieStoreDeleteOptions): Promise<void>;
delete(options: any) {
if (typeof options !== 'string') throw Error('Overload not implemented.');
return this.#store.delete(`${options}${EXT}`);
}
#decrypt = async (cookie: CookieListItem): Promise<CookieListItem> => {
const errors: any[] = [];
for (const key of this.#keyring) {
try {
const buffer = new Base64Decoder().decode(cookie.value);
const [iv, cipher] = splitBufferSource(buffer, IV_LENGTH);
const clearBuffer = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, cipher);
const clearText = new TextDecoder().decode(clearBuffer);
cookie.name = cookie.name.substring(0, cookie.name.length - EXT.length);
cookie.value = clearText;
return cookie;
} catch (err) {
errors.push(err);
}
}
throw new AggregateError(errors, 'None of the provided keys was able to decrypt the cookie.');
}
addEventListener(...args: Parameters<CookieStore['addEventListener']>): void {
return this.#store.addEventListener(...args);
}
dispatchEvent(event: Event): boolean {
return this.#store.dispatchEvent(event);
}
removeEventListener(...args: Parameters<CookieStore['removeEventListener']>): void {
return this.#store.removeEventListener(...args);
}
}