nextidp 0.0.1 → 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/CHANGELOG.md +98 -0
- package/README.md +502 -4
- package/dist/cjs/access-token.d.ts +56 -0
- package/dist/cjs/access-token.js +196 -0
- package/dist/cjs/config.d.ts +128 -0
- package/dist/cjs/config.js +233 -0
- package/dist/cjs/cookies.d.ts +23 -0
- package/dist/cjs/cookies.js +118 -0
- package/dist/cjs/discovery.d.ts +24 -0
- package/dist/cjs/discovery.js +91 -0
- package/dist/cjs/errors.d.ts +63 -0
- package/dist/cjs/errors.js +69 -0
- package/dist/cjs/express.d.ts +71 -0
- package/dist/cjs/express.js +114 -0
- package/dist/cjs/hono.d.ts +65 -0
- package/dist/cjs/hono.js +130 -0
- package/dist/cjs/http.d.ts +10 -0
- package/dist/cjs/http.js +68 -0
- package/dist/cjs/id-token.d.ts +19 -0
- package/dist/cjs/id-token.js +77 -0
- package/dist/cjs/idp.d.ts +81 -0
- package/dist/cjs/idp.js +482 -0
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +53 -0
- package/dist/cjs/logout-token.d.ts +13 -0
- package/dist/cjs/logout-token.js +63 -0
- package/dist/cjs/nestjs.d.ts +53 -0
- package/dist/cjs/nestjs.js +142 -0
- package/dist/cjs/next.d.ts +61 -0
- package/dist/cjs/next.js +118 -0
- package/dist/cjs/node-adapter.d.ts +16 -0
- package/dist/cjs/node-adapter.js +76 -0
- package/dist/cjs/node-http.d.ts +22 -0
- package/dist/cjs/node-http.js +127 -0
- package/dist/cjs/node.d.ts +50 -0
- package/dist/cjs/node.js +57 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/pkce.d.ts +6 -0
- package/dist/cjs/pkce.js +49 -0
- package/dist/cjs/return-path.d.ts +6 -0
- package/dist/cjs/return-path.js +41 -0
- package/dist/cjs/sealed.d.ts +12 -0
- package/dist/cjs/sealed.js +78 -0
- package/dist/cjs/service.d.ts +61 -0
- package/dist/cjs/service.js +173 -0
- package/dist/cjs/session.d.ts +51 -0
- package/dist/cjs/session.js +97 -0
- package/dist/cjs/store.d.ts +58 -0
- package/dist/cjs/store.js +145 -0
- package/dist/cjs/tokens.d.ts +38 -0
- package/dist/cjs/tokens.js +138 -0
- package/dist/esm/access-token.d.ts +56 -0
- package/dist/esm/access-token.js +156 -0
- package/dist/esm/config.d.ts +128 -0
- package/dist/esm/config.js +206 -0
- package/dist/esm/cookies.d.ts +23 -0
- package/dist/esm/cookies.js +89 -0
- package/dist/esm/discovery.d.ts +24 -0
- package/dist/esm/discovery.js +56 -0
- package/dist/esm/errors.d.ts +63 -0
- package/dist/esm/errors.js +41 -0
- package/dist/esm/express.d.ts +71 -0
- package/dist/esm/express.js +90 -0
- package/dist/esm/hono.d.ts +65 -0
- package/dist/esm/hono.js +106 -0
- package/dist/esm/http.d.ts +10 -0
- package/dist/esm/http.js +44 -0
- package/dist/esm/id-token.d.ts +19 -0
- package/dist/esm/id-token.js +51 -0
- package/dist/esm/idp.d.ts +81 -0
- package/dist/esm/idp.js +488 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +30 -0
- package/dist/esm/logout-token.d.ts +13 -0
- package/dist/esm/logout-token.js +39 -0
- package/dist/esm/nestjs.d.ts +53 -0
- package/dist/esm/nestjs.js +118 -0
- package/dist/esm/next.d.ts +61 -0
- package/dist/esm/next.js +94 -0
- package/dist/esm/node-adapter.d.ts +16 -0
- package/dist/esm/node-adapter.js +60 -0
- package/dist/esm/node-http.d.ts +22 -0
- package/dist/esm/node-http.js +96 -0
- package/dist/esm/node.d.ts +50 -0
- package/dist/esm/node.js +33 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/pkce.d.ts +6 -0
- package/dist/esm/pkce.js +23 -0
- package/dist/esm/return-path.d.ts +6 -0
- package/dist/esm/return-path.js +17 -0
- package/dist/esm/sealed.d.ts +12 -0
- package/dist/esm/sealed.js +54 -0
- package/dist/esm/service.d.ts +61 -0
- package/dist/esm/service.js +148 -0
- package/dist/esm/session.d.ts +51 -0
- package/dist/esm/session.js +58 -0
- package/dist/esm/store.d.ts +58 -0
- package/dist/esm/store.js +119 -0
- package/dist/esm/tokens.d.ts +38 -0
- package/dist/esm/tokens.js +100 -0
- package/package.json +152 -5
package/dist/cjs/hono.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var hono_exports = {};
|
|
20
|
+
__export(hono_exports, {
|
|
21
|
+
IdpError: () => import_errors2.IdpError,
|
|
22
|
+
SIGN_IN_ERROR_CODES: () => import_errors2.SIGN_IN_ERROR_CODES,
|
|
23
|
+
idp: () => idp,
|
|
24
|
+
isIdpError: () => import_errors2.isIdpError,
|
|
25
|
+
isSignInErrorCode: () => import_errors2.isSignInErrorCode,
|
|
26
|
+
isTokenError: () => import_errors2.isTokenError,
|
|
27
|
+
memoryStore: () => import_store.memoryStore
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(hono_exports);
|
|
30
|
+
var import_errors = require("./errors.js");
|
|
31
|
+
var import_idp = require("./idp.js");
|
|
32
|
+
var import_errors2 = require("./errors.js");
|
|
33
|
+
var import_store = require("./store.js");
|
|
34
|
+
function putCookies(c, cookies) {
|
|
35
|
+
if (cookies.length === 0) return;
|
|
36
|
+
try {
|
|
37
|
+
for (const cookie of cookies) c.res.headers.append("set-cookie", cookie);
|
|
38
|
+
} catch {
|
|
39
|
+
const copy = new Response(c.res.body, c.res);
|
|
40
|
+
for (const cookie of cookies) copy.headers.append("set-cookie", cookie);
|
|
41
|
+
c.res = void 0;
|
|
42
|
+
c.res = copy;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function underBasePath(pathname, basePath) {
|
|
46
|
+
return pathname === basePath || pathname.startsWith(`${basePath}/`);
|
|
47
|
+
}
|
|
48
|
+
function idp(options) {
|
|
49
|
+
const core = (0, import_idp.createIdp)(options);
|
|
50
|
+
const sessionOf = async (c, refresh) => {
|
|
51
|
+
if (!refresh) return { session: await core.getSession(c.req.raw), setCookies: [] };
|
|
52
|
+
const { session, setCookies } = await core.refreshIfNeeded(c.req.raw);
|
|
53
|
+
return { session, setCookies };
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
...core,
|
|
57
|
+
routes: () => (c) => core.handler(c.req.raw),
|
|
58
|
+
middleware({ refresh = true } = {}) {
|
|
59
|
+
return async (c, next) => {
|
|
60
|
+
if (underBasePath(new URL(c.req.url).pathname, core.config().basePath)) {
|
|
61
|
+
await next();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const { session, setCookies } = await sessionOf(c, refresh);
|
|
65
|
+
c.set("session", session);
|
|
66
|
+
await next();
|
|
67
|
+
putCookies(c, setCookies);
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
requireSession() {
|
|
71
|
+
return async (c, next) => {
|
|
72
|
+
const known = c.get("session");
|
|
73
|
+
const { session, setCookies } = known === void 0 ? await sessionOf(c, true) : { session: known, setCookies: [] };
|
|
74
|
+
if (session) {
|
|
75
|
+
c.set("session", session);
|
|
76
|
+
await next();
|
|
77
|
+
putCookies(c, setCookies);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const method = c.req.method.toUpperCase();
|
|
81
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
82
|
+
c.res = c.text("Sign in first.", 401, { "cache-control": "no-store" });
|
|
83
|
+
} else {
|
|
84
|
+
const url = new URL(c.req.url);
|
|
85
|
+
const signIn = new URL(
|
|
86
|
+
core.signInPath(`${url.pathname}${url.search}`),
|
|
87
|
+
core.config().appUrl
|
|
88
|
+
);
|
|
89
|
+
c.res = c.redirect(signIn.toString(), 302);
|
|
90
|
+
c.res.headers.set("cache-control", "no-store");
|
|
91
|
+
}
|
|
92
|
+
putCookies(c, setCookies);
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
requireToken(requirements = {}) {
|
|
96
|
+
return async (c, next) => {
|
|
97
|
+
let token;
|
|
98
|
+
try {
|
|
99
|
+
token = await core.verifyBearer(c.req.header("authorization"), requirements);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if ((0, import_errors.isTokenError)(error)) return core.tokenErrorResponse(error);
|
|
102
|
+
console.error(
|
|
103
|
+
`nextidp: a token couldn't be checked: ${(0, import_errors.isIdpError)(error) ? error.code : "unexpected error"}`
|
|
104
|
+
);
|
|
105
|
+
return c.json(
|
|
106
|
+
{
|
|
107
|
+
error: "temporarily_unavailable",
|
|
108
|
+
error_description: "The token can't be checked now."
|
|
109
|
+
},
|
|
110
|
+
503,
|
|
111
|
+
{ "retry-after": "5", "cache-control": "no-store" }
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
c.set("token", token);
|
|
115
|
+
await next();
|
|
116
|
+
return void 0;
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
122
|
+
0 && (module.exports = {
|
|
123
|
+
IdpError,
|
|
124
|
+
SIGN_IN_ERROR_CODES,
|
|
125
|
+
idp,
|
|
126
|
+
isIdpError,
|
|
127
|
+
isSignInErrorCode,
|
|
128
|
+
isTokenError,
|
|
129
|
+
memoryStore
|
|
130
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type Answer = {
|
|
2
|
+
ok: boolean;
|
|
3
|
+
status: number;
|
|
4
|
+
data: unknown;
|
|
5
|
+
};
|
|
6
|
+
export type Http = {
|
|
7
|
+
get(url: string, headers?: Record<string, string>): Promise<Answer>;
|
|
8
|
+
postForm(url: string, form: URLSearchParams, headers?: Record<string, string>): Promise<Answer>;
|
|
9
|
+
};
|
|
10
|
+
export declare function createIdpHttp(): Http;
|
package/dist/cjs/http.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var http_exports = {};
|
|
20
|
+
__export(http_exports, {
|
|
21
|
+
createIdpHttp: () => createIdpHttp
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(http_exports);
|
|
24
|
+
var import_fast_http_client = require("fast-http-client");
|
|
25
|
+
function bare(error) {
|
|
26
|
+
return new Error(error instanceof Error ? error.message : "The request failed.");
|
|
27
|
+
}
|
|
28
|
+
function refusal(error) {
|
|
29
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
30
|
+
const { status, data } = error;
|
|
31
|
+
return typeof status === "number" ? { ok: false, status, data } : void 0;
|
|
32
|
+
}
|
|
33
|
+
function createIdpHttp() {
|
|
34
|
+
const client = (0, import_fast_http_client.createHttp)({
|
|
35
|
+
timeoutMs: 1e4,
|
|
36
|
+
headers: { accept: "application/json" },
|
|
37
|
+
circuitBreaker: false
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
async get(url, headers) {
|
|
41
|
+
try {
|
|
42
|
+
const response = await client.get(url, headers ? { headers } : {});
|
|
43
|
+
return { ok: true, status: response.status, data: response.data };
|
|
44
|
+
} catch (error) {
|
|
45
|
+
const answer = refusal(error);
|
|
46
|
+
if (answer) return answer;
|
|
47
|
+
throw bare(error);
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
async postForm(url, form, headers) {
|
|
51
|
+
try {
|
|
52
|
+
const response = await client.post(url, form, {
|
|
53
|
+
retry: false,
|
|
54
|
+
headers: { "content-type": "application/x-www-form-urlencoded", ...headers }
|
|
55
|
+
});
|
|
56
|
+
return { ok: true, status: response.status, data: response.data };
|
|
57
|
+
} catch (error) {
|
|
58
|
+
const answer = refusal(error);
|
|
59
|
+
if (answer) return answer;
|
|
60
|
+
throw bare(error);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
66
|
+
0 && (module.exports = {
|
|
67
|
+
createIdpHttp
|
|
68
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type JWTPayload, type JWTVerifyGetKey } from "jose";
|
|
2
|
+
export type IdTokenCheck = {
|
|
3
|
+
issuer: string;
|
|
4
|
+
clientId: string;
|
|
5
|
+
/** The nonce the authorization request sent. Absent for a token from a refresh. */
|
|
6
|
+
nonce?: string | undefined;
|
|
7
|
+
/** What discovery lists, if anything; narrowed to ALLOWED_ALGORITHMS. */
|
|
8
|
+
algorithms?: readonly string[] | undefined;
|
|
9
|
+
};
|
|
10
|
+
/** What discovery lists, narrowed to ALLOWED_ALGORITHMS; RS256 when it lists none of them. */
|
|
11
|
+
export declare function signingAlgorithms(listed: readonly string[] | undefined): string[];
|
|
12
|
+
/** The issuer's public keys from its jwks_uri: fetched once, again for a new `kid`. */
|
|
13
|
+
export declare function createKeys(): (jwksUri: string) => JWTVerifyGetKey;
|
|
14
|
+
/**
|
|
15
|
+
* Verifies an ID token (OIDC Core 3.1.3.7): a signature by one of the issuer's keys, `iss`
|
|
16
|
+
* the issuer, `aud` this client (and `azp` too when there are several audiences), `sub`,
|
|
17
|
+
* `iat`, not expired, and the nonce this sign-in sent.
|
|
18
|
+
*/
|
|
19
|
+
export declare function verifyIdToken(idToken: string, keys: JWTVerifyGetKey, check: IdTokenCheck): Promise<JWTPayload>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var id_token_exports = {};
|
|
20
|
+
__export(id_token_exports, {
|
|
21
|
+
createKeys: () => createKeys,
|
|
22
|
+
signingAlgorithms: () => signingAlgorithms,
|
|
23
|
+
verifyIdToken: () => verifyIdToken
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(id_token_exports);
|
|
26
|
+
var import_jose = require("jose");
|
|
27
|
+
var import_errors = require("./errors.js");
|
|
28
|
+
var import_pkce = require("./pkce.js");
|
|
29
|
+
const ALLOWED_ALGORITHMS = ["RS256", "PS256", "ES256", "EdDSA"];
|
|
30
|
+
function signingAlgorithms(listed) {
|
|
31
|
+
const algorithms = listed ? listed.filter((alg) => ALLOWED_ALGORITHMS.includes(alg)) : [];
|
|
32
|
+
return algorithms.length > 0 ? algorithms : ["RS256"];
|
|
33
|
+
}
|
|
34
|
+
function createKeys() {
|
|
35
|
+
const sets = /* @__PURE__ */ new Map();
|
|
36
|
+
return (jwksUri) => {
|
|
37
|
+
let keys = sets.get(jwksUri);
|
|
38
|
+
if (!keys) {
|
|
39
|
+
keys = (0, import_jose.createRemoteJWKSet)(new URL(jwksUri), { cooldownDuration: 6e4 });
|
|
40
|
+
sets.set(jwksUri, keys);
|
|
41
|
+
}
|
|
42
|
+
return keys;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async function verifyIdToken(idToken, keys, check) {
|
|
46
|
+
let payload;
|
|
47
|
+
try {
|
|
48
|
+
({ payload } = await (0, import_jose.jwtVerify)(idToken, keys, {
|
|
49
|
+
algorithms: signingAlgorithms(check.algorithms),
|
|
50
|
+
issuer: check.issuer,
|
|
51
|
+
audience: check.clientId,
|
|
52
|
+
requiredClaims: ["sub", "iat", "exp"],
|
|
53
|
+
clockTolerance: 30
|
|
54
|
+
}));
|
|
55
|
+
} catch (cause) {
|
|
56
|
+
throw new import_errors.IdpError("invalid_id_token", "The ID token didn't verify.", { cause });
|
|
57
|
+
}
|
|
58
|
+
const audiences = Array.isArray(payload.aud) ? payload.aud : [payload.aud];
|
|
59
|
+
if ((audiences.length > 1 || payload.azp !== void 0) && payload.azp !== check.clientId) {
|
|
60
|
+
throw new import_errors.IdpError("invalid_id_token", "The ID token was issued to another party (azp).");
|
|
61
|
+
}
|
|
62
|
+
if (check.nonce !== void 0) {
|
|
63
|
+
if (typeof payload.nonce !== "string" || !(0, import_pkce.sameString)(payload.nonce, check.nonce)) {
|
|
64
|
+
throw new import_errors.IdpError(
|
|
65
|
+
"invalid_id_token",
|
|
66
|
+
"The ID token's nonce isn't the one this sign-in sent."
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return payload;
|
|
71
|
+
}
|
|
72
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
73
|
+
0 && (module.exports = {
|
|
74
|
+
createKeys,
|
|
75
|
+
signingAlgorithms,
|
|
76
|
+
verifyIdToken
|
|
77
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { type AccessToken, type TokenRequirements } from "./access-token.js";
|
|
2
|
+
import { type ApiConfig, type IdpConfig, type IdpOptions } from "./config.js";
|
|
3
|
+
import { IdpError } from "./errors.js";
|
|
4
|
+
import { type InterceptableClient, type ServiceTokenOptions, type UseServiceTokenOptions } from "./service.js";
|
|
5
|
+
import { type Session } from "./session.js";
|
|
6
|
+
/** Where cookies are read from: a request, its headers, or the Cookie header itself. */
|
|
7
|
+
export type CookieSource = Request | Headers | string | null | undefined;
|
|
8
|
+
/** The session after `refreshIfNeeded`, and what to tell the browser and the rest of the request. */
|
|
9
|
+
export type Refreshed = {
|
|
10
|
+
session: Session | null;
|
|
11
|
+
/** Set-Cookie headers for the response: a new session, or the old one expired. */
|
|
12
|
+
setCookies: string[];
|
|
13
|
+
/** The Cookie header the rest of this request should see, when the cookies changed. */
|
|
14
|
+
cookieHeader: string | undefined;
|
|
15
|
+
};
|
|
16
|
+
/** What `idp()` returns, whatever the framework. */
|
|
17
|
+
export type Idp = {
|
|
18
|
+
/** The checked settings. Throws IdpError("misconfigured") naming what's wrong. */
|
|
19
|
+
config(): IdpConfig;
|
|
20
|
+
/**
|
|
21
|
+
* Answers nextidp's routes under `basePath`: `GET /login`, `GET /callback`,
|
|
22
|
+
* `POST /logout`, `POST /refresh`, and with a store `POST /backchannel-logout`. Anything
|
|
23
|
+
* else under it is a 404.
|
|
24
|
+
*/
|
|
25
|
+
handler(request: Request): Promise<Response>;
|
|
26
|
+
/** The signed-in session, or null. Doesn't refresh: see `refreshIfNeeded`. */
|
|
27
|
+
getSession(source: CookieSource): Promise<Session | null>;
|
|
28
|
+
/**
|
|
29
|
+
* The session, refreshed first when its access token expires within `leeway` seconds
|
|
30
|
+
* (default 60). Use it where cookies can be written: a proxy, a route handler, middleware.
|
|
31
|
+
*/
|
|
32
|
+
refreshIfNeeded(source: CookieSource, options?: {
|
|
33
|
+
leeway?: number;
|
|
34
|
+
}): Promise<Refreshed>;
|
|
35
|
+
/** The userinfo endpoint's claims for an access token. */
|
|
36
|
+
userInfo(accessToken: string): Promise<Record<string, unknown>>;
|
|
37
|
+
/** The path that starts a sign-in, returning to `returnTo` (a path of this app) after. */
|
|
38
|
+
signInPath(returnTo?: string): string;
|
|
39
|
+
/** For an API: the checked settings it needs (issuer and audience). */
|
|
40
|
+
apiConfig(): ApiConfig;
|
|
41
|
+
/**
|
|
42
|
+
* Checks an access token an API was called with: a JWT against the issuer's keys, an
|
|
43
|
+
* opaque one by introspection. Throws IdpError invalid_token (401) or insufficient_scope
|
|
44
|
+
* (403); `tokenErrorResponse` turns either into the answer.
|
|
45
|
+
*/
|
|
46
|
+
verifyAccessToken(token: string, requirements?: TokenRequirements): Promise<AccessToken>;
|
|
47
|
+
/** `verifyAccessToken` for an `Authorization` header; none at all is missing_token (401). */
|
|
48
|
+
verifyBearer(authorization: string | null | undefined, requirements?: TokenRequirements): Promise<AccessToken>;
|
|
49
|
+
/** The 401 or 403 for a refused token, with its WWW-Authenticate challenge (RFC 6750). */
|
|
50
|
+
tokenErrorResponse(error: IdpError): Response;
|
|
51
|
+
/**
|
|
52
|
+
* This service's own access token (client credentials), to call another API as itself.
|
|
53
|
+
* Kept until a minute before it expires, per scopes and API; calls at the same time share
|
|
54
|
+
* one request. Needs a confidential client: `clientId` and `clientSecret`. Throws
|
|
55
|
+
* IdpError("service_token_failed") when the auth server refuses or doesn't answer.
|
|
56
|
+
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* const token = await auth.serviceToken({ scopes: ["orders:read"], resource: "https://orders.example.com" });
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
serviceToken(options?: ServiceTokenOptions): Promise<string>;
|
|
62
|
+
/** Forgets a kept service token an API refused (401), so the next call asks for a new one. */
|
|
63
|
+
dropServiceToken(token: string): void;
|
|
64
|
+
/**
|
|
65
|
+
* Puts a service token on every request of a fast-http-client client to the API (the origin
|
|
66
|
+
* of `resource`, or `origins`), and nowhere else; on a 401 it gets a new one and sends the
|
|
67
|
+
* request once more. Returns a function that undoes it.
|
|
68
|
+
*
|
|
69
|
+
* ```ts
|
|
70
|
+
* const orders = createHttp({ baseUrl: "https://orders.example.com" });
|
|
71
|
+
* auth.useServiceToken(orders, { scopes: ["orders:read"], resource: "https://orders.example.com" });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
useServiceToken(client: InterceptableClient, options?: UseServiceTokenOptions): () => void;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* The client for one app. Settings are checked on first use, not here, so a module that
|
|
78
|
+
* calls `idp()` can be imported where they aren't set (a build). Pass a function to read
|
|
79
|
+
* them only then, too.
|
|
80
|
+
*/
|
|
81
|
+
export declare function createIdp(options?: IdpOptions | (() => IdpOptions)): Idp;
|