@exortek/apikey 1.0.0
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/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/README.md +263 -0
- package/dist/errors.d.ts +20 -0
- package/dist/index.cjs +1632 -0
- package/dist/index.d.ts +278 -0
- package/dist/index.mjs +1617 -0
- package/dist/internal/guards.d.ts +4 -0
- package/dist/middleware/core.d.ts +86 -0
- package/dist/middleware/express.cjs +1363 -0
- package/dist/middleware/express.d.ts +4 -0
- package/dist/middleware/express.mjs +1361 -0
- package/dist/middleware/fastify.cjs +1374 -0
- package/dist/middleware/fastify.d.ts +1 -0
- package/dist/middleware/fastify.mjs +1372 -0
- package/dist/scopes.d.ts +21 -0
- package/dist/stores/index.d.ts +2 -0
- package/dist/stores/memory.d.ts +6 -0
- package/dist/stores/redis.d.ts +13 -0
- package/dist/stores.cjs +880 -0
- package/dist/stores.mjs +877 -0
- package/dist/token.d.ts +88 -0
- package/package.json +98 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export const invalidArgument: (msg: string, extra?: WrapExtra) => Error;
|
|
2
|
+
export const parse: (schema: ParseableSchema, input: unknown, path?: string) => unknown;
|
|
3
|
+
export const assertObject: (value: unknown, name: string, opts?: AssertOptions) => void;
|
|
4
|
+
export const assertNonEmptyString: (value: unknown, name: string, opts?: AssertOptions) => void;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize + validate middleware options. Throws `ApiKeyError` on
|
|
3
|
+
* misconfig so the mistake surfaces at boot, not per-request.
|
|
4
|
+
*
|
|
5
|
+
* @param {ApiKeyMiddlewareOptions} options
|
|
6
|
+
*/
|
|
7
|
+
export function normalizeOptions(options: ApiKeyMiddlewareOptions): {
|
|
8
|
+
store: import("../index.js").ApiKeyStore;
|
|
9
|
+
peppers: (string | Buffer | Uint8Array<ArrayBufferLike>)[] | undefined;
|
|
10
|
+
requiredScopes: string[] | undefined;
|
|
11
|
+
expectedPrefix: string | undefined;
|
|
12
|
+
updateLastUsed: boolean;
|
|
13
|
+
headerName: string;
|
|
14
|
+
scheme: "bearer" | "raw";
|
|
15
|
+
allowQueryParam: boolean;
|
|
16
|
+
queryParamName: string;
|
|
17
|
+
attach: string;
|
|
18
|
+
tokenFromRequest: ((ctx: AdapterContext) => string | undefined) | undefined;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Extract the raw key from a request context per the configured
|
|
22
|
+
* scheme. Returns `null` when no candidate was found (the caller then
|
|
23
|
+
* responds with 401 `missing_key`).
|
|
24
|
+
*
|
|
25
|
+
* @param {AdapterContext} ctx
|
|
26
|
+
* @param {ReturnType<typeof normalizeOptions>} config
|
|
27
|
+
* @returns {string | null}
|
|
28
|
+
*/
|
|
29
|
+
export function extractKey(ctx: AdapterContext, config: ReturnType<typeof normalizeOptions>): string | null;
|
|
30
|
+
/**
|
|
31
|
+
* Run the verify against the request. Returns a discriminated result:
|
|
32
|
+
* on success the caller attaches `verifyResult` to the request and
|
|
33
|
+
* proceeds; on failure the caller sends the returned `response`.
|
|
34
|
+
*
|
|
35
|
+
* @param {AdapterContext} ctx
|
|
36
|
+
* @param {ReturnType<typeof normalizeOptions>} config
|
|
37
|
+
* @returns {Promise<AdapterResult>}
|
|
38
|
+
*/
|
|
39
|
+
export function runApiKey(ctx: AdapterContext, config: ReturnType<typeof normalizeOptions>): Promise<AdapterResult>;
|
|
40
|
+
export type AdapterContext = {
|
|
41
|
+
getHeader: (name: string) => string | undefined;
|
|
42
|
+
method?: string | undefined;
|
|
43
|
+
ip?: string | undefined;
|
|
44
|
+
query?: Record<string, unknown> | undefined;
|
|
45
|
+
};
|
|
46
|
+
export type ApiKeyMiddlewareOptions = {
|
|
47
|
+
store: import("../index.js").ApiKeyStore;
|
|
48
|
+
peppers?: (string | Buffer | Uint8Array<ArrayBufferLike>)[] | undefined;
|
|
49
|
+
requiredScopes?: string[] | undefined;
|
|
50
|
+
expectedPrefix?: string | undefined;
|
|
51
|
+
updateLastUsed?: boolean | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Case-insensitive. The default reads `Authorization: Bearer <key>`;
|
|
54
|
+
* a `x-api-key` config reads `X-API-Key: <key>` directly.
|
|
55
|
+
*/
|
|
56
|
+
headerName?: string | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* `'bearer'` expects `Bearer <key>`; `'raw'` uses the header value as-is.
|
|
59
|
+
*/
|
|
60
|
+
scheme?: "bearer" | "raw" | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* When true, falls back to `?api_key=<key>` if no matching header is
|
|
63
|
+
* found. Discouraged because query strings leak into access logs and
|
|
64
|
+
* referer headers — off by default.
|
|
65
|
+
*/
|
|
66
|
+
allowQueryParam?: boolean | undefined;
|
|
67
|
+
queryParamName?: string | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Property name attached to the request object on success.
|
|
70
|
+
*/
|
|
71
|
+
attach?: string | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Override the default extraction entirely.
|
|
74
|
+
*/
|
|
75
|
+
tokenFromRequest?: ((ctx: AdapterContext) => string | undefined) | undefined;
|
|
76
|
+
};
|
|
77
|
+
export type AdapterResult = {
|
|
78
|
+
verifyResult: import("../index.js").VerifyApiKeyResult;
|
|
79
|
+
response?: {
|
|
80
|
+
status: number;
|
|
81
|
+
body: {
|
|
82
|
+
error: string;
|
|
83
|
+
reason?: string;
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
};
|