@japikey/cloudflare 0.2.1 → 0.3.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/README.md +7 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/router.d.ts +8 -3
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +11 -8
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
createApiKeyRouter,
|
|
25
25
|
createJWKSRouter,
|
|
26
26
|
isJWKSPath,
|
|
27
|
-
type
|
|
27
|
+
type ApiKeyRouterOptions,
|
|
28
28
|
type CreateApiKeyData,
|
|
29
29
|
} from '@japikey/cloudflare';
|
|
30
30
|
|
|
@@ -75,7 +75,7 @@ export default {
|
|
|
75
75
|
const db = new D1Driver(env.DB);
|
|
76
76
|
await db.ensureTable();
|
|
77
77
|
|
|
78
|
-
const options:
|
|
78
|
+
const options: ApiKeyRouterOptions<Env> = {
|
|
79
79
|
getUserId,
|
|
80
80
|
parseCreateApiKeyRequest,
|
|
81
81
|
issuer: new URL('https://example.com/'),
|
|
@@ -91,7 +91,11 @@ export default {
|
|
|
91
91
|
// Handle JWKS routes
|
|
92
92
|
const baseIssuer = new URL('https://example.com/');
|
|
93
93
|
if (isJWKSPath(request, baseIssuer)) {
|
|
94
|
-
const jwksRouter = createJWKSRouter(
|
|
94
|
+
const jwksRouter = createJWKSRouter({
|
|
95
|
+
baseIssuer,
|
|
96
|
+
db,
|
|
97
|
+
maxAgeSeconds: 300,
|
|
98
|
+
});
|
|
95
99
|
return jwksRouter.fetch(request, env);
|
|
96
100
|
}
|
|
97
101
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ApiKeyRouterOptions, CreateApiKeyData, JwksRouterOptions } from './router.ts';
|
|
2
2
|
import { createApiKeyRouter, createJWKSRouter, isJWKSPath } from './router.ts';
|
|
3
3
|
import D1Driver from './d1.ts';
|
|
4
|
-
export { createApiKeyRouter, createJWKSRouter, isJWKSPath, D1Driver, type
|
|
4
|
+
export { createApiKeyRouter, createJWKSRouter, isJWKSPath, D1Driver, type ApiKeyRouterOptions, type CreateApiKeyData, type JwksRouterOptions, };
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC/E,OAAO,QAAQ,MAAM,SAAS,CAAC;AAE/B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,UAAU,EACV,QAAQ,EACR,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,GACvB,CAAC"}
|
package/dist/router.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export type CreateApiKeyData = {
|
|
|
5
5
|
claims: Record<string, unknown>;
|
|
6
6
|
databaseMetadata: Record<string, unknown>;
|
|
7
7
|
};
|
|
8
|
-
export type
|
|
8
|
+
export type ApiKeyRouterOptions<Env> = {
|
|
9
9
|
getUserId: (request: Request, env: Env) => Promise<string>;
|
|
10
10
|
parseCreateApiKeyRequest: (request: Request, env: Env) => Promise<CreateApiKeyData>;
|
|
11
11
|
issuer: URL;
|
|
@@ -13,7 +13,12 @@ export type CreateApiKeyRouterOptions<Env> = {
|
|
|
13
13
|
db: DatabaseDriver;
|
|
14
14
|
routePrefix: string;
|
|
15
15
|
};
|
|
16
|
-
export
|
|
16
|
+
export type JwksRouterOptions = {
|
|
17
|
+
baseIssuer: URL;
|
|
18
|
+
db: DatabaseDriver;
|
|
19
|
+
maxAgeSeconds?: number;
|
|
20
|
+
};
|
|
21
|
+
export declare function createJWKSRouter<Env>(options: JwksRouterOptions): ExportedHandler<Env>;
|
|
17
22
|
export declare function isJWKSPath(request: Request, baseIssuer: URL): boolean;
|
|
18
|
-
export declare function createApiKeyRouter<Env>(options:
|
|
23
|
+
export declare function createApiKeyRouter<Env>(options: ApiKeyRouterOptions<Env>): ExportedHandler<Env>;
|
|
19
24
|
//# sourceMappingURL=router.d.ts.map
|
package/dist/router.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAiB,MAAM,kBAAkB,CAAC;AAGtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAGjE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,IAAI,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC3C,CAAC;AAEF,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAiB,MAAM,kBAAkB,CAAC;AAGtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAGjE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,IAAI,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC3C,CAAC;AAEF,MAAM,MAAM,mBAAmB,CAAC,GAAG,IAAI;IACrC,SAAS,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAC3D,wBAAwB,EAAE,CACxB,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,GAAG,KACL,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC/B,MAAM,EAAE,GAAG,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,EAAE,EAAE,cAAc,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,UAAU,EAAE,GAAG,CAAC;IAChB,EAAE,EAAE,cAAc,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AA2EF,wBAAgB,gBAAgB,CAAC,GAAG,EAClC,OAAO,EAAE,iBAAiB,GACzB,eAAe,CAAC,GAAG,CAAC,CAUtB;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,GAAG,OAAO,CAOrE;AA6HD,wBAAgB,kBAAkB,CAAC,GAAG,EACpC,OAAO,EAAE,mBAAmB,CAAC,GAAG,CAAC,GAChC,eAAe,CAAC,GAAG,CAAC,CAmCtB"}
|
package/dist/router.js
CHANGED
|
@@ -25,18 +25,21 @@ function wrapError(fn) {
|
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
|
-
async function handleJWKSRequest(kid,
|
|
29
|
-
const row = await db.getApiKey(kid);
|
|
28
|
+
async function handleJWKSRequest(kid, options) {
|
|
29
|
+
const row = await options.db.getApiKey(kid);
|
|
30
30
|
if (!row || row.revoked) {
|
|
31
31
|
throw new NotFoundError('API key not found');
|
|
32
32
|
}
|
|
33
33
|
const jwks = {
|
|
34
34
|
keys: [row.jwk],
|
|
35
35
|
};
|
|
36
|
+
const headers = {
|
|
37
|
+
'Content-Type': 'application/json',
|
|
38
|
+
};
|
|
39
|
+
const maxAge = Math.max(options.maxAgeSeconds ?? 0, 0); // Negative values are undefined per mdn - clamp to 0
|
|
40
|
+
headers['Cache-Control'] = `max-age=${maxAge}`;
|
|
36
41
|
return new Response(JSON.stringify(jwks), {
|
|
37
|
-
headers
|
|
38
|
-
'Content-Type': 'application/json',
|
|
39
|
-
},
|
|
42
|
+
headers,
|
|
40
43
|
});
|
|
41
44
|
}
|
|
42
45
|
function getWellKnownKid(request, baseIssuer) {
|
|
@@ -60,11 +63,11 @@ function getWellKnownKid(request, baseIssuer) {
|
|
|
60
63
|
}
|
|
61
64
|
return kid;
|
|
62
65
|
}
|
|
63
|
-
export function createJWKSRouter(
|
|
66
|
+
export function createJWKSRouter(options) {
|
|
64
67
|
return {
|
|
65
68
|
fetch: wrapError(async function fetch(request, env) {
|
|
66
|
-
const kid = getWellKnownKid(request, baseIssuer);
|
|
67
|
-
return handleJWKSRequest(kid,
|
|
69
|
+
const kid = getWellKnownKid(request, options.baseIssuer);
|
|
70
|
+
return handleJWKSRequest(kid, options);
|
|
68
71
|
}),
|
|
69
72
|
};
|
|
70
73
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@japikey/cloudflare",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Cloudflare worker-based endpoints for japikey",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.5.0"
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"format:check": "prettier --check src/**/*.ts"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@japikey/japikey": "0.
|
|
47
|
+
"@japikey/japikey": "0.3.0",
|
|
48
48
|
"path-to-regexp": "^8.2.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|