Skip to main content
Module

x/jwe_cookie_map/mod.ts

Easy JWE Cookies for Deno
Latest
import * as jweCookieMap from "https://deno.land/x/jwe_cookie_map@v1.0.1/mod.ts";

Provides a iterable map interfaces for managing JWE cookies server side. Similar to CookieMap

By default the JWECookieMap uses a automatically generated "RSA-OAEP-256" key pair in the DEFAULT_CONFIG, this value is not persisted between executions.

newCookieWithKeyPair is a helper functions to generate a JWECookieMap with a new set of automatically generated key pair.

However the recommended use of this library, is loading your key pair and creating a reusable instance of JWECookieConfiguration with your default cookie options.

Examples

You can easily set encrypted cookies (JWEs) on your response and get they decrypted payloads:

import { mergeHeaders } from "https://deno.land/std/http/cookie_map.ts";
import { JWECookieMap } from "https://deno.land/x/jwe_cookie_map/mod.ts";

const initialRequestHeader = new Headers();
const response = new Response("hello", {
  headers: { "content-type": "text/plain" },
});
const initialCookies = new JWECookieMap(initialRequestHeader, { response });
const payload = { foo: "bar" };
await initialCookies.setEncrypted("key", payload);

// The cookie set on the first request will come on the next request from the client
const nextRequestHeaders = new Headers();
const cookie = mergeHeaders(initialCookies).get("set-cookie");
nextRequestHeaders.set("Cookie", cookie!);
const nextCookies = new JWECookieMap(nextRequestHeaders);
console.log(await nextCookies.getDecrypted("key")); // Expects to log { foo: "bar" }

To access cookies not encrypted in a request and have any set keys available for creating a response:

import { mergeHeaders } from "https://deno.land/std/http/cookie_map.ts";
import { JWECookieMap } from "https://deno.land/x/jwe_cookie_map/mod.ts";

const request = new Request("https://localhost/", {
  headers: { "cookie": "foo=bar; bar=baz;" },
});

const cookies = new JWECookieMap(request, { secure: true });
console.log(cookies.get("foo")); // Expected to log "bar"
cookies.set("session", "1234567", { secure: true });
console.log(cookies.get("session")); // Expected to log undefined
const response = new Response("hello", {
  headers: mergeHeaders({
    "content-type": "text/plain",
  }, cookies),
});

If you have a Response or Headers for a response at construction of the cookies object, they can be passed and any set cookies will be added directly to the response headers:

import { JWECookieMap } from "https://deno.land/x/jwe_cookie_map/mod.ts";

const request = new Request("https://localhost/", {
  headers: { "cookie": "foo=bar; bar=baz;" },
});

const response = new Response("hello", {
  headers: { "content-type": "text/plain" },
});

const cookies = new JWECookieMap(request, { response });
console.log(cookies.get("foo")); // Expected to log "bar"
cookies.set("session", "1234567");
console.log(cookies.get("session")); // Expected to log undefined

Classes

Provides a way to configure the keys used for encryption, default options for cookies and a way to add more jose.EncryptJWT configurations.

Provides a way to manage encrypted cookies in a request and response on the server as a single iterable collection. Extends CookieMap.

Variables

The default JWECookieConfiguration used by new JWECookieMap instance, comes with a automatically generated "RSA-OAEP-256" key pair that is not persisted between executions.

The default encryptConfiguration used by JWECookieConfiguration will set a protected header with proper parameters to match the automatic key pair generation.

Functions

Generates a new "RSA-OAEP-256" key pair and return a new JWECookieConfiguration instance with other default values

Generates a new "RSA-OAEP-256" key pair and return a new JWECookieMap instance with other default values

Type Aliases

Alias type for defaultEncryptConfiguration and JWECookieMap.setEncrypted parameter encryptConfiguration.