@keycardai/express 0.1.0 → 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/LICENSE +21 -0
- package/dist/cjs/grant.d.ts +16 -4
- package/dist/cjs/grant.d.ts.map +1 -1
- package/dist/cjs/grant.js +35 -11
- package/dist/cjs/grant.js.map +1 -1
- package/dist/esm/grant.d.ts +16 -4
- package/dist/esm/grant.d.ts.map +1 -1
- package/dist/esm/grant.js +35 -11
- package/dist/esm/grant.js.map +1 -1
- package/package.json +11 -11
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Keycard
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/cjs/grant.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { RequestHandler } from "express";
|
|
|
2
2
|
import { AccessContext } from "@keycardai/oauth/server/accessContext";
|
|
3
3
|
import type { ApplicationCredential } from "@keycardai/oauth/credentials";
|
|
4
4
|
import type { AuthenticatedRequest } from "./bearerAuth.js";
|
|
5
|
+
import type { AccessToken } from "@keycardai/oauth/server/accessToken";
|
|
5
6
|
export interface GrantedRequest extends AuthenticatedRequest {
|
|
6
7
|
accessContext: AccessContext;
|
|
7
8
|
}
|
|
@@ -12,13 +13,24 @@ export interface GrantOptions {
|
|
|
12
13
|
*/
|
|
13
14
|
zoneUrl?: string;
|
|
14
15
|
/**
|
|
15
|
-
* Keycard zone ID
|
|
16
|
-
*
|
|
16
|
+
* Keycard zone ID, or a function that resolves it from the verified
|
|
17
|
+
* access token at request time. Use the function form for multi-zone
|
|
18
|
+
* deployments where each request may target a different zone.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* // Static zone
|
|
22
|
+
* grant(resources, { zoneId: "zone-abc" });
|
|
23
|
+
*
|
|
24
|
+
* // Dynamic zone extracted from the token's clientId
|
|
25
|
+
* grant(resources, { zoneId: (auth) => auth.clientId });
|
|
26
|
+
* ```
|
|
17
27
|
*/
|
|
18
|
-
zoneId?: string;
|
|
28
|
+
zoneId?: string | ((auth: AccessToken) => string);
|
|
19
29
|
/**
|
|
20
30
|
* Application credential provider for authenticated token exchange.
|
|
21
|
-
*
|
|
31
|
+
* For multi-zone deployments, pass a `ClientSecret` constructed with a
|
|
32
|
+
* `Record<zoneId, [clientId, clientSecret]>` so the correct credentials
|
|
33
|
+
* are selected per request.
|
|
22
34
|
*/
|
|
23
35
|
applicationCredential?: ApplicationCredential;
|
|
24
36
|
}
|
package/dist/cjs/grant.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grant.d.ts","sourceRoot":"","sources":["../../src/grant.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAC;AAE/E,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AACtE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAE1E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"grant.d.ts","sourceRoot":"","sources":["../../src/grant.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAC;AAE/E,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AACtE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAE1E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAC5D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qCAAqC,CAAC;AAGvE,MAAM,WAAW,cAAe,SAAQ,oBAAoB;IAC1D,aAAa,EAAE,aAAa,CAAC;CAC9B;AAED,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,WAAW,KAAK,MAAM,CAAC,CAAC;IAClD;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,qBAAqB,CAAC;CAC/C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,KAAK,CACnB,SAAS,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,EACrC,OAAO,EAAE,YAAY,GACpB,cAAc,CAuGhB"}
|
package/dist/cjs/grant.js
CHANGED
|
@@ -25,10 +25,16 @@ const errors_1 = require("@keycardai/oauth/errors");
|
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
27
|
function grant(resources, options) {
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
// Validate at construction time that a zone is specified.
|
|
29
|
+
const hasZoneOption = !!(options.zoneUrl || options.zoneId);
|
|
30
|
+
if (!hasZoneOption) {
|
|
30
31
|
throw new errors_1.AuthProviderConfigurationError("grant: either `zoneUrl` or `zoneId` is required");
|
|
31
32
|
}
|
|
33
|
+
// Cache TokenExchangeClient instances keyed by resolved zone URL.
|
|
34
|
+
// One client per zone amortizes AS discovery (GET /.well-known/) across
|
|
35
|
+
// requests — the TokenExchangeClient already caches the token_endpoint
|
|
36
|
+
// internally after the first discovery call.
|
|
37
|
+
const clientCache = new Map();
|
|
32
38
|
return async (req, _res, next) => {
|
|
33
39
|
const authReq = req;
|
|
34
40
|
const subjectToken = authReq.auth?.token;
|
|
@@ -40,19 +46,35 @@ function grant(resources, options) {
|
|
|
40
46
|
req.accessContext = accessCtx;
|
|
41
47
|
return next();
|
|
42
48
|
}
|
|
43
|
-
|
|
49
|
+
// Resolve zone at request time — zoneId may be a static string or a
|
|
50
|
+
// function that extracts the zone from the verified access token.
|
|
51
|
+
let resolvedZoneId;
|
|
44
52
|
try {
|
|
45
|
-
|
|
46
|
-
|
|
53
|
+
resolvedZoneId =
|
|
54
|
+
typeof options.zoneId === "function"
|
|
55
|
+
? options.zoneId(authReq.auth)
|
|
56
|
+
: options.zoneId;
|
|
47
57
|
}
|
|
48
58
|
catch (e) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
59
|
+
return next(e);
|
|
60
|
+
}
|
|
61
|
+
const resolvedZoneUrl = options.zoneUrl ?? buildZoneUrl(resolvedZoneId);
|
|
62
|
+
if (!resolvedZoneUrl) {
|
|
63
|
+
accessCtx.setError({ message: "Could not resolve zone URL for this request." });
|
|
53
64
|
req.accessContext = accessCtx;
|
|
54
65
|
return next();
|
|
55
66
|
}
|
|
67
|
+
// Look up or create a cached client for this zone.
|
|
68
|
+
let client = clientCache.get(resolvedZoneUrl);
|
|
69
|
+
if (!client) {
|
|
70
|
+
// Pass the credential directly so TokenExchangeClient can call
|
|
71
|
+
// getAuth(zoneId) at exchange time, enabling multi-zone credential
|
|
72
|
+
// routing without pre-resolving credentials here.
|
|
73
|
+
client = new tokenExchange_1.TokenExchangeClient(resolvedZoneUrl, {
|
|
74
|
+
credential: options.applicationCredential,
|
|
75
|
+
});
|
|
76
|
+
clientCache.set(resolvedZoneUrl, client);
|
|
77
|
+
}
|
|
56
78
|
const resourceList = Array.isArray(resources)
|
|
57
79
|
? resources
|
|
58
80
|
: [resources];
|
|
@@ -61,7 +83,7 @@ function grant(resources, options) {
|
|
|
61
83
|
try {
|
|
62
84
|
let exchangeRequest;
|
|
63
85
|
if (options.applicationCredential) {
|
|
64
|
-
exchangeRequest = await options.applicationCredential.prepareTokenExchangeRequest(subjectToken, resource);
|
|
86
|
+
exchangeRequest = await options.applicationCredential.prepareTokenExchangeRequest(subjectToken, resource, { zoneId: resolvedZoneId });
|
|
65
87
|
}
|
|
66
88
|
else {
|
|
67
89
|
exchangeRequest = {
|
|
@@ -70,7 +92,9 @@ function grant(resources, options) {
|
|
|
70
92
|
subjectTokenType: "urn:ietf:params:oauth:token-type:access_token",
|
|
71
93
|
};
|
|
72
94
|
}
|
|
73
|
-
tokens[resource] = await client.exchangeToken(exchangeRequest
|
|
95
|
+
tokens[resource] = await client.exchangeToken(exchangeRequest, {
|
|
96
|
+
zoneId: resolvedZoneId,
|
|
97
|
+
});
|
|
74
98
|
}
|
|
75
99
|
catch (e) {
|
|
76
100
|
const detail = {
|
package/dist/cjs/grant.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grant.js","sourceRoot":"","sources":["../../src/grant.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"grant.js","sourceRoot":"","sources":["../../src/grant.ts"],"names":[],"mappings":";;AA8DA,sBA0GC;AAvKD,kEAAqE;AACrE,yEAAsE;AAEtE,oDAAqF;AAsCrF;;;;;;;;;;;;;;;;;;;GAm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AAC;gBACX,MAAM,MAAM,GAAgF;oBAC1F,OAAO,EAAE,6BAA6B,QAAQ,EAAE;iBACjD,CAAC;gBACF,IAAI,CAAC,YAAY,mBAAU,EAAE,CAAC;oBAC5B,MAAM,CAAC,IAAI,GAAG,CAAC,CAAC,SAAS,CAAC;oBAC1B,MAAM,CAAC,WAAW,GAAG,CAAC,CAAC,OAAO,CAAC;gBACjC,CAAC;qBAAM,CAAC;oBACN,MAAM,CAAC,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;gBAC9B,CAAC;gBACD,SAAS,CAAC,gBAAgB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;YAC/C,CAAC;QACH,CAAC;QAED,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QAC/B,GAAsB,CAAC,aAAa,GAAG,SAAS,CAAC;QAClD,IAAI,EAAE,CAAC;IACT,CAAC,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CAAC,MAAe;IACnC,IAAI,CAAC,MAAM;QAAE,OAAO,SAAS,CAAC;IAC9B,OAAO,WAAW,MAAM,gBAAgB,CAAC;AAC3C,CAAC"}
|
package/dist/esm/grant.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { RequestHandler } from "express";
|
|
|
2
2
|
import { AccessContext } from "@keycardai/oauth/server/accessContext";
|
|
3
3
|
import type { ApplicationCredential } from "@keycardai/oauth/credentials";
|
|
4
4
|
import type { AuthenticatedRequest } from "./bearerAuth.js";
|
|
5
|
+
import type { AccessToken } from "@keycardai/oauth/server/accessToken";
|
|
5
6
|
export interface GrantedRequest extends AuthenticatedRequest {
|
|
6
7
|
accessContext: AccessContext;
|
|
7
8
|
}
|
|
@@ -12,13 +13,24 @@ export interface GrantOptions {
|
|
|
12
13
|
*/
|
|
13
14
|
zoneUrl?: string;
|
|
14
15
|
/**
|
|
15
|
-
* Keycard zone ID
|
|
16
|
-
*
|
|
16
|
+
* Keycard zone ID, or a function that resolves it from the verified
|
|
17
|
+
* access token at request time. Use the function form for multi-zone
|
|
18
|
+
* deployments where each request may target a different zone.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* // Static zone
|
|
22
|
+
* grant(resources, { zoneId: "zone-abc" });
|
|
23
|
+
*
|
|
24
|
+
* // Dynamic zone extracted from the token's clientId
|
|
25
|
+
* grant(resources, { zoneId: (auth) => auth.clientId });
|
|
26
|
+
* ```
|
|
17
27
|
*/
|
|
18
|
-
zoneId?: string;
|
|
28
|
+
zoneId?: string | ((auth: AccessToken) => string);
|
|
19
29
|
/**
|
|
20
30
|
* Application credential provider for authenticated token exchange.
|
|
21
|
-
*
|
|
31
|
+
* For multi-zone deployments, pass a `ClientSecret` constructed with a
|
|
32
|
+
* `Record<zoneId, [clientId, clientSecret]>` so the correct credentials
|
|
33
|
+
* are selected per request.
|
|
22
34
|
*/
|
|
23
35
|
applicationCredential?: ApplicationCredential;
|
|
24
36
|
}
|
package/dist/esm/grant.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grant.d.ts","sourceRoot":"","sources":["../../src/grant.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAC;AAE/E,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AACtE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAE1E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"grant.d.ts","sourceRoot":"","sources":["../../src/grant.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAC;AAE/E,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AACtE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAE1E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAC5D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qCAAqC,CAAC;AAGvE,MAAM,WAAW,cAAe,SAAQ,oBAAoB;IAC1D,aAAa,EAAE,aAAa,CAAC;CAC9B;AAED,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,WAAW,KAAK,MAAM,CAAC,CAAC;IAClD;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,qBAAqB,CAAC;CAC/C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,KAAK,CACnB,SAAS,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,EACrC,OAAO,EAAE,YAAY,GACpB,cAAc,CAuGhB"}
|
package/dist/esm/grant.js
CHANGED
|
@@ -22,10 +22,16 @@ import { OAuthError, AuthProviderConfigurationError } from "@keycardai/oauth/err
|
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
24
|
export function grant(resources, options) {
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
// Validate at construction time that a zone is specified.
|
|
26
|
+
const hasZoneOption = !!(options.zoneUrl || options.zoneId);
|
|
27
|
+
if (!hasZoneOption) {
|
|
27
28
|
throw new AuthProviderConfigurationError("grant: either `zoneUrl` or `zoneId` is required");
|
|
28
29
|
}
|
|
30
|
+
// Cache TokenExchangeClient instances keyed by resolved zone URL.
|
|
31
|
+
// One client per zone amortizes AS discovery (GET /.well-known/) across
|
|
32
|
+
// requests — the TokenExchangeClient already caches the token_endpoint
|
|
33
|
+
// internally after the first discovery call.
|
|
34
|
+
const clientCache = new Map();
|
|
29
35
|
return async (req, _res, next) => {
|
|
30
36
|
const authReq = req;
|
|
31
37
|
const subjectToken = authReq.auth?.token;
|
|
@@ -37,19 +43,35 @@ export function grant(resources, options) {
|
|
|
37
43
|
req.accessContext = accessCtx;
|
|
38
44
|
return next();
|
|
39
45
|
}
|
|
40
|
-
|
|
46
|
+
// Resolve zone at request time — zoneId may be a static string or a
|
|
47
|
+
// function that extracts the zone from the verified access token.
|
|
48
|
+
let resolvedZoneId;
|
|
41
49
|
try {
|
|
42
|
-
|
|
43
|
-
|
|
50
|
+
resolvedZoneId =
|
|
51
|
+
typeof options.zoneId === "function"
|
|
52
|
+
? options.zoneId(authReq.auth)
|
|
53
|
+
: options.zoneId;
|
|
44
54
|
}
|
|
45
55
|
catch (e) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
56
|
+
return next(e);
|
|
57
|
+
}
|
|
58
|
+
const resolvedZoneUrl = options.zoneUrl ?? buildZoneUrl(resolvedZoneId);
|
|
59
|
+
if (!resolvedZoneUrl) {
|
|
60
|
+
accessCtx.setError({ message: "Could not resolve zone URL for this request." });
|
|
50
61
|
req.accessContext = accessCtx;
|
|
51
62
|
return next();
|
|
52
63
|
}
|
|
64
|
+
// Look up or create a cached client for this zone.
|
|
65
|
+
let client = clientCache.get(resolvedZoneUrl);
|
|
66
|
+
if (!client) {
|
|
67
|
+
// Pass the credential directly so TokenExchangeClient can call
|
|
68
|
+
// getAuth(zoneId) at exchange time, enabling multi-zone credential
|
|
69
|
+
// routing without pre-resolving credentials here.
|
|
70
|
+
client = new TokenExchangeClient(resolvedZoneUrl, {
|
|
71
|
+
credential: options.applicationCredential,
|
|
72
|
+
});
|
|
73
|
+
clientCache.set(resolvedZoneUrl, client);
|
|
74
|
+
}
|
|
53
75
|
const resourceList = Array.isArray(resources)
|
|
54
76
|
? resources
|
|
55
77
|
: [resources];
|
|
@@ -58,7 +80,7 @@ export function grant(resources, options) {
|
|
|
58
80
|
try {
|
|
59
81
|
let exchangeRequest;
|
|
60
82
|
if (options.applicationCredential) {
|
|
61
|
-
exchangeRequest = await options.applicationCredential.prepareTokenExchangeRequest(subjectToken, resource);
|
|
83
|
+
exchangeRequest = await options.applicationCredential.prepareTokenExchangeRequest(subjectToken, resource, { zoneId: resolvedZoneId });
|
|
62
84
|
}
|
|
63
85
|
else {
|
|
64
86
|
exchangeRequest = {
|
|
@@ -67,7 +89,9 @@ export function grant(resources, options) {
|
|
|
67
89
|
subjectTokenType: "urn:ietf:params:oauth:token-type:access_token",
|
|
68
90
|
};
|
|
69
91
|
}
|
|
70
|
-
tokens[resource] = await client.exchangeToken(exchangeRequest
|
|
92
|
+
tokens[resource] = await client.exchangeToken(exchangeRequest, {
|
|
93
|
+
zoneId: resolvedZoneId,
|
|
94
|
+
});
|
|
71
95
|
}
|
|
72
96
|
catch (e) {
|
|
73
97
|
const detail = {
|
package/dist/esm/grant.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grant.js","sourceRoot":"","sources":["../../src/grant.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AAEtE,OAAO,EAAE,UAAU,EAAE,8BAA8B,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"grant.js","sourceRoot":"","sources":["../../src/grant.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AAEtE,OAAO,EAAE,UAAU,EAAE,8BAA8B,EAAE,MAAM,yBAAyB,CAAC;AAsCrF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,KAAK,CACnB,SAAqC,EACrC,OAAqB;IAErB,0DAA0D;IAC1D,MAAM,aAAa,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC5D,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,IAAI,8BAA8B,CACtC,iDAAiD,CAClD,CAAC;IACJ,CAAC;IAED,k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ACN,MAAM,CAAC,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;gBAC9B,CAAC;gBACD,SAAS,CAAC,gBAAgB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;YAC/C,CAAC;QACH,CAAC;QAED,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QAC/B,GAAsB,CAAC,aAAa,GAAG,SAAS,CAAC;QAClD,IAAI,EAAE,CAAC;IACT,CAAC,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CAAC,MAAe;IACnC,IAAI,CAAC,MAAM;QAAE,OAAO,SAAS,CAAC;IAC9B,OAAO,WAAW,MAAM,gBAAgB,CAAC;AAC3C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keycardai/express",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "[Preview] Keycard auth middleware for Express: bearer token validation, RFC 6750 challenges, delegated token exchange, and OAuth discovery routes",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -26,16 +26,8 @@
|
|
|
26
26
|
]
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
|
-
"scripts": {
|
|
30
|
-
"build": "pnpm run build:esm && pnpm run build:cjs",
|
|
31
|
-
"build:esm": "tsc -p tsconfig.prod.json && echo '{\"type\": \"module\"}' > dist/esm/package.json",
|
|
32
|
-
"build:cjs": "tsc -p tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json",
|
|
33
|
-
"test": "NODE_OPTIONS='--experimental-vm-modules' jest",
|
|
34
|
-
"typecheck": "tsc --noEmit",
|
|
35
|
-
"clean": "rm -rf dist"
|
|
36
|
-
},
|
|
37
29
|
"dependencies": {
|
|
38
|
-
"@keycardai/oauth": "
|
|
30
|
+
"@keycardai/oauth": "0.6.0"
|
|
39
31
|
},
|
|
40
32
|
"peerDependencies": {
|
|
41
33
|
"express": ">=4.0.0"
|
|
@@ -49,5 +41,13 @@
|
|
|
49
41
|
"supertest": "^7.1.1",
|
|
50
42
|
"ts-jest": "^29.4.0",
|
|
51
43
|
"typescript": "^5.8.3"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "pnpm run build:esm && pnpm run build:cjs",
|
|
47
|
+
"build:esm": "tsc -p tsconfig.prod.json && echo '{\"type\": \"module\"}' > dist/esm/package.json",
|
|
48
|
+
"build:cjs": "tsc -p tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json",
|
|
49
|
+
"test": "NODE_OPTIONS='--experimental-vm-modules' jest",
|
|
50
|
+
"typecheck": "tsc --noEmit",
|
|
51
|
+
"clean": "rm -rf dist"
|
|
52
52
|
}
|
|
53
|
-
}
|
|
53
|
+
}
|