nextidp 0.1.5 → 0.4.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 +43 -0
- package/README.md +86 -0
- package/dist/cjs/access-token.d.ts +29 -4
- package/dist/cjs/access-token.js +53 -10
- package/dist/cjs/config.d.ts +36 -0
- package/dist/cjs/config.js +36 -1
- package/dist/cjs/discovery.d.ts +2 -0
- package/dist/cjs/discovery.js +2 -0
- package/dist/cjs/errors.d.ts +16 -3
- package/dist/cjs/errors.js +7 -1
- package/dist/cjs/exchange.d.ts +21 -0
- package/dist/cjs/exchange.js +76 -0
- package/dist/cjs/express.d.ts +12 -1
- package/dist/cjs/express.js +7 -0
- package/dist/cjs/fastify.d.ts +13 -1
- package/dist/cjs/fastify.js +14 -0
- package/dist/cjs/hono.d.ts +12 -1
- package/dist/cjs/hono.js +8 -0
- package/dist/cjs/id-token.js +1 -1
- package/dist/cjs/idp.d.ts +27 -1
- package/dist/cjs/idp.js +76 -8
- package/dist/cjs/index.d.ts +3 -2
- package/dist/cjs/nestjs.d.ts +2 -1
- package/dist/cjs/next.d.ts +12 -1
- package/dist/cjs/next.js +4 -0
- package/dist/cjs/node-adapter.d.ts +2 -0
- package/dist/cjs/node-adapter.js +8 -0
- package/dist/cjs/node.d.ts +7 -1
- package/dist/cjs/node.js +1 -0
- package/dist/cjs/pushed-authorization.d.ts +9 -0
- package/dist/cjs/pushed-authorization.js +66 -0
- package/dist/cjs/service.d.ts +3 -0
- package/dist/cjs/service.js +13 -41
- package/dist/cjs/token-cache.d.ts +18 -0
- package/dist/cjs/token-cache.js +75 -0
- package/dist/cjs/tokens.d.ts +13 -0
- package/dist/cjs/tokens.js +19 -0
- package/dist/esm/access-token.d.ts +29 -4
- package/dist/esm/access-token.js +50 -9
- package/dist/esm/config.d.ts +36 -0
- package/dist/esm/config.js +35 -1
- package/dist/esm/discovery.d.ts +2 -0
- package/dist/esm/discovery.js +2 -0
- package/dist/esm/errors.d.ts +16 -3
- package/dist/esm/errors.js +7 -1
- package/dist/esm/exchange.d.ts +21 -0
- package/dist/esm/exchange.js +52 -0
- package/dist/esm/express.d.ts +12 -1
- package/dist/esm/express.js +7 -0
- package/dist/esm/fastify.d.ts +13 -1
- package/dist/esm/fastify.js +15 -1
- package/dist/esm/hono.d.ts +12 -1
- package/dist/esm/hono.js +8 -0
- package/dist/esm/id-token.js +1 -1
- package/dist/esm/idp.d.ts +27 -1
- package/dist/esm/idp.js +79 -9
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/nestjs.d.ts +2 -1
- package/dist/esm/next.d.ts +12 -1
- package/dist/esm/next.js +4 -0
- package/dist/esm/node-adapter.d.ts +2 -0
- package/dist/esm/node-adapter.js +8 -0
- package/dist/esm/node.d.ts +7 -1
- package/dist/esm/node.js +1 -0
- package/dist/esm/pushed-authorization.d.ts +9 -0
- package/dist/esm/pushed-authorization.js +32 -0
- package/dist/esm/service.d.ts +3 -0
- package/dist/esm/service.js +11 -41
- package/dist/esm/token-cache.d.ts +18 -0
- package/dist/esm/token-cache.js +50 -0
- package/dist/esm/tokens.d.ts +13 -0
- package/dist/esm/tokens.js +16 -0
- package/package.json +3 -2
package/dist/cjs/express.d.ts
CHANGED
|
@@ -47,6 +47,16 @@ export type ExpressIdp = Idp & {
|
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
49
|
requireToken(requirements?: TokenRequirements): Middleware;
|
|
50
|
+
/**
|
|
51
|
+
* For an API with `resourceMetadata` on: answers its protected resource metadata (RFC 9728)
|
|
52
|
+
* at `/.well-known/oauth-protected-resource` and the audience's path, so MCP clients find the
|
|
53
|
+
* auth server. Mount it on the app, before anything that needs a token:
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* app.use(auth.resourceMetadata());
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
resourceMetadata(): Middleware;
|
|
50
60
|
/** The access token `requireToken` checked for this request. */
|
|
51
61
|
tokenOf(req: IncomingMessage): AccessToken | undefined;
|
|
52
62
|
};
|
|
@@ -61,9 +71,10 @@ export type ExpressIdp = Idp & {
|
|
|
61
71
|
* ```
|
|
62
72
|
*/
|
|
63
73
|
export declare function idp(options?: IdpOptions | (() => IdpOptions)): ExpressIdp;
|
|
64
|
-
export type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
74
|
+
export type { AccessToken, ResourceMetadata, TokenRequirements } from "./access-token.js";
|
|
65
75
|
export type { ApiConfig, IdpConfig, IdpOptions } from "./config.js";
|
|
66
76
|
export { IdpError, type IdpErrorCode, isIdpError, isSignInErrorCode, isTokenError, SIGN_IN_ERROR_CODES, type SignInErrorCode, type TokenErrorCode, } from "./errors.js";
|
|
77
|
+
export type { ExchangeTokenOptions } from "./exchange.js";
|
|
67
78
|
export type { Idp, Refreshed, SignInPathOptions } from "./idp.js";
|
|
68
79
|
export type { SessionOptions } from "./node-adapter.js";
|
|
69
80
|
export type { InterceptableClient, ServiceTokenOptions, UseServiceTokenOptions, } from "./service.js";
|
package/dist/cjs/express.js
CHANGED
|
@@ -99,6 +99,13 @@ function idp(options) {
|
|
|
99
99
|
);
|
|
100
100
|
};
|
|
101
101
|
},
|
|
102
|
+
resourceMetadata() {
|
|
103
|
+
return (req, res, next) => {
|
|
104
|
+
node.resourceMetadata(req, res).then((handled) => {
|
|
105
|
+
if (!handled) next();
|
|
106
|
+
}, next);
|
|
107
|
+
};
|
|
108
|
+
},
|
|
102
109
|
tokenOf: (req) => req.idp?.token
|
|
103
110
|
};
|
|
104
111
|
}
|
package/dist/cjs/fastify.d.ts
CHANGED
|
@@ -41,6 +41,17 @@ export type FastifyIdp = Idp & {
|
|
|
41
41
|
requireToken(requirements?: TokenRequirements): IdpHook;
|
|
42
42
|
/** The access token `requireToken` checked for this request. */
|
|
43
43
|
tokenOf(request: FastifyRequest): AccessToken | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* For an API with `resourceMetadata` on: a plugin that answers its protected resource
|
|
46
|
+
* metadata (RFC 9728) at `/.well-known/oauth-protected-resource` and the audience's path, so
|
|
47
|
+
* MCP clients find the auth server. Register it on the app itself, not under a prefix: the
|
|
48
|
+
* well-known path is at the root, whatever prefix the API's routes have.
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* await app.register(auth.resourceMetadata());
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
resourceMetadata(): FastifyPluginAsync;
|
|
44
55
|
};
|
|
45
56
|
/**
|
|
46
57
|
* The client for a Fastify app or API. An app that signs people in needs the sign-in settings
|
|
@@ -57,9 +68,10 @@ export type FastifyIdp = Idp & {
|
|
|
57
68
|
* ```
|
|
58
69
|
*/
|
|
59
70
|
export declare function idp(options?: IdpOptions | (() => IdpOptions)): FastifyIdp;
|
|
60
|
-
export type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
71
|
+
export type { AccessToken, ResourceMetadata, TokenRequirements } from "./access-token.js";
|
|
61
72
|
export type { ApiConfig, IdpConfig, IdpOptions } from "./config.js";
|
|
62
73
|
export { IdpError, type IdpErrorCode, isIdpError, isSignInErrorCode, isTokenError, SIGN_IN_ERROR_CODES, type SignInErrorCode, type TokenErrorCode, } from "./errors.js";
|
|
74
|
+
export type { ExchangeTokenOptions } from "./exchange.js";
|
|
63
75
|
export type { Idp, Refreshed, SignInPathOptions } from "./idp.js";
|
|
64
76
|
export type { SessionOptions } from "./node-adapter.js";
|
|
65
77
|
export type { InterceptableClient, ServiceTokenOptions, UseServiceTokenOptions, } from "./service.js";
|
package/dist/cjs/fastify.js
CHANGED
|
@@ -83,6 +83,20 @@ function idp(options) {
|
|
|
83
83
|
Object.assign(plugin, { [/* @__PURE__ */ Symbol.for("skip-override")]: true });
|
|
84
84
|
return plugin;
|
|
85
85
|
},
|
|
86
|
+
resourceMetadata() {
|
|
87
|
+
const plugin = async (app) => {
|
|
88
|
+
app.addHook("onRequest", async (request, reply) => {
|
|
89
|
+
const response = core.resourceMetadataResponse(
|
|
90
|
+
new Request(new URL(request.url, "http://localhost"), { method: request.method })
|
|
91
|
+
);
|
|
92
|
+
if (!response) return;
|
|
93
|
+
reply.hijack();
|
|
94
|
+
await (0, import_node_http.sendWebResponse)(reply.raw, response);
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
Object.assign(plugin, { [/* @__PURE__ */ Symbol.for("skip-override")]: true });
|
|
98
|
+
return plugin;
|
|
99
|
+
},
|
|
86
100
|
requireSession() {
|
|
87
101
|
return async (request, reply) => {
|
|
88
102
|
const session = await sessionFor(request, reply);
|
package/dist/cjs/hono.d.ts
CHANGED
|
@@ -37,6 +37,16 @@ export type HonoIdp = Idp & {
|
|
|
37
37
|
* WWW-Authenticate (RFC 6750); one that can't be checked now (the auth server is down) 503.
|
|
38
38
|
*/
|
|
39
39
|
requireToken(requirements?: TokenRequirements): MiddlewareHandler<IdpEnv>;
|
|
40
|
+
/**
|
|
41
|
+
* For an API with `resourceMetadata` on: answers its protected resource metadata (RFC 9728)
|
|
42
|
+
* at `/.well-known/oauth-protected-resource` and the audience's path, so MCP clients find the
|
|
43
|
+
* auth server; every other request goes on.
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* app.use(auth.resourceMetadata());
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
resourceMetadata(): MiddlewareHandler<IdpEnv>;
|
|
40
50
|
};
|
|
41
51
|
/**
|
|
42
52
|
* The client for a Hono app or API:
|
|
@@ -56,9 +66,10 @@ export type HonoIdp = Idp & {
|
|
|
56
66
|
* `idp(() => ({ env: workerEnv }))`.
|
|
57
67
|
*/
|
|
58
68
|
export declare function idp(options?: IdpOptions | (() => IdpOptions)): HonoIdp;
|
|
59
|
-
export type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
69
|
+
export type { AccessToken, ResourceMetadata, TokenRequirements } from "./access-token.js";
|
|
60
70
|
export type { ApiConfig, IdpConfig, IdpOptions } from "./config.js";
|
|
61
71
|
export { IdpError, type IdpErrorCode, isIdpError, isSignInErrorCode, isTokenError, SIGN_IN_ERROR_CODES, type SignInErrorCode, type TokenErrorCode, } from "./errors.js";
|
|
72
|
+
export type { ExchangeTokenOptions } from "./exchange.js";
|
|
62
73
|
export type { Idp, Refreshed, SignInPathOptions } from "./idp.js";
|
|
63
74
|
export type { InterceptableClient, ServiceTokenOptions, UseServiceTokenOptions, } from "./service.js";
|
|
64
75
|
export type { IdpUser, Session } from "./session.js";
|
package/dist/cjs/hono.js
CHANGED
|
@@ -92,6 +92,14 @@ function idp(options) {
|
|
|
92
92
|
putCookies(c, setCookies);
|
|
93
93
|
};
|
|
94
94
|
},
|
|
95
|
+
resourceMetadata() {
|
|
96
|
+
return async (c, next) => {
|
|
97
|
+
const response = core.resourceMetadataResponse(c.req.raw);
|
|
98
|
+
if (response) return response;
|
|
99
|
+
await next();
|
|
100
|
+
return void 0;
|
|
101
|
+
};
|
|
102
|
+
},
|
|
95
103
|
requireToken(requirements = {}) {
|
|
96
104
|
return async (c, next) => {
|
|
97
105
|
let token;
|
package/dist/cjs/id-token.js
CHANGED
|
@@ -26,7 +26,7 @@ module.exports = __toCommonJS(id_token_exports);
|
|
|
26
26
|
var import_jose = require("jose");
|
|
27
27
|
var import_errors = require("./errors.js");
|
|
28
28
|
var import_pkce = require("./pkce.js");
|
|
29
|
-
const ALLOWED_ALGORITHMS = ["RS256", "PS256", "ES256", "EdDSA"];
|
|
29
|
+
const ALLOWED_ALGORITHMS = ["RS256", "PS256", "ES256", "Ed25519", "EdDSA"];
|
|
30
30
|
function signingAlgorithms(listed) {
|
|
31
31
|
const algorithms = listed ? listed.filter((alg) => ALLOWED_ALGORITHMS.includes(alg)) : [];
|
|
32
32
|
return algorithms.length > 0 ? algorithms : ["RS256"];
|
package/dist/cjs/idp.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { type AccessToken, type TokenRequirements } from "./access-token.js";
|
|
1
|
+
import { type AccessToken, type ResourceMetadata, type TokenRequirements } from "./access-token.js";
|
|
2
2
|
import { type ApiConfig, type IdpConfig, type IdpOptions } from "./config.js";
|
|
3
3
|
import { IdpError } from "./errors.js";
|
|
4
|
+
import { type ExchangeTokenOptions } from "./exchange.js";
|
|
4
5
|
import { type InterceptableClient, type ServiceTokenOptions, type UseServiceTokenOptions } from "./service.js";
|
|
5
6
|
import { type Session } from "./session.js";
|
|
6
7
|
/** Where cookies are read from: a request, its headers, or the Cookie header itself. */
|
|
@@ -53,6 +54,31 @@ export type Idp = {
|
|
|
53
54
|
verifyBearer(authorization: string | null | undefined, requirements?: TokenRequirements): Promise<AccessToken>;
|
|
54
55
|
/** The 401 or 403 for a refused token, with its WWW-Authenticate challenge (RFC 6750). */
|
|
55
56
|
tokenErrorResponse(error: IdpError): Response;
|
|
57
|
+
/**
|
|
58
|
+
* This API's protected resource metadata (RFC 9728), with `resourceMetadata` on: the auth
|
|
59
|
+
* server to sign in with, and the scopes. MCP clients read it to find the auth server.
|
|
60
|
+
*/
|
|
61
|
+
resourceMetadataDocument(): ResourceMetadata;
|
|
62
|
+
/**
|
|
63
|
+
* The answer to a request for the protected resource metadata (at
|
|
64
|
+
* `/.well-known/oauth-protected-resource` and the audience's path), or undefined for any
|
|
65
|
+
* other request, or while `resourceMetadata` is off. The adapters' `resourceMetadata()`
|
|
66
|
+
* serve it.
|
|
67
|
+
*/
|
|
68
|
+
resourceMetadataResponse(request: Request): Response | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Token exchange (RFC 8693), for an MCP server that calls another API as the person: the
|
|
71
|
+
* access token it was called with, traded for one meant for `resource`, with this
|
|
72
|
+
* service's credentials (`clientId` and `clientSecret`) and within the policy the auth
|
|
73
|
+
* server has for it. Kept until a minute before it expires, never past the token it came
|
|
74
|
+
* from; calls at the same time share one request. Throws IdpError("token_exchange_failed")
|
|
75
|
+
* when the auth server refuses or doesn't answer.
|
|
76
|
+
*
|
|
77
|
+
* ```ts
|
|
78
|
+
* const crm = await auth.exchangeToken({ subjectToken: token.raw, resource: "https://crm.example.com" });
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
exchangeToken(options: ExchangeTokenOptions): Promise<string>;
|
|
56
82
|
/**
|
|
57
83
|
* This service's own access token (client credentials), to call another API as itself.
|
|
58
84
|
* Kept until a minute before it expires, per scopes and API; calls at the same time share
|
package/dist/cjs/idp.js
CHANGED
|
@@ -26,10 +26,12 @@ var import_config = require("./config.js");
|
|
|
26
26
|
var import_cookies = require("./cookies.js");
|
|
27
27
|
var import_discovery = require("./discovery.js");
|
|
28
28
|
var import_errors = require("./errors.js");
|
|
29
|
+
var import_exchange = require("./exchange.js");
|
|
29
30
|
var import_http = require("./http.js");
|
|
30
31
|
var import_id_token = require("./id-token.js");
|
|
31
32
|
var import_logout_token = require("./logout-token.js");
|
|
32
33
|
var import_pkce = require("./pkce.js");
|
|
34
|
+
var import_pushed_authorization = require("./pushed-authorization.js");
|
|
33
35
|
var import_return_path = require("./return-path.js");
|
|
34
36
|
var import_sealed = require("./sealed.js");
|
|
35
37
|
var import_service = require("./service.js");
|
|
@@ -94,6 +96,7 @@ function createIdp(options = {}) {
|
|
|
94
96
|
let state;
|
|
95
97
|
let apiState;
|
|
96
98
|
let serviceTokens;
|
|
99
|
+
let tokenExchange;
|
|
97
100
|
const shared = /* @__PURE__ */ new Map();
|
|
98
101
|
let resolvedOptions;
|
|
99
102
|
const optionsOf = () => {
|
|
@@ -143,6 +146,13 @@ function createIdp(options = {}) {
|
|
|
143
146
|
serviceTokens = (0, import_service.createServiceTokens)({ http, discovery, config });
|
|
144
147
|
return serviceTokens;
|
|
145
148
|
};
|
|
149
|
+
const exchangeReady = () => {
|
|
150
|
+
if (tokenExchange) return tokenExchange;
|
|
151
|
+
const config = (0, import_config.resolveServiceConfig)(optionsOf());
|
|
152
|
+
const { http, discovery } = sharedFor(config.issuer);
|
|
153
|
+
tokenExchange = (0, import_exchange.createTokenExchange)({ http, discovery, config });
|
|
154
|
+
return tokenExchange;
|
|
155
|
+
};
|
|
146
156
|
const introspectCached = async (s, discovery, token) => {
|
|
147
157
|
const key = await sha256(token);
|
|
148
158
|
const now = Date.now();
|
|
@@ -163,11 +173,21 @@ function createIdp(options = {}) {
|
|
|
163
173
|
});
|
|
164
174
|
return checked;
|
|
165
175
|
};
|
|
176
|
+
const refusalFor = (error, s, requirements) => new import_errors.IdpError(error.code, error.message, {
|
|
177
|
+
status: error.status,
|
|
178
|
+
cause: error.cause,
|
|
179
|
+
scopes: requirements.scopes?.length ? requirements.scopes : error.scopes,
|
|
180
|
+
resourceMetadata: s.config.resourceMetadata?.url
|
|
181
|
+
});
|
|
166
182
|
const verifyAccessToken = async (token, requirements = {}) => {
|
|
167
183
|
const s = apiReady();
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
184
|
+
try {
|
|
185
|
+
const discovery = await s.discovery();
|
|
186
|
+
const checked = (0, import_access_token.isJwt)(token) ? await (0, import_access_token.verifyJwtAccessToken)(token, s.keys(discovery.jwks_uri), s.config) : await introspectCached(s, discovery, token);
|
|
187
|
+
return { ...(0, import_access_token.requireScopes)(checked, requirements.scopes), raw: token };
|
|
188
|
+
} catch (error) {
|
|
189
|
+
throw (0, import_errors.isTokenError)(error) ? refusalFor(error, s, requirements) : error;
|
|
190
|
+
}
|
|
171
191
|
};
|
|
172
192
|
const fail = (error) => {
|
|
173
193
|
const idpError = (0, import_errors.isIdpError)(error) ? error : new import_errors.IdpError("token_failed", "Something went wrong.", { cause: error });
|
|
@@ -269,6 +289,13 @@ function createIdp(options = {}) {
|
|
|
269
289
|
setCookies: clear()
|
|
270
290
|
};
|
|
271
291
|
}
|
|
292
|
+
function pushEndpoint(s, discovery) {
|
|
293
|
+
const setting = s.config.pushedAuthorization;
|
|
294
|
+
if (setting === false) return void 0;
|
|
295
|
+
const endpoint = discovery.pushed_authorization_request_endpoint;
|
|
296
|
+
if (endpoint) return endpoint;
|
|
297
|
+
return setting === true ? "missing" : void 0;
|
|
298
|
+
}
|
|
272
299
|
async function login(s, request, url) {
|
|
273
300
|
const returnTo = (0, import_return_path.safePath)(url.searchParams.get("returnTo")) ?? s.config.afterSignIn;
|
|
274
301
|
const { prompt, setCookies } = promptOf(s, request, url);
|
|
@@ -280,8 +307,7 @@ function createIdp(options = {}) {
|
|
|
280
307
|
return plain(503, "The sign-in service can't be reached. Try again in a moment.");
|
|
281
308
|
}
|
|
282
309
|
const flow = { verifier: (0, import_pkce.randomToken)(), state: (0, import_pkce.randomToken)(), nonce: (0, import_pkce.randomToken)(), returnTo };
|
|
283
|
-
const
|
|
284
|
-
authorize.search = new URLSearchParams({
|
|
310
|
+
const parameters = new URLSearchParams({
|
|
285
311
|
response_type: "code",
|
|
286
312
|
client_id: s.config.clientId,
|
|
287
313
|
redirect_uri: s.config.redirectUri,
|
|
@@ -292,7 +318,33 @@ function createIdp(options = {}) {
|
|
|
292
318
|
code_challenge_method: "S256",
|
|
293
319
|
...s.config.resource ? { resource: s.config.resource } : {},
|
|
294
320
|
...prompt ? { prompt } : {}
|
|
295
|
-
})
|
|
321
|
+
});
|
|
322
|
+
const authorize = new URL(discovery.authorization_endpoint);
|
|
323
|
+
const push = pushEndpoint(s, discovery);
|
|
324
|
+
if (push === "missing") {
|
|
325
|
+
fail(
|
|
326
|
+
new import_errors.IdpError(
|
|
327
|
+
"pushed_authorization_failed",
|
|
328
|
+
"pushedAuthorization is true, and the auth server doesn't advertise pushed_authorization_request_endpoint."
|
|
329
|
+
)
|
|
330
|
+
);
|
|
331
|
+
return plain(503, "The sign-in service can't be reached. Try again in a moment.");
|
|
332
|
+
}
|
|
333
|
+
if (push) {
|
|
334
|
+
let requestUri;
|
|
335
|
+
try {
|
|
336
|
+
requestUri = await (0, import_pushed_authorization.pushAuthorizationRequest)(s.http, push, s.client, parameters);
|
|
337
|
+
} catch (error) {
|
|
338
|
+
fail(error);
|
|
339
|
+
return plain(503, "The sign-in service can't be reached. Try again in a moment.");
|
|
340
|
+
}
|
|
341
|
+
authorize.search = new URLSearchParams({
|
|
342
|
+
client_id: s.config.clientId,
|
|
343
|
+
request_uri: requestUri
|
|
344
|
+
}).toString();
|
|
345
|
+
} else {
|
|
346
|
+
authorize.search = parameters.toString();
|
|
347
|
+
}
|
|
296
348
|
const sealed = await s.sealer.seal(flow, import_session.FLOW_PURPOSE, import_session.FLOW_MAX_AGE);
|
|
297
349
|
return redirect(authorize.toString(), 302, [
|
|
298
350
|
// Lax: the cookie comes back on the top-level redirect from the auth server.
|
|
@@ -501,10 +553,14 @@ function createIdp(options = {}) {
|
|
|
501
553
|
},
|
|
502
554
|
apiConfig: () => apiReady().config,
|
|
503
555
|
verifyAccessToken,
|
|
504
|
-
async verifyBearer(authorization, requirements) {
|
|
556
|
+
async verifyBearer(authorization, requirements = {}) {
|
|
505
557
|
const token = (0, import_access_token.bearerToken)(authorization);
|
|
506
558
|
if (!token) {
|
|
507
|
-
throw
|
|
559
|
+
throw refusalFor(
|
|
560
|
+
new import_errors.IdpError("missing_token", "An access token is required.", { status: 401 }),
|
|
561
|
+
apiReady(),
|
|
562
|
+
requirements
|
|
563
|
+
);
|
|
508
564
|
}
|
|
509
565
|
return verifyAccessToken(token, requirements);
|
|
510
566
|
},
|
|
@@ -515,6 +571,18 @@ function createIdp(options = {}) {
|
|
|
515
571
|
headers: refusal.headers
|
|
516
572
|
});
|
|
517
573
|
},
|
|
574
|
+
resourceMetadataDocument() {
|
|
575
|
+
const document = (0, import_access_token.resourceMetadataDocument)(apiReady().config);
|
|
576
|
+
if (!document) {
|
|
577
|
+
throw new import_errors.IdpError(
|
|
578
|
+
"misconfigured",
|
|
579
|
+
"nextidp is misconfigured: resourceMetadata (IDP_RESOURCE_METADATA) is off."
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
return document;
|
|
583
|
+
},
|
|
584
|
+
resourceMetadataResponse: (request) => (0, import_access_token.resourceMetadataResponse)(apiReady().config, request),
|
|
585
|
+
exchangeToken: async (options2) => exchangeReady().exchange(options2),
|
|
518
586
|
serviceToken: async (options2) => serviceReady().get(options2),
|
|
519
587
|
dropServiceToken(token) {
|
|
520
588
|
serviceTokens?.drop(token);
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export { type AccessToken, bearerChallenge, bearerToken, type TokenRequirements, } from "./access-token.js";
|
|
2
|
-
export type { ApiConfig, IdpConfig, IdpOptions, ServiceConfig } from "./config.js";
|
|
1
|
+
export { type AccessToken, bearerChallenge, bearerToken, type ResourceMetadata, type TokenRequirements, } from "./access-token.js";
|
|
2
|
+
export type { ApiConfig, IdpConfig, IdpOptions, ResourceMetadataConfig, ServiceConfig, } from "./config.js";
|
|
3
3
|
export { DEFAULT_SCOPES } from "./config.js";
|
|
4
4
|
export { IdpError, type IdpErrorCode, isIdpError, isSignInErrorCode, isTokenError, SIGN_IN_ERROR_CODES, type SignInErrorCode, type TokenErrorCode, } from "./errors.js";
|
|
5
|
+
export type { ExchangeTokenOptions } from "./exchange.js";
|
|
5
6
|
export { type CookieSource, createIdp as idp, type Idp, type Refreshed, type SignInPathOptions, } from "./idp.js";
|
|
6
7
|
export { safePath } from "./return-path.js";
|
|
7
8
|
export type { InterceptableClient, ServiceTokenOptions, UseServiceTokenOptions, } from "./service.js";
|
package/dist/cjs/nestjs.d.ts
CHANGED
|
@@ -46,8 +46,9 @@ export declare class IdpModule {
|
|
|
46
46
|
static forRoot(auth?: Idp | IdpOptions, options?: IdpModuleOptions): DynamicModule;
|
|
47
47
|
static forRootAsync(options: IdpModuleAsyncOptions): DynamicModule;
|
|
48
48
|
}
|
|
49
|
-
export type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
49
|
+
export type { AccessToken, ResourceMetadata, TokenRequirements } from "./access-token.js";
|
|
50
50
|
export type { ApiConfig, IdpOptions } from "./config.js";
|
|
51
51
|
export { IdpError, isIdpError, isTokenError, type TokenErrorCode } from "./errors.js";
|
|
52
|
+
export type { ExchangeTokenOptions } from "./exchange.js";
|
|
52
53
|
export { createIdp as idp, type Idp } from "./idp.js";
|
|
53
54
|
export type { InterceptableClient, ServiceTokenOptions, UseServiceTokenOptions, } from "./service.js";
|
package/dist/cjs/next.d.ts
CHANGED
|
@@ -39,6 +39,16 @@ export type NextIdp = Idp & {
|
|
|
39
39
|
* );
|
|
40
40
|
* ```
|
|
41
41
|
*/
|
|
42
|
+
/**
|
|
43
|
+
* For an API with `resourceMetadata` on: its protected resource metadata (RFC 9728), so MCP
|
|
44
|
+
* clients find the auth server. In
|
|
45
|
+
* `app/.well-known/oauth-protected-resource/[[...path]]/route.ts`:
|
|
46
|
+
* `export const { GET, OPTIONS } = auth.resourceMetadata();`
|
|
47
|
+
*/
|
|
48
|
+
resourceMetadata(): {
|
|
49
|
+
GET: (request: Request) => Response;
|
|
50
|
+
OPTIONS: (request: Request) => Response;
|
|
51
|
+
};
|
|
42
52
|
withToken<Context extends object = object>(requirements: TokenRequirements, handler: (request: NextRequest, context: Context & {
|
|
43
53
|
token: AccessToken;
|
|
44
54
|
}) => Response | Promise<Response>): (request: NextRequest, context: Context) => Promise<Response>;
|
|
@@ -52,9 +62,10 @@ export type NextIdp = Idp & {
|
|
|
52
62
|
* ```
|
|
53
63
|
*/
|
|
54
64
|
export declare function idp(options?: IdpOptions | (() => IdpOptions)): NextIdp;
|
|
55
|
-
export type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
65
|
+
export type { AccessToken, ResourceMetadata, TokenRequirements } from "./access-token.js";
|
|
56
66
|
export type { ApiConfig, IdpConfig, IdpOptions } from "./config.js";
|
|
57
67
|
export { IdpError, type IdpErrorCode, isIdpError, isSignInErrorCode, isTokenError, SIGN_IN_ERROR_CODES, type SignInErrorCode, type TokenErrorCode, } from "./errors.js";
|
|
68
|
+
export type { ExchangeTokenOptions } from "./exchange.js";
|
|
58
69
|
export type { Idp, Refreshed, SignInPathOptions } from "./idp.js";
|
|
59
70
|
export type { InterceptableClient, ServiceTokenOptions, UseServiceTokenOptions, } from "./service.js";
|
|
60
71
|
export type { IdpUser, Session } from "./session.js";
|
package/dist/cjs/next.js
CHANGED
|
@@ -66,6 +66,10 @@ function idp(options) {
|
|
|
66
66
|
return {
|
|
67
67
|
...core,
|
|
68
68
|
routes: () => ({ GET: handle, POST: handle }),
|
|
69
|
+
resourceMetadata() {
|
|
70
|
+
const answer = (request) => core.resourceMetadataResponse(request) ?? new Response("Not found", { status: 404 });
|
|
71
|
+
return { GET: answer, OPTIONS: answer };
|
|
72
|
+
},
|
|
69
73
|
session,
|
|
70
74
|
async requireSession(returnTo) {
|
|
71
75
|
const current = await session();
|
|
@@ -12,5 +12,7 @@ export type NodeAdapter = {
|
|
|
12
12
|
handle(req: IncomingMessage, res: ServerResponse): Promise<boolean>;
|
|
13
13
|
session(req: IncomingMessage, res: ServerResponse, options?: SessionOptions): Promise<Session | null>;
|
|
14
14
|
turnAway(req: IncomingMessage, res: ServerResponse, returnTo?: string): void;
|
|
15
|
+
/** Answers a request for the protected resource metadata and returns true; else false. */
|
|
16
|
+
resourceMetadata(req: IncomingMessage, res: ServerResponse): Promise<boolean>;
|
|
15
17
|
};
|
|
16
18
|
export declare function nodeAdapter(core: Idp): NodeAdapter;
|
package/dist/cjs/node-adapter.js
CHANGED
|
@@ -41,6 +41,14 @@ function nodeAdapter(core) {
|
|
|
41
41
|
await (0, import_node_http.sendWebResponse)(res, await core.handler(request));
|
|
42
42
|
return true;
|
|
43
43
|
},
|
|
44
|
+
async resourceMetadata(req, res) {
|
|
45
|
+
const response = core.resourceMetadataResponse(
|
|
46
|
+
new Request(new URL(req.url ?? "/", "http://localhost"), { method: req.method ?? "GET" })
|
|
47
|
+
);
|
|
48
|
+
if (!response) return false;
|
|
49
|
+
await (0, import_node_http.sendWebResponse)(res, response);
|
|
50
|
+
return true;
|
|
51
|
+
},
|
|
44
52
|
session(req, res, { refresh = true } = {}) {
|
|
45
53
|
const known = sessions.get(req);
|
|
46
54
|
if (known) return known;
|
package/dist/cjs/node.d.ts
CHANGED
|
@@ -21,6 +21,11 @@ export type NodeIdp = Idp & {
|
|
|
21
21
|
* response is sent.
|
|
22
22
|
*/
|
|
23
23
|
requireSession(req: IncomingMessage, res: ServerResponse, returnTo?: string): Promise<Session | null>;
|
|
24
|
+
/**
|
|
25
|
+
* For an API with `resourceMetadata` on: answers a request for its protected resource
|
|
26
|
+
* metadata (RFC 9728) and returns true; any other path is left to you, and it returns false.
|
|
27
|
+
*/
|
|
28
|
+
resourceMetadata(req: IncomingMessage, res: ServerResponse): Promise<boolean>;
|
|
24
29
|
};
|
|
25
30
|
/**
|
|
26
31
|
* The client for an app on Node's own HTTP server:
|
|
@@ -40,9 +45,10 @@ export type NodeIdp = Idp & {
|
|
|
40
45
|
* ```
|
|
41
46
|
*/
|
|
42
47
|
export declare function idp(options?: IdpOptions | (() => IdpOptions)): NodeIdp;
|
|
43
|
-
export type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
48
|
+
export type { AccessToken, ResourceMetadata, TokenRequirements } from "./access-token.js";
|
|
44
49
|
export type { ApiConfig, IdpConfig, IdpOptions } from "./config.js";
|
|
45
50
|
export { IdpError, type IdpErrorCode, isIdpError, isSignInErrorCode, isTokenError, SIGN_IN_ERROR_CODES, type SignInErrorCode, type TokenErrorCode, } from "./errors.js";
|
|
51
|
+
export type { ExchangeTokenOptions } from "./exchange.js";
|
|
46
52
|
export type { Idp, Refreshed, SignInPathOptions } from "./idp.js";
|
|
47
53
|
export type { SessionOptions } from "./node-adapter.js";
|
|
48
54
|
export type { InterceptableClient, ServiceTokenOptions, UseServiceTokenOptions, } from "./service.js";
|
package/dist/cjs/node.js
CHANGED
|
@@ -38,6 +38,7 @@ function idp(options) {
|
|
|
38
38
|
...core,
|
|
39
39
|
handle: node.handle,
|
|
40
40
|
session: node.session,
|
|
41
|
+
resourceMetadata: node.resourceMetadata,
|
|
41
42
|
async requireSession(req, res, returnTo) {
|
|
42
43
|
const session = await node.session(req, res);
|
|
43
44
|
if (!session) node.turnAway(req, res, returnTo);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Http } from "./http.js";
|
|
2
|
+
import { type Client } from "./tokens.js";
|
|
3
|
+
/**
|
|
4
|
+
* Posts the authorization request's parameters, authenticated as at the token endpoint (Basic
|
|
5
|
+
* for a confidential client, client_id in the body for a public one), and returns the
|
|
6
|
+
* request_uri. One attempt, no retries: a request_uri works once, for 60 seconds on
|
|
7
|
+
* Next-Identity Server, so it's asked for at the moment of the redirect.
|
|
8
|
+
*/
|
|
9
|
+
export declare function pushAuthorizationRequest(http: Http, endpoint: string, client: Client, parameters: URLSearchParams): Promise<string>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var pushed_authorization_exports = {};
|
|
30
|
+
__export(pushed_authorization_exports, {
|
|
31
|
+
pushAuthorizationRequest: () => pushAuthorizationRequest
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(pushed_authorization_exports);
|
|
34
|
+
var v = __toESM(require("valibot"), 1);
|
|
35
|
+
var import_errors = require("./errors.js");
|
|
36
|
+
var import_tokens = require("./tokens.js");
|
|
37
|
+
const PushedRequestSchema = v.object({
|
|
38
|
+
request_uri: v.pipe(v.string(), v.nonEmpty()),
|
|
39
|
+
expires_in: v.number()
|
|
40
|
+
});
|
|
41
|
+
async function pushAuthorizationRequest(http, endpoint, client, parameters) {
|
|
42
|
+
const form = new URLSearchParams(parameters);
|
|
43
|
+
const headers = (0, import_tokens.authenticate)(client, form);
|
|
44
|
+
let answer;
|
|
45
|
+
try {
|
|
46
|
+
answer = await http.postForm(endpoint, form, headers);
|
|
47
|
+
} catch (cause) {
|
|
48
|
+
throw new import_errors.IdpError("pushed_authorization_failed", "The PAR endpoint didn't answer.", { cause });
|
|
49
|
+
}
|
|
50
|
+
if (!answer.ok) {
|
|
51
|
+
throw new import_errors.IdpError(
|
|
52
|
+
"pushed_authorization_failed",
|
|
53
|
+
`The PAR endpoint refused: ${(0, import_tokens.oauthError)(answer.data) ?? answer.status}.`,
|
|
54
|
+
{ status: answer.status }
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
const parsed = v.safeParse(PushedRequestSchema, answer.data);
|
|
58
|
+
if (!parsed.success) {
|
|
59
|
+
throw new import_errors.IdpError("pushed_authorization_failed", "The PAR endpoint's answer isn't valid.");
|
|
60
|
+
}
|
|
61
|
+
return parsed.output.request_uri;
|
|
62
|
+
}
|
|
63
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
64
|
+
0 && (module.exports = {
|
|
65
|
+
pushAuthorizationRequest
|
|
66
|
+
});
|
package/dist/cjs/service.d.ts
CHANGED
|
@@ -40,6 +40,9 @@ export type InterceptableClient = {
|
|
|
40
40
|
};
|
|
41
41
|
};
|
|
42
42
|
};
|
|
43
|
+
/** The scopes as the token endpoint gets them, sorted, so the same set is one key. */
|
|
44
|
+
export declare function scopeOf(scopes: readonly string[] | undefined): string | undefined;
|
|
45
|
+
export declare function resourceOf(resource: string | undefined): string | undefined;
|
|
43
46
|
export type ServiceTokens = {
|
|
44
47
|
get(options?: ServiceTokenOptions): Promise<string>;
|
|
45
48
|
drop(token: string): void;
|
package/dist/cjs/service.js
CHANGED
|
@@ -19,13 +19,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var service_exports = {};
|
|
20
20
|
__export(service_exports, {
|
|
21
21
|
createServiceTokens: () => createServiceTokens,
|
|
22
|
+
resourceOf: () => resourceOf,
|
|
23
|
+
scopeOf: () => scopeOf,
|
|
22
24
|
useServiceToken: () => useServiceToken
|
|
23
25
|
});
|
|
24
26
|
module.exports = __toCommonJS(service_exports);
|
|
25
27
|
var import_access_token = require("./access-token.js");
|
|
26
28
|
var import_errors = require("./errors.js");
|
|
29
|
+
var import_token_cache = require("./token-cache.js");
|
|
27
30
|
var import_tokens = require("./tokens.js");
|
|
28
|
-
const RENEW_BEFORE_S = 60;
|
|
29
31
|
const MAX_KEPT = 100;
|
|
30
32
|
function misconfigured(problem) {
|
|
31
33
|
return new import_errors.IdpError("misconfigured", `nextidp is misconfigured: ${problem}.`);
|
|
@@ -43,54 +45,22 @@ function resourceOf(resource) {
|
|
|
43
45
|
return resource;
|
|
44
46
|
}
|
|
45
47
|
function createServiceTokens(deps) {
|
|
46
|
-
const
|
|
48
|
+
const cache = (0, import_token_cache.createTokenCache)(MAX_KEPT);
|
|
47
49
|
const client = { clientId: deps.config.clientId, clientSecret: deps.config.clientSecret };
|
|
48
|
-
const issue = async (scope, resource) => {
|
|
49
|
-
const tokens = await (0, import_tokens.clientCredentials)(deps.http, await deps.discovery(), client, {
|
|
50
|
-
scope,
|
|
51
|
-
resource
|
|
52
|
-
});
|
|
53
|
-
const lifetime = tokens.expires_in ?? 0;
|
|
54
|
-
const renewIn = lifetime - Math.min(RENEW_BEFORE_S, lifetime / 2);
|
|
55
|
-
return { token: tokens.access_token, until: Date.now() + Math.max(0, renewIn) * 1e3 };
|
|
56
|
-
};
|
|
57
|
-
const prune = (now) => {
|
|
58
|
-
for (const [key, entry] of kept) {
|
|
59
|
-
if (entry.issued && entry.issued.until <= now) kept.delete(key);
|
|
60
|
-
}
|
|
61
|
-
const oldest = kept.keys().next();
|
|
62
|
-
if (kept.size >= MAX_KEPT && !oldest.done) kept.delete(oldest.value);
|
|
63
|
-
};
|
|
64
50
|
return {
|
|
65
51
|
async get(options = {}) {
|
|
66
52
|
const scope = scopeOf(options.scopes);
|
|
67
53
|
const resource = resourceOf(options.resource ?? deps.config.resource);
|
|
68
54
|
const key = JSON.stringify([resource ?? null, scope ?? null]);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (kept.size >= MAX_KEPT) prune(now);
|
|
76
|
-
const entry = { pending: issue(scope, resource) };
|
|
77
|
-
entry.pending.then(
|
|
78
|
-
(issued) => {
|
|
79
|
-
entry.issued = issued;
|
|
80
|
-
},
|
|
81
|
-
() => void 0
|
|
55
|
+
return cache.get(
|
|
56
|
+
key,
|
|
57
|
+
async () => (0, import_token_cache.issued)(
|
|
58
|
+
await (0, import_tokens.clientCredentials)(deps.http, await deps.discovery(), client, { scope, resource })
|
|
59
|
+
),
|
|
60
|
+
options.fresh
|
|
82
61
|
);
|
|
83
|
-
kept.set(key, entry);
|
|
84
|
-
try {
|
|
85
|
-
return (await entry.pending).token;
|
|
86
|
-
} catch (error) {
|
|
87
|
-
if (kept.get(key) === entry) kept.delete(key);
|
|
88
|
-
throw error;
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
drop(token) {
|
|
92
|
-
for (const [key, entry] of kept) if (entry.issued?.token === token) kept.delete(key);
|
|
93
62
|
},
|
|
63
|
+
drop: (token) => cache.drop(token),
|
|
94
64
|
defaultResource: deps.config.resource
|
|
95
65
|
};
|
|
96
66
|
}
|
|
@@ -169,5 +139,7 @@ function useServiceToken(tokens, client, options = {}) {
|
|
|
169
139
|
// Annotate the CommonJS export names for ESM import in node:
|
|
170
140
|
0 && (module.exports = {
|
|
171
141
|
createServiceTokens,
|
|
142
|
+
resourceOf,
|
|
143
|
+
scopeOf,
|
|
172
144
|
useServiceToken
|
|
173
145
|
});
|