@cardonjs/access-tokens 0.0.0-stage → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +447 -2
- package/dist/duration.d.ts +11 -0
- package/dist/duration.d.ts.map +1 -0
- package/dist/duration.js +21 -0
- package/dist/duration.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.d.ts +53 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +155 -0
- package/dist/middleware.js.map +1 -0
- package/dist/security-scheme.d.ts +12 -0
- package/dist/security-scheme.d.ts.map +1 -0
- package/dist/security-scheme.js +42 -0
- package/dist/security-scheme.js.map +1 -0
- package/dist/store.d.ts +58 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +2 -0
- package/dist/store.js.map +1 -0
- package/dist/testing.d.ts +7 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +66 -0
- package/dist/testing.js.map +1 -0
- package/dist/tokens.d.ts +74 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +146 -0
- package/dist/tokens.js.map +1 -0
- package/dist/validation.d.ts +36 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +122 -0
- package/dist/validation.js.map +1 -0
- package/llms.txt +139 -0
- package/package.json +52 -3
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { documentMiddleware, ServiceUnavailableError } from "@cardonjs/core";
|
|
2
|
+
import { HTTPException } from "hono/http-exception";
|
|
3
|
+
import { ACCESS_TOKEN_SECURITY_SCHEME, accessTokenSecurityScheme, checkHeaderName, } from "./security-scheme.js";
|
|
4
|
+
import { copyResource, isResource, isScope } from "./validation.js";
|
|
5
|
+
const WHERE = "requireAccessToken";
|
|
6
|
+
const AUTHORIZATION = "authorization";
|
|
7
|
+
/** Exactly one credential after the (case-insensitive) scheme, separated by spaces only. */
|
|
8
|
+
const BEARER = /^Bearer +([^\s,]+) *$/i;
|
|
9
|
+
/** Spaces and tabs around a custom header's value (`Headers` strips them already). */
|
|
10
|
+
const HTTP_WHITESPACE = /^[ \t]+|[ \t]+$/g;
|
|
11
|
+
/** The only message a rejected token gets: it never says why. */
|
|
12
|
+
const MESSAGE = "A valid access token is required.";
|
|
13
|
+
function failureName(error) {
|
|
14
|
+
if (error instanceof Error && typeof error.name === "string")
|
|
15
|
+
return error.name.slice(0, 100);
|
|
16
|
+
return typeof error;
|
|
17
|
+
}
|
|
18
|
+
function checkSelector(selector) {
|
|
19
|
+
if (typeof selector === "function")
|
|
20
|
+
return;
|
|
21
|
+
if (typeof selector === "object" && selector !== null) {
|
|
22
|
+
const { type, param } = selector;
|
|
23
|
+
if (isResource({ type, id: "x" }) &&
|
|
24
|
+
typeof param === "string" &&
|
|
25
|
+
/^[A-Za-z_][A-Za-z0-9_]*$/.test(param)) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
throw new TypeError(`${WHERE}: resource must be { type, param } (a valid resource type and a path parameter ` +
|
|
30
|
+
"name) or a function of the context returning { type, id }.");
|
|
31
|
+
}
|
|
32
|
+
function checkScopeOption(scope) {
|
|
33
|
+
const scopes = typeof scope === "string" ? [scope] : scope;
|
|
34
|
+
if (!Array.isArray(scopes) || scopes.length === 0 || !scopes.every(isScope)) {
|
|
35
|
+
throw new TypeError(`${WHERE}: scope must be a scope string such as "order:read", or a non-empty array of them.`);
|
|
36
|
+
}
|
|
37
|
+
return Object.freeze([...new Set(scopes)]);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Authorises a route with an access token from `tokens` (an instance, or a function of the
|
|
41
|
+
* context such as `(c) => c.var.adapters.tokens`). The token is read only from one header
|
|
42
|
+
* (default `Authorization: Bearer <token>`), never from the query string or the path.
|
|
43
|
+
*
|
|
44
|
+
* Every failure (no token, a malformed, unknown, expired or revoked one, a token for another
|
|
45
|
+
* resource or without the scope) answers the same `401 UNAUTHORIZED` envelope with a
|
|
46
|
+
* `WWW-Authenticate` header; the reason is logged at `info`, the token never. A store failure
|
|
47
|
+
* answers `503`. On success `c.var.accessToken` is the grant (the record without its hash). List the route as public in
|
|
48
|
+
* `requireAuth`: this middleware is its authorisation. Throws a `TypeError` at creation for
|
|
49
|
+
* invalid options.
|
|
50
|
+
*/
|
|
51
|
+
export function requireAccessToken(tokens, options) {
|
|
52
|
+
if (typeof tokens !== "function" && (typeof tokens !== "object" || tokens === null)) {
|
|
53
|
+
throw new TypeError(`${WHERE}: tokens must be an AccessTokens instance or a function of the context.`);
|
|
54
|
+
}
|
|
55
|
+
if (typeof options !== "object" || options === null) {
|
|
56
|
+
throw new TypeError(`${WHERE}: options must be an object { resource, scope }.`);
|
|
57
|
+
}
|
|
58
|
+
checkSelector(options.resource);
|
|
59
|
+
const scopes = checkScopeOption(options.scope);
|
|
60
|
+
const header = options.header ?? "Authorization";
|
|
61
|
+
checkHeaderName(WHERE, header);
|
|
62
|
+
const bearer = header.toLowerCase() === AUTHORIZATION;
|
|
63
|
+
if (options.optional !== undefined && typeof options.optional !== "boolean") {
|
|
64
|
+
throw new TypeError(`${WHERE}: optional must be a boolean.`);
|
|
65
|
+
}
|
|
66
|
+
const optional = options.optional === true;
|
|
67
|
+
const selector = options.resource;
|
|
68
|
+
const challenge = bearer ? "Bearer" : `AccessToken header="${header}"`;
|
|
69
|
+
const middleware = async (c, next) => {
|
|
70
|
+
const reject = (reason, tokenId) => {
|
|
71
|
+
c.var.logger?.info("access token rejected", {
|
|
72
|
+
reason,
|
|
73
|
+
...(tokenId === undefined ? {} : { tokenId }),
|
|
74
|
+
});
|
|
75
|
+
throw new HTTPException(401, {
|
|
76
|
+
message: MESSAGE,
|
|
77
|
+
res: new Response(null, { status: 401, headers: { "WWW-Authenticate": challenge } }),
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
const instance = typeof tokens === "function" ? tokens(c) : tokens;
|
|
81
|
+
const raw = c.req.header(header);
|
|
82
|
+
let token;
|
|
83
|
+
if (raw !== undefined && raw !== "") {
|
|
84
|
+
if (bearer) {
|
|
85
|
+
// On an optional route, a value that never mentions `<prefix>_` is another kind of
|
|
86
|
+
// credential (a session bearer, Basic, ...): no token. Anything that does must parse
|
|
87
|
+
// strictly as one bearer token, so a mangled token (a tab, a no-break space, joined
|
|
88
|
+
// duplicate headers, another scheme) is rejected, never served anonymously.
|
|
89
|
+
if (!optional || raw.includes(`${instance.prefix}_`)) {
|
|
90
|
+
token = BEARER.exec(raw)?.[1];
|
|
91
|
+
if (token === undefined)
|
|
92
|
+
reject("invalid");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// The whole value is the token: only spaces and tabs are trimmed, so a no-break space
|
|
97
|
+
// or a comma-joined duplicate stays in it and fails verification.
|
|
98
|
+
token = raw.replace(HTTP_WHITESPACE, "");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (token === undefined || token === "") {
|
|
102
|
+
if (optional) {
|
|
103
|
+
c.set("accessToken", undefined);
|
|
104
|
+
await next();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
reject("missing");
|
|
108
|
+
}
|
|
109
|
+
let verified;
|
|
110
|
+
try {
|
|
111
|
+
verified = await instance.verify(token);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
c.var.logger?.error("access token store failed", { error: failureName(error) });
|
|
115
|
+
throw new ServiceUnavailableError();
|
|
116
|
+
}
|
|
117
|
+
if (!verified.ok) {
|
|
118
|
+
reject(verified.reason);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const record = verified.record;
|
|
122
|
+
let resource;
|
|
123
|
+
if (typeof selector === "function") {
|
|
124
|
+
// Copied once, so the resource that is checked is the one compared.
|
|
125
|
+
resource = copyResource(await selector(c));
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
resource = { type: selector.type, id: c.req.param(selector.param) };
|
|
129
|
+
}
|
|
130
|
+
if (!isResource(resource)) {
|
|
131
|
+
// A route parameter that cannot be a resource id (or a selector bug): no token matches it.
|
|
132
|
+
reject("resource", record.id);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (record.resourceType !== resource.type || record.resourceId !== resource.id) {
|
|
136
|
+
reject("resource", record.id);
|
|
137
|
+
}
|
|
138
|
+
if (!scopes.every((scope) => record.scopes.includes(scope)))
|
|
139
|
+
reject("scope", record.id);
|
|
140
|
+
c.set("accessToken", record);
|
|
141
|
+
await next();
|
|
142
|
+
};
|
|
143
|
+
documentMiddleware(middleware, optional
|
|
144
|
+
? // Served without a credential too: the guard's own status decides the route's security.
|
|
145
|
+
// Declare `security` on the route to list the token as an alternative.
|
|
146
|
+
{ errors: ["UNAUTHORIZED", "SERVICE_UNAVAILABLE"] }
|
|
147
|
+
: {
|
|
148
|
+
errors: ["UNAUTHORIZED", "SERVICE_UNAVAILABLE"],
|
|
149
|
+
security: {
|
|
150
|
+
schemes: { [ACCESS_TOKEN_SECURITY_SCHEME]: accessTokenSecurityScheme(header) },
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
return middleware;
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAe,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAE1F,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EACL,4BAA4B,EAC5B,yBAAyB,EACzB,eAAe,GAChB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAsCpE,MAAM,KAAK,GAAG,oBAAoB,CAAC;AACnC,MA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,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACtE,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC1B,2FAA2F;YAC3F,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC;YAC9B,OAAO;QACT,CAAC;QACD,IAAI,MAAM,CAAC,YAAY,KAAK,QAAQ,CAAC,IAAI,IAAI,MAAM,CAAC,UAAU,KAAK,QAAQ,CAAC,EAAE,EAAE,CAAC;YAC/E,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC;QAChC,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;YAAE,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC;QAExF,CAAC,CAAC,GAAG,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC;QAC7B,MAAM,IAAI,EAAE,CAAC;IACf,CAAC,CAAC;IAEF,kBAAkB,CAChB,UAAU,EACV,QAAQ;QACN,CAAC,CAAC,wFAAwF;YACxF,uEAAuE;YACvE,EAAE,MAAM,EAAE,CAAC,cAAc,EAAE,qBAAqB,CAAC,EAAE;QACrD,CAAC,CAAC;YACE,MAAM,EAAE,CAAC,cAAc,EAAE,qBAAqB,CAAC;YAC/C,QAAQ,EAAE;gBACR,OAAO,EAAE,EAAE,CAAC,4BAA4B,CAAC,EAAE,yBAAyB,CAAC,MAAM,CAAC,EAAE;aAC/E;SACF,CACN,CAAC;IACF,OAAO,UAAmF,CAAC;AAC7F,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SecuritySchemeObject } from "@cardonjs/core";
|
|
2
|
+
/** The name `requireAccessToken` registers its scheme under in `components.securitySchemes`. */
|
|
3
|
+
export declare const ACCESS_TOKEN_SECURITY_SCHEME = "accessToken";
|
|
4
|
+
/** Throws a `TypeError` unless `name` is a header name a token may travel in. */
|
|
5
|
+
export declare function checkHeaderName(where: string, name: unknown): asserts name is string;
|
|
6
|
+
/**
|
|
7
|
+
* The OpenAPI security scheme of routes that take an access token: HTTP bearer for the default
|
|
8
|
+
* `Authorization` header, or an API key in the custom `header`. `requireAccessToken` registers
|
|
9
|
+
* it on its own (as `accessToken`); use this to declare it elsewhere, e.g. on an optional route.
|
|
10
|
+
*/
|
|
11
|
+
export declare function accessTokenSecurityScheme(header?: string): SecuritySchemeObject;
|
|
12
|
+
//# sourceMappingURL=security-scheme.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"security-scheme.d.ts","sourceRoot":"","sources":["../src/security-scheme.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAE3D,gGAAgG;AAChG,eAAO,MAAM,4BAA4B,gBAAgB,CAAC;AAc1D,iFAAiF;AACjF,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,IAAI,MAAM,CAOpF;AAMD;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,MAAM,SAAkB,GAAG,oBAAoB,CAWxF"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** The name `requireAccessToken` registers its scheme under in `components.securitySchemes`. */
|
|
2
|
+
export const ACCESS_TOKEN_SECURITY_SCHEME = "accessToken";
|
|
3
|
+
/** An HTTP header name (RFC 9110 token). */
|
|
4
|
+
const HEADER_NAME = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
|
|
5
|
+
/** Headers that carry other credentials or that clients and proxies manage. */
|
|
6
|
+
const FORBIDDEN_HEADERS = new Set([
|
|
7
|
+
"cookie",
|
|
8
|
+
"set-cookie",
|
|
9
|
+
"proxy-authorization",
|
|
10
|
+
"host",
|
|
11
|
+
"content-type",
|
|
12
|
+
"content-length",
|
|
13
|
+
]);
|
|
14
|
+
/** Throws a `TypeError` unless `name` is a header name a token may travel in. */
|
|
15
|
+
export function checkHeaderName(where, name) {
|
|
16
|
+
if (typeof name !== "string" || name.length > 64 || !HEADER_NAME.test(name)) {
|
|
17
|
+
throw new TypeError(`${where}: header must be a header name, e.g. "X-Access-Token".`);
|
|
18
|
+
}
|
|
19
|
+
if (FORBIDDEN_HEADERS.has(name.toLowerCase())) {
|
|
20
|
+
throw new TypeError(`${where}: header may not be ${name}.`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const DESCRIPTION = "An access token for one resource (`<prefix>_` and 43 base64url characters), as issued by " +
|
|
24
|
+
"the API. Never send it in a URL.";
|
|
25
|
+
/**
|
|
26
|
+
* The OpenAPI security scheme of routes that take an access token: HTTP bearer for the default
|
|
27
|
+
* `Authorization` header, or an API key in the custom `header`. `requireAccessToken` registers
|
|
28
|
+
* it on its own (as `accessToken`); use this to declare it elsewhere, e.g. on an optional route.
|
|
29
|
+
*/
|
|
30
|
+
export function accessTokenSecurityScheme(header = "Authorization") {
|
|
31
|
+
checkHeaderName("accessTokenSecurityScheme", header);
|
|
32
|
+
if (header.toLowerCase() === "authorization") {
|
|
33
|
+
return Object.freeze({
|
|
34
|
+
type: "http",
|
|
35
|
+
scheme: "bearer",
|
|
36
|
+
bearerFormat: "access token",
|
|
37
|
+
description: DESCRIPTION,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return Object.freeze({ type: "apiKey", in: "header", name: header, description: DESCRIPTION });
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=security-scheme.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"security-scheme.js","sourceRoot":"","sources":["../src/security-scheme.ts"],"names":[],"mappings":"AAEA,gGAAgG;AAChG,MAAM,CAAC,MAAM,4BAA4B,GAAG,aAAa,CAAC;AAE1D,4CAA4C;AAC5C,MAAM,WAA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|
|
|
1
|
+
/** The resource a token grants access to, e.g. `{ type: "order", id: "019a…" }`. */
|
|
2
|
+
export interface AccessTokenResource {
|
|
3
|
+
/** Lowercase letters, digits, `_`, `.` and `-`, starting with a letter; 1 to 64 characters. */
|
|
4
|
+
readonly type: string;
|
|
5
|
+
/** 1 to 255 printable ASCII characters (U+0021–U+007E), no spaces. */
|
|
6
|
+
readonly id: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* What a verified or issued token grants, as `issue`, `verify` and `c.var.accessToken` expose it:
|
|
10
|
+
* the record without its hash. No personal data. Dates have millisecond precision.
|
|
11
|
+
*/
|
|
12
|
+
export interface AccessTokenGrant {
|
|
13
|
+
/** A UUIDv7 from core's `newId`. */
|
|
14
|
+
readonly id: string;
|
|
15
|
+
readonly resourceType: string;
|
|
16
|
+
readonly resourceId: string;
|
|
17
|
+
/** Non-empty, without duplicates, in the order they were issued. */
|
|
18
|
+
readonly scopes: readonly string[];
|
|
19
|
+
readonly createdAt: Date;
|
|
20
|
+
/** Always set: no token lives forever. */
|
|
21
|
+
readonly expiresAt: Date;
|
|
22
|
+
/** Set once, by the first revocation. */
|
|
23
|
+
readonly revokedAt?: Date;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A token as the store keeps it: the grant plus the SHA-256 of the token, never the token
|
|
27
|
+
* itself. Only stores see it; `issue`, `verify` and the middleware hand out the
|
|
28
|
+
* {@link AccessTokenGrant}.
|
|
29
|
+
*/
|
|
30
|
+
export interface AccessTokenRecord extends AccessTokenGrant {
|
|
31
|
+
/** Lowercase hex SHA-256 of the whole token (UTF-8), 64 characters. */
|
|
32
|
+
readonly tokenHash: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Keeps the access token records. Small on purpose; every store passes
|
|
36
|
+
* `accessTokenStoreContract` from `@cardonjs/testing`. A store only keeps and finds records:
|
|
37
|
+
* whether a token is expired, revoked or for the right resource is decided by `verify` and the
|
|
38
|
+
* middleware, never by the store.
|
|
39
|
+
*/
|
|
40
|
+
export interface AccessTokenStore {
|
|
41
|
+
/** Adds a record. Rejects when a record with the same `id` or `tokenHash` exists. */
|
|
42
|
+
insert(record: AccessTokenRecord): Promise<void>;
|
|
43
|
+
/** The record with this hash, expired and revoked ones included, or `undefined`. */
|
|
44
|
+
findByHash(tokenHash: string): Promise<AccessTokenRecord | undefined>;
|
|
45
|
+
/**
|
|
46
|
+
* Sets `revokedAt` on the record with this id unless it is already revoked. Resolves whether
|
|
47
|
+
* it did (`false` for an unknown or already revoked record).
|
|
48
|
+
*/
|
|
49
|
+
revoke(id: string, at: Date): Promise<boolean>;
|
|
50
|
+
/**
|
|
51
|
+
* Sets `revokedAt` on every record of the resource that is not revoked yet (exact match on
|
|
52
|
+
* both type and id). Resolves how many it revoked.
|
|
53
|
+
*/
|
|
54
|
+
revokeForResource(resourceType: string, resourceId: string, at: Date): Promise<number>;
|
|
55
|
+
/** Deletes every record whose `expiresAt` is at or before `before`. Resolves how many. */
|
|
56
|
+
purgeExpired(before: Date): Promise<number>;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,MAAM,WAAW,mBAAmB;IAClC,+FAA+F;IAC/F,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,sEAAsE;IACtE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,oCAAoC;IACpC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,oEAAoE;IACpE,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC;IACzB,0CAA0C;IAC1C,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC;IACzB,yCAAyC;IACzC,QAAQ,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC;CAC3B;AAED;;;;GAIG;AACH,MAAM,WAAW,iBAAkB,SAAQ,gBAAgB;IACzD,uEAAuE;IACvE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC/B,qFAAqF;IACrF,MAAM,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACjD,oFAAoF;IACpF,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,GAAG,SAAS,CAAC,CAAC;IACtE;;;OAGG;IACH,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC/C;;;OAGG;IACH,iBAAiB,CAAC,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACvF,0FAA0F;IAC1F,YAAY,CAAC,MAAM,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAC7C"}
|
package/dist/store.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { AccessTokenStore } from "./store.js";
|
|
2
|
+
/**
|
|
3
|
+
* An {@link AccessTokenStore} in memory, for tests and local development only (per isolate, lost
|
|
4
|
+
* on restart). Keeps copies: changing a record after `insert` or `findByHash` changes nothing.
|
|
5
|
+
*/
|
|
6
|
+
export declare function memoryAccessTokenStore(): AccessTokenStore;
|
|
7
|
+
//# sourceMappingURL=testing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAqB,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAetE;;;GAGG;AACH,wBAAgB,sBAAsB,IAAI,gBAAgB,CAuDzD"}
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
function copy(record) {
|
|
2
|
+
return {
|
|
3
|
+
id: record.id,
|
|
4
|
+
tokenHash: record.tokenHash,
|
|
5
|
+
resourceType: record.resourceType,
|
|
6
|
+
resourceId: record.resourceId,
|
|
7
|
+
scopes: [...record.scopes],
|
|
8
|
+
createdAt: new Date(record.createdAt.getTime()),
|
|
9
|
+
expiresAt: new Date(record.expiresAt.getTime()),
|
|
10
|
+
...(record.revokedAt === undefined ? {} : { revokedAt: new Date(record.revokedAt.getTime()) }),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* An {@link AccessTokenStore} in memory, for tests and local development only (per isolate, lost
|
|
15
|
+
* on restart). Keeps copies: changing a record after `insert` or `findByHash` changes nothing.
|
|
16
|
+
*/
|
|
17
|
+
export function memoryAccessTokenStore() {
|
|
18
|
+
const byHash = new Map();
|
|
19
|
+
const hashById = new Map();
|
|
20
|
+
return {
|
|
21
|
+
async insert(record) {
|
|
22
|
+
if (byHash.has(record.tokenHash) || hashById.has(record.id)) {
|
|
23
|
+
throw new Error("memoryAccessTokenStore: a record with this id or token hash exists.");
|
|
24
|
+
}
|
|
25
|
+
byHash.set(record.tokenHash, copy(record));
|
|
26
|
+
hashById.set(record.id, record.tokenHash);
|
|
27
|
+
},
|
|
28
|
+
async findByHash(tokenHash) {
|
|
29
|
+
const record = byHash.get(tokenHash);
|
|
30
|
+
return record === undefined ? undefined : copy(record);
|
|
31
|
+
},
|
|
32
|
+
async revoke(id, at) {
|
|
33
|
+
const hash = hashById.get(id);
|
|
34
|
+
const record = hash === undefined ? undefined : byHash.get(hash);
|
|
35
|
+
if (hash === undefined || record === undefined || record.revokedAt !== undefined) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
byHash.set(hash, { ...record, revokedAt: new Date(at.getTime()) });
|
|
39
|
+
return true;
|
|
40
|
+
},
|
|
41
|
+
async revokeForResource(resourceType, resourceId, at) {
|
|
42
|
+
let revoked = 0;
|
|
43
|
+
for (const [hash, record] of byHash) {
|
|
44
|
+
if (record.resourceType === resourceType &&
|
|
45
|
+
record.resourceId === resourceId &&
|
|
46
|
+
record.revokedAt === undefined) {
|
|
47
|
+
byHash.set(hash, { ...record, revokedAt: new Date(at.getTime()) });
|
|
48
|
+
revoked++;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return revoked;
|
|
52
|
+
},
|
|
53
|
+
async purgeExpired(before) {
|
|
54
|
+
let removed = 0;
|
|
55
|
+
for (const [hash, record] of byHash) {
|
|
56
|
+
if (record.expiresAt.getTime() <= before.getTime()) {
|
|
57
|
+
byHash.delete(hash);
|
|
58
|
+
hashById.delete(record.id);
|
|
59
|
+
removed++;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return removed;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=testing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.js","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAEA,SAAS,IAAI,CAAC,MAAyB;IACrC,OAAO;QACL,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,SAAS,EAAE,MAAM,CAAC,SAAS;QAC3B,YAAY,EAAE,MAAM,CAAC,YAAY;QACjC,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,MAAM,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;QAC1B,SAAS,EAAE,IAAI,IAAI,CAAC,MAAM,CA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AC,CAAC;oBACpB,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;oBAC3B,OAAO,EAAE,CAAC;gBACZ,CAAC;YACH,CAAC;YACD,OAAO,OAAO,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/tokens.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type Duration } from "./duration.js";
|
|
2
|
+
import type { AccessTokenGrant, AccessTokenResource, AccessTokenStore } from "./store.js";
|
|
3
|
+
export interface AccessTokensOptions {
|
|
4
|
+
/** Where the records are kept, e.g. `d1AccessTokenStore(db.client)`. */
|
|
5
|
+
readonly store: AccessTokenStore;
|
|
6
|
+
/**
|
|
7
|
+
* Starts every token, before an `_`: lowercase letters and digits, 2 to 12 characters.
|
|
8
|
+
* Default: `"cat"`. A recognisable prefix lets secret scanners and log filters spot leaks.
|
|
9
|
+
*/
|
|
10
|
+
readonly prefix?: string;
|
|
11
|
+
/** Lifetime of a token issued without `ttl`. Default: `"1d"`, or `maxTtl` when shorter. */
|
|
12
|
+
readonly defaultTtl?: Duration;
|
|
13
|
+
/** Longest lifetime `issue` accepts, at most `"366d"`. Default: `"30d"`. */
|
|
14
|
+
readonly maxTtl?: Duration;
|
|
15
|
+
/** The clock. Default: `() => new Date()`. */
|
|
16
|
+
readonly now?: () => Date;
|
|
17
|
+
}
|
|
18
|
+
export interface IssueAccessTokenInput {
|
|
19
|
+
readonly resource: AccessTokenResource;
|
|
20
|
+
/** What the token allows, e.g. `["order:read", "order:cancel"]`. Non-empty, at most 32. */
|
|
21
|
+
readonly scopes: readonly string[];
|
|
22
|
+
/** At most `maxTtl`. Default: `defaultTtl`. */
|
|
23
|
+
readonly ttl?: Duration;
|
|
24
|
+
}
|
|
25
|
+
export interface IssuedAccessToken {
|
|
26
|
+
/** The token. Returned once, here: hand it to its holder and never log or store it. */
|
|
27
|
+
readonly token: string;
|
|
28
|
+
/** What it grants (the stored record without its hash). */
|
|
29
|
+
readonly record: AccessTokenGrant;
|
|
30
|
+
}
|
|
31
|
+
/** Why a token was not accepted. Internal: clients always get the same `401`. */
|
|
32
|
+
export type AccessTokenFailure = "missing" | "invalid" | "expired" | "revoked";
|
|
33
|
+
export type AccessTokenVerification = {
|
|
34
|
+
readonly ok: true;
|
|
35
|
+
readonly record: AccessTokenGrant;
|
|
36
|
+
} | {
|
|
37
|
+
readonly ok: false;
|
|
38
|
+
readonly reason: AccessTokenFailure;
|
|
39
|
+
};
|
|
40
|
+
export interface PurgeExpiredOptions {
|
|
41
|
+
/** Keep records that expired less than this long ago. Default: none kept. */
|
|
42
|
+
readonly olderThan?: Duration;
|
|
43
|
+
}
|
|
44
|
+
/** Issues, verifies and revokes access tokens over one store. */
|
|
45
|
+
export interface AccessTokens {
|
|
46
|
+
/** The prefix every token of this instance starts with (before the `_`). */
|
|
47
|
+
readonly prefix: string;
|
|
48
|
+
/**
|
|
49
|
+
* A new token for one resource. Throws a `TypeError` for an invalid resource, scopes or ttl
|
|
50
|
+
* (above `maxTtl` included); rejects when the store does.
|
|
51
|
+
*/
|
|
52
|
+
issue(input: IssueAccessTokenInput): Promise<IssuedAccessToken>;
|
|
53
|
+
/**
|
|
54
|
+
* Whether `token` is a live token of this instance: `missing` (not a non-empty string),
|
|
55
|
+
* `invalid` (wrong format or prefix, or unknown), `revoked` or `expired`. Only a well-formed
|
|
56
|
+
* token reaches the store. Rejects when the store fails or returns a malformed record.
|
|
57
|
+
*/
|
|
58
|
+
verify(token: unknown): Promise<AccessTokenVerification>;
|
|
59
|
+
/** Revokes one token by its record id. Resolves whether it was revoked now. */
|
|
60
|
+
revoke(id: string): Promise<boolean>;
|
|
61
|
+
/** Revokes every token of the resource. Resolves how many were revoked now. */
|
|
62
|
+
revokeForResource(resource: AccessTokenResource): Promise<number>;
|
|
63
|
+
/** Deletes expired records (for an hourly cron). Resolves how many. */
|
|
64
|
+
purgeExpired(options?: PurgeExpiredOptions): Promise<number>;
|
|
65
|
+
}
|
|
66
|
+
/** Lowercase hex SHA-256 of the token (UTF-8). */
|
|
67
|
+
export declare function hashAccessToken(token: string): Promise<string>;
|
|
68
|
+
/**
|
|
69
|
+
* Access tokens over `store`: opaque `<prefix>_<43 base64url characters>` strings (32 bytes from
|
|
70
|
+
* `crypto.getRandomValues`), of which only the SHA-256 is stored. Every token expires
|
|
71
|
+
* (`ttl` ≤ `maxTtl`) and can be revoked. Synchronous; throws a `TypeError` for invalid options.
|
|
72
|
+
*/
|
|
73
|
+
export declare function accessTokens(options: AccessTokensOptions): AccessTokens;
|
|
74
|
+
//# sourceMappingURL=tokens.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.d.ts","sourceRoot":"","sources":["../src/tokens.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,QAAQ,EAAmB,MAAM,eAAe,CAAC;AAC/D,OAAO,KAAK,EACV,gBAAgB,EAEhB,mBAAmB,EACnB,gBAAgB,EACjB,MAAM,YAAY,CAAC;AAqBpB,MAAM,WAAW,mBAAmB;IAClC,wEAAwE;IACxE,QAAQ,CAAC,KAAK,EAAE,gBAAgB,CAAC;IACjC;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,2FAA2F;IAC3F,QAAQ,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC;IAC/B,4EAA4E;IAC5E,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC;IAC3B,8CAA8C;IAC9C,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,IAAI,CAAC;CAC3B;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,QAAQ,EAAE,mBAAmB,CAAC;IACvC,2FAA2F;IAC3F,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,+CAA+C;IAC/C,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC;CACzB;AAED,MAAM,WAAW,iB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AIG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,YAAY,CAwGvE"}
|
package/dist/tokens.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { newId, timingSafeEqual } from "@cardonjs/core";
|
|
2
|
+
import { durationSeconds } from "./duration.js";
|
|
3
|
+
import { assertResource, checkScopes, copyResource, DEFAULT_PREFIX, PREFIX, TOKEN_BYTES, TOKEN_SECRET_LENGTH, toGrant, toRecord, } from "./validation.js";
|
|
4
|
+
const WHERE = "accessTokens";
|
|
5
|
+
/** Longest `maxTtl` accepted: 366 days. */
|
|
6
|
+
const MAX_TTL_LIMIT_SECONDS = 366 * 86_400;
|
|
7
|
+
const DEFAULT_MAX_TTL_SECONDS = 30 * 86_400;
|
|
8
|
+
const DEFAULT_TTL_SECONDS = 86_400;
|
|
9
|
+
/** Longest `olderThan` accepted by `purgeExpired`: about ten years. */
|
|
10
|
+
const MAX_KEEP_SECONDS = 3_660 * 86_400;
|
|
11
|
+
function base64url(bytes) {
|
|
12
|
+
let binary = "";
|
|
13
|
+
for (const byte of bytes)
|
|
14
|
+
binary += String.fromCharCode(byte);
|
|
15
|
+
return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
|
|
16
|
+
}
|
|
17
|
+
/** Lowercase hex SHA-256 of the token (UTF-8). */
|
|
18
|
+
export async function hashAccessToken(token) {
|
|
19
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(token)));
|
|
20
|
+
let hex = "";
|
|
21
|
+
for (const byte of digest)
|
|
22
|
+
hex += byte.toString(16).padStart(2, "0");
|
|
23
|
+
return hex;
|
|
24
|
+
}
|
|
25
|
+
function ttlSeconds(field, value, max) {
|
|
26
|
+
const seconds = durationSeconds(value);
|
|
27
|
+
if (seconds === undefined) {
|
|
28
|
+
throw new TypeError(`${WHERE}: ${field} must be a positive whole number of seconds or a string such as "15m", ` +
|
|
29
|
+
'"12h" or "7d".');
|
|
30
|
+
}
|
|
31
|
+
if (seconds > max) {
|
|
32
|
+
throw new TypeError(`${WHERE}: ${field} must be at most ${max} seconds.`);
|
|
33
|
+
}
|
|
34
|
+
return seconds;
|
|
35
|
+
}
|
|
36
|
+
function currentTime(now) {
|
|
37
|
+
const value = now();
|
|
38
|
+
if (!(value instanceof Date) || Number.isNaN(value.getTime())) {
|
|
39
|
+
throw new TypeError(`${WHERE}: now must return a valid Date.`);
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Access tokens over `store`: opaque `<prefix>_<43 base64url characters>` strings (32 bytes from
|
|
45
|
+
* `crypto.getRandomValues`), of which only the SHA-256 is stored. Every token expires
|
|
46
|
+
* (`ttl` ≤ `maxTtl`) and can be revoked. Synchronous; throws a `TypeError` for invalid options.
|
|
47
|
+
*/
|
|
48
|
+
export function accessTokens(options) {
|
|
49
|
+
if (typeof options !== "object" || options === null) {
|
|
50
|
+
throw new TypeError(`${WHERE}: options must be an object, e.g. \`{ store }\`.`);
|
|
51
|
+
}
|
|
52
|
+
const { store } = options;
|
|
53
|
+
if (typeof store !== "object" ||
|
|
54
|
+
store === null ||
|
|
55
|
+
!["insert", "findByHash", "revoke", "revokeForResource", "purgeExpired"].every((method) => typeof store[method] === "function")) {
|
|
56
|
+
throw new TypeError(`${WHERE}: store must be an AccessTokenStore.`);
|
|
57
|
+
}
|
|
58
|
+
const prefix = options.prefix === undefined ? DEFAULT_PREFIX : options.prefix;
|
|
59
|
+
if (typeof prefix !== "string" || !PREFIX.test(prefix)) {
|
|
60
|
+
throw new TypeError(`${WHERE}: prefix must be 2 to 12 lowercase letters or digits.`);
|
|
61
|
+
}
|
|
62
|
+
const maxTtl = options.maxTtl === undefined
|
|
63
|
+
? DEFAULT_MAX_TTL_SECONDS
|
|
64
|
+
: ttlSeconds("maxTtl", options.maxTtl, MAX_TTL_LIMIT_SECONDS);
|
|
65
|
+
const defaultTtl = options.defaultTtl === undefined
|
|
66
|
+
? Math.min(DEFAULT_TTL_SECONDS, maxTtl)
|
|
67
|
+
: ttlSeconds("defaultTtl", options.defaultTtl, maxTtl);
|
|
68
|
+
const now = options.now ?? (() => new Date());
|
|
69
|
+
if (typeof now !== "function")
|
|
70
|
+
throw new TypeError(`${WHERE}: now must be a function.`);
|
|
71
|
+
const tokenPattern = new RegExp(`^${prefix}_[A-Za-z0-9_-]{${TOKEN_SECRET_LENGTH}}$`);
|
|
72
|
+
return Object.freeze({
|
|
73
|
+
prefix,
|
|
74
|
+
async issue(input) {
|
|
75
|
+
if (typeof input !== "object" || input === null) {
|
|
76
|
+
throw new TypeError(`${WHERE}: issue takes { resource, scopes, ttl? }.`);
|
|
77
|
+
}
|
|
78
|
+
// Each input field is read once: the copy is validated and the copy is stored.
|
|
79
|
+
const { resource: givenResource, scopes: givenScopes, ttl: givenTtl } = input;
|
|
80
|
+
const resource = copyResource(givenResource);
|
|
81
|
+
assertResource(`${WHERE}.issue`, resource);
|
|
82
|
+
const scopes = checkScopes(`${WHERE}.issue`, givenScopes);
|
|
83
|
+
const ttl = givenTtl === undefined ? defaultTtl : ttlSeconds("ttl", givenTtl, maxTtl);
|
|
84
|
+
const createdAt = currentTime(now);
|
|
85
|
+
const secret = base64url(crypto.getRandomValues(new Uint8Array(TOKEN_BYTES)));
|
|
86
|
+
const token = `${prefix}_${secret}`;
|
|
87
|
+
const record = Object.freeze({
|
|
88
|
+
id: newId(),
|
|
89
|
+
tokenHash: await hashAccessToken(token),
|
|
90
|
+
resourceType: resource.type,
|
|
91
|
+
resourceId: resource.id,
|
|
92
|
+
scopes,
|
|
93
|
+
createdAt: new Date(createdAt.getTime()),
|
|
94
|
+
expiresAt: new Date(createdAt.getTime() + ttl * 1000),
|
|
95
|
+
});
|
|
96
|
+
await store.insert(record);
|
|
97
|
+
return Object.freeze({ token, record: toGrant(record) });
|
|
98
|
+
},
|
|
99
|
+
async verify(token) {
|
|
100
|
+
if (typeof token !== "string" || token === "")
|
|
101
|
+
return { ok: false, reason: "missing" };
|
|
102
|
+
if (!tokenPattern.test(token))
|
|
103
|
+
return { ok: false, reason: "invalid" };
|
|
104
|
+
const hash = await hashAccessToken(token);
|
|
105
|
+
const found = await store.findByHash(hash);
|
|
106
|
+
if (found === undefined || found === null)
|
|
107
|
+
return { ok: false, reason: "invalid" };
|
|
108
|
+
const record = toRecord(found);
|
|
109
|
+
if (record === undefined) {
|
|
110
|
+
const error = new Error(`${WHERE}: the store returned a malformed record.`);
|
|
111
|
+
error.name = "MalformedRecord";
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
// The store looked the hash up; compare again in constant time, so a store that answers
|
|
115
|
+
// with another record never grants access.
|
|
116
|
+
if (!timingSafeEqual(record.tokenHash, hash))
|
|
117
|
+
return { ok: false, reason: "invalid" };
|
|
118
|
+
if (record.revokedAt !== undefined)
|
|
119
|
+
return { ok: false, reason: "revoked" };
|
|
120
|
+
if (record.expiresAt.getTime() <= currentTime(now).getTime()) {
|
|
121
|
+
return { ok: false, reason: "expired" };
|
|
122
|
+
}
|
|
123
|
+
return { ok: true, record: toGrant(record) };
|
|
124
|
+
},
|
|
125
|
+
async revoke(id) {
|
|
126
|
+
if (typeof id !== "string" || id === "" || id.length > 255) {
|
|
127
|
+
throw new TypeError(`${WHERE}.revoke: id must be a record id.`);
|
|
128
|
+
}
|
|
129
|
+
return (await store.revoke(id, currentTime(now))) === true;
|
|
130
|
+
},
|
|
131
|
+
async revokeForResource(resource) {
|
|
132
|
+
assertResource(`${WHERE}.revokeForResource`, resource);
|
|
133
|
+
return store.revokeForResource(resource.type, resource.id, currentTime(now));
|
|
134
|
+
},
|
|
135
|
+
async purgeExpired(purge = {}) {
|
|
136
|
+
if (typeof purge !== "object" || purge === null) {
|
|
137
|
+
throw new TypeError(`${WHERE}.purgeExpired: options must be an object.`);
|
|
138
|
+
}
|
|
139
|
+
const keep = purge.olderThan === undefined
|
|
140
|
+
? 0
|
|
141
|
+
: ttlSeconds("olderThan", purge.olderThan, MAX_KEEP_SECONDS);
|
|
142
|
+
return store.purgeExpired(new Date(currentTime(now).getTime() - keep * 1000));
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=tokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.js","sourceRoot":"","sources":["../src/tokens.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAiB,eAAe,EAAE,MAAM,eAAe,CAAC;AAO/D,OAAO,EACL,cAAc,EACd,WAAW,EACX,YAAY,EACZ,cAAc,EACd,MAAM,EACN,WAAW,EACX,mBAAmB,EACnB,OAAO,EACP,QAAQ,GACT,MAAM,iBAAiB,CAAC;AAEzB,MAAM,KAAK,GAAG,cAAc,CAAC;AAC7B,2CAA2C;AAC3C,MAAM,qBAAqB,GAAG,GAAG,GAAG,MAAM,CAAC;AAC3C,MAAM,uBAAuB,GAAG,EAAE,GAAG,MAAM,CAAC;AAC5C,MAAM,mBAAmB,GAAG,MAAM,CAAC;AACnC,uEAAuE;AACvE,MAAM,gBAAgB,GAAG,KAAK,GAAG,MAAM,CAAC;AAoExC,SAAS,SAAS,CAAC,KAAiB;IAClC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,MAAM,IAAI,IAAI,KAAK;QAAE,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IAC9D,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;AACnF,CAAC;AAED,kDAAkD;AAClD,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,KAAa;IACjD,MAAM,MAAM,GAAG,IAAI,UAAU,CAC3B,MAAM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