@worker-tools/signed-cookie-store
Version:
A partial implementation of the Cookie Store API that transparently signs and verifies cookies via the Web Cryptography API.
54 lines (53 loc) • 2.27 kB
TypeScript
import type { CookieInit, CookieList, CookieListItem, CookieStore, CookieStoreDeleteOptions, CookieStoreGetOptions } from 'cookie-store-interface';
export * from 'cookie-store-interface';
export interface SignedCookieStoreOptions {
/**
* One or more crypto keys that were previously used to sign cookies.
* `SignedCookieStore` will try to verify the signature using these, but they are not used for signing.
*/
keyring?: readonly CryptoKey[];
}
export interface DeriveOptions {
secret: string | BufferSource | JsonWebKey;
salt?: BufferSource;
iterations?: number;
format?: KeyFormat;
hash?: HashAlgorithmIdentifier;
hmacHash?: HashAlgorithmIdentifier;
length?: number;
}
/**
* # Signed Cookie Store
* A partial implementation of the [Cookie Store API](https://wicg.github.io/cookie-store)
* that transparently signs and verifies cookies via the Web Cryptography API.
*
* This is likely only useful in server-side implementations,
* but written in a platform-agnostic way.
*/
export declare class SignedCookieStore implements CookieStore {
#private;
/**
* A helper function to derive a crypto key from a passphrase.
*/
static deriveCryptoKey(opts: DeriveOptions): Promise<CryptoKey>;
constructor(store: CookieStore, key: CryptoKey, opts?: SignedCookieStoreOptions);
/**
* @throws if the signature doesn't match.
* @returns null when the signature cookie is missing.
*/
get(name?: string): Promise<CookieListItem | null>;
get(options?: CookieStoreGetOptions): Promise<CookieListItem | null>;
/**
* @throws if any signature doesn't match.
* @returns A list of cookies, exclusive of all cookies without signatures
*/
getAll(name?: string): Promise<CookieList>;
getAll(options?: CookieStoreGetOptions): Promise<CookieList>;
set(name: string, value: string): Promise<void>;
set(options: CookieInit): Promise<void>;
delete(name: string): Promise<void>;
delete(options: CookieStoreDeleteOptions): Promise<void>;
addEventListener(...args: Parameters<CookieStore['addEventListener']>): void;
dispatchEvent(event: Event): boolean;
removeEventListener(...args: Parameters<CookieStore['removeEventListener']>): void;
}