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
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Context, MiddlewareHandler } from "hono";
|
|
2
|
+
import type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
3
|
+
import type { IdpOptions } from "./config.js";
|
|
4
|
+
import { type Idp } from "./idp.js";
|
|
5
|
+
import type { Session } from "./session.js";
|
|
6
|
+
/**
|
|
7
|
+
* The context variables nextidp sets: `session` (by `middleware()`, null when signed out) and
|
|
8
|
+
* `token` (by `requireToken()`). Give it to your app: `new Hono<IdpEnv>()`.
|
|
9
|
+
*/
|
|
10
|
+
export type IdpEnv = {
|
|
11
|
+
Variables: {
|
|
12
|
+
session: Session | null;
|
|
13
|
+
token: AccessToken;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export type HonoSessionOptions = {
|
|
17
|
+
/** Refresh the access token when it expires within a minute (default true). */
|
|
18
|
+
refresh?: boolean;
|
|
19
|
+
};
|
|
20
|
+
export type HonoIdp = Idp & {
|
|
21
|
+
/** nextidp's routes, for `app.all("/auth/*", auth.routes())` (under `basePath`). */
|
|
22
|
+
routes(): (c: Context) => Promise<Response>;
|
|
23
|
+
/**
|
|
24
|
+
* Reads the session for every request (refreshing the access token when it's about to
|
|
25
|
+
* expire, with the new cookie on the response) and puts it on the context:
|
|
26
|
+
* `c.get("session")`, null when signed out. `app.use(auth.middleware())`.
|
|
27
|
+
*/
|
|
28
|
+
middleware(options?: HonoSessionOptions): MiddlewareHandler<IdpEnv>;
|
|
29
|
+
/**
|
|
30
|
+
* Lets a request through only with a session. Signed out, a GET or HEAD goes to sign in and
|
|
31
|
+
* comes back after; anything else gets 401.
|
|
32
|
+
*/
|
|
33
|
+
requireSession(): MiddlewareHandler<IdpEnv>;
|
|
34
|
+
/**
|
|
35
|
+
* Lets a request through only with a valid access token that has every scope in
|
|
36
|
+
* `requirements`, put on the context as `c.get("token")`. A refused one gets 401 or 403 with
|
|
37
|
+
* WWW-Authenticate (RFC 6750); one that can't be checked now (the auth server is down) 503.
|
|
38
|
+
*/
|
|
39
|
+
requireToken(requirements?: TokenRequirements): MiddlewareHandler<IdpEnv>;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* The client for a Hono app or API:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { Hono } from "hono";
|
|
46
|
+
* import { idp, type IdpEnv } from "nextidp/hono";
|
|
47
|
+
*
|
|
48
|
+
* const auth = idp({ issuer: "https://auth.example.com", clientId: "…", appUrl: "…", secret: "…" });
|
|
49
|
+
* const app = new Hono<IdpEnv>();
|
|
50
|
+
* app.all("/auth/*", auth.routes());
|
|
51
|
+
* app.use(auth.middleware());
|
|
52
|
+
* app.get("/dashboard", auth.requireSession(), (c) => c.text(`Hello, ${c.get("session")?.user.name}`));
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* On a runtime without `process.env` (Cloudflare Workers), pass the settings, or `env`:
|
|
56
|
+
* `idp(() => ({ env: workerEnv }))`.
|
|
57
|
+
*/
|
|
58
|
+
export declare function idp(options?: IdpOptions | (() => IdpOptions)): HonoIdp;
|
|
59
|
+
export type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
60
|
+
export type { ApiConfig, IdpConfig, IdpOptions } from "./config.js";
|
|
61
|
+
export { IdpError, type IdpErrorCode, isIdpError, isSignInErrorCode, isTokenError, SIGN_IN_ERROR_CODES, type SignInErrorCode, type TokenErrorCode, } from "./errors.js";
|
|
62
|
+
export type { Idp, Refreshed } from "./idp.js";
|
|
63
|
+
export type { InterceptableClient, ServiceTokenOptions, UseServiceTokenOptions, } from "./service.js";
|
|
64
|
+
export type { IdpUser, Session } from "./session.js";
|
|
65
|
+
export { memoryStore, type SessionStore } from "./store.js";
|
package/dist/esm/hono.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { isIdpError, isTokenError } from "./errors.js";
|
|
2
|
+
import { createIdp } from "./idp.js";
|
|
3
|
+
function putCookies(c, cookies) {
|
|
4
|
+
if (cookies.length === 0) return;
|
|
5
|
+
try {
|
|
6
|
+
for (const cookie of cookies) c.res.headers.append("set-cookie", cookie);
|
|
7
|
+
} catch {
|
|
8
|
+
const copy = new Response(c.res.body, c.res);
|
|
9
|
+
for (const cookie of cookies) copy.headers.append("set-cookie", cookie);
|
|
10
|
+
c.res = void 0;
|
|
11
|
+
c.res = copy;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function underBasePath(pathname, basePath) {
|
|
15
|
+
return pathname === basePath || pathname.startsWith(`${basePath}/`);
|
|
16
|
+
}
|
|
17
|
+
function idp(options) {
|
|
18
|
+
const core = createIdp(options);
|
|
19
|
+
const sessionOf = async (c, refresh) => {
|
|
20
|
+
if (!refresh) return { session: await core.getSession(c.req.raw), setCookies: [] };
|
|
21
|
+
const { session, setCookies } = await core.refreshIfNeeded(c.req.raw);
|
|
22
|
+
return { session, setCookies };
|
|
23
|
+
};
|
|
24
|
+
return {
|
|
25
|
+
...core,
|
|
26
|
+
routes: () => (c) => core.handler(c.req.raw),
|
|
27
|
+
middleware({ refresh = true } = {}) {
|
|
28
|
+
return async (c, next) => {
|
|
29
|
+
if (underBasePath(new URL(c.req.url).pathname, core.config().basePath)) {
|
|
30
|
+
await next();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const { session, setCookies } = await sessionOf(c, refresh);
|
|
34
|
+
c.set("session", session);
|
|
35
|
+
await next();
|
|
36
|
+
putCookies(c, setCookies);
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
requireSession() {
|
|
40
|
+
return async (c, next) => {
|
|
41
|
+
const known = c.get("session");
|
|
42
|
+
const { session, setCookies } = known === void 0 ? await sessionOf(c, true) : { session: known, setCookies: [] };
|
|
43
|
+
if (session) {
|
|
44
|
+
c.set("session", session);
|
|
45
|
+
await next();
|
|
46
|
+
putCookies(c, setCookies);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const method = c.req.method.toUpperCase();
|
|
50
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
51
|
+
c.res = c.text("Sign in first.", 401, { "cache-control": "no-store" });
|
|
52
|
+
} else {
|
|
53
|
+
const url = new URL(c.req.url);
|
|
54
|
+
const signIn = new URL(
|
|
55
|
+
core.signInPath(`${url.pathname}${url.search}`),
|
|
56
|
+
core.config().appUrl
|
|
57
|
+
);
|
|
58
|
+
c.res = c.redirect(signIn.toString(), 302);
|
|
59
|
+
c.res.headers.set("cache-control", "no-store");
|
|
60
|
+
}
|
|
61
|
+
putCookies(c, setCookies);
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
requireToken(requirements = {}) {
|
|
65
|
+
return async (c, next) => {
|
|
66
|
+
let token;
|
|
67
|
+
try {
|
|
68
|
+
token = await core.verifyBearer(c.req.header("authorization"), requirements);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (isTokenError(error)) return core.tokenErrorResponse(error);
|
|
71
|
+
console.error(
|
|
72
|
+
`nextidp: a token couldn't be checked: ${isIdpError(error) ? error.code : "unexpected error"}`
|
|
73
|
+
);
|
|
74
|
+
return c.json(
|
|
75
|
+
{
|
|
76
|
+
error: "temporarily_unavailable",
|
|
77
|
+
error_description: "The token can't be checked now."
|
|
78
|
+
},
|
|
79
|
+
503,
|
|
80
|
+
{ "retry-after": "5", "cache-control": "no-store" }
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
c.set("token", token);
|
|
84
|
+
await next();
|
|
85
|
+
return void 0;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
import {
|
|
91
|
+
IdpError,
|
|
92
|
+
isIdpError as isIdpError2,
|
|
93
|
+
isSignInErrorCode,
|
|
94
|
+
isTokenError as isTokenError2,
|
|
95
|
+
SIGN_IN_ERROR_CODES
|
|
96
|
+
} from "./errors.js";
|
|
97
|
+
import { memoryStore } from "./store.js";
|
|
98
|
+
export {
|
|
99
|
+
IdpError,
|
|
100
|
+
SIGN_IN_ERROR_CODES,
|
|
101
|
+
idp,
|
|
102
|
+
isIdpError2 as isIdpError,
|
|
103
|
+
isSignInErrorCode,
|
|
104
|
+
isTokenError2 as isTokenError,
|
|
105
|
+
memoryStore
|
|
106
|
+
};
|
|
@@ -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/esm/http.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createHttp } from "fast-http-client";
|
|
2
|
+
function bare(error) {
|
|
3
|
+
return new Error(error instanceof Error ? error.message : "The request failed.");
|
|
4
|
+
}
|
|
5
|
+
function refusal(error) {
|
|
6
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
7
|
+
const { status, data } = error;
|
|
8
|
+
return typeof status === "number" ? { ok: false, status, data } : void 0;
|
|
9
|
+
}
|
|
10
|
+
function createIdpHttp() {
|
|
11
|
+
const client = createHttp({
|
|
12
|
+
timeoutMs: 1e4,
|
|
13
|
+
headers: { accept: "application/json" },
|
|
14
|
+
circuitBreaker: false
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
async get(url, headers) {
|
|
18
|
+
try {
|
|
19
|
+
const response = await client.get(url, headers ? { headers } : {});
|
|
20
|
+
return { ok: true, status: response.status, data: response.data };
|
|
21
|
+
} catch (error) {
|
|
22
|
+
const answer = refusal(error);
|
|
23
|
+
if (answer) return answer;
|
|
24
|
+
throw bare(error);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
async postForm(url, form, headers) {
|
|
28
|
+
try {
|
|
29
|
+
const response = await client.post(url, form, {
|
|
30
|
+
retry: false,
|
|
31
|
+
headers: { "content-type": "application/x-www-form-urlencoded", ...headers }
|
|
32
|
+
});
|
|
33
|
+
return { ok: true, status: response.status, data: response.data };
|
|
34
|
+
} catch (error) {
|
|
35
|
+
const answer = refusal(error);
|
|
36
|
+
if (answer) return answer;
|
|
37
|
+
throw bare(error);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
createIdpHttp
|
|
44
|
+
};
|
|
@@ -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,51 @@
|
|
|
1
|
+
import { createRemoteJWKSet, jwtVerify } from "jose";
|
|
2
|
+
import { IdpError } from "./errors.js";
|
|
3
|
+
import { sameString } from "./pkce.js";
|
|
4
|
+
const ALLOWED_ALGORITHMS = ["RS256", "PS256", "ES256", "EdDSA"];
|
|
5
|
+
function signingAlgorithms(listed) {
|
|
6
|
+
const algorithms = listed ? listed.filter((alg) => ALLOWED_ALGORITHMS.includes(alg)) : [];
|
|
7
|
+
return algorithms.length > 0 ? algorithms : ["RS256"];
|
|
8
|
+
}
|
|
9
|
+
function createKeys() {
|
|
10
|
+
const sets = /* @__PURE__ */ new Map();
|
|
11
|
+
return (jwksUri) => {
|
|
12
|
+
let keys = sets.get(jwksUri);
|
|
13
|
+
if (!keys) {
|
|
14
|
+
keys = createRemoteJWKSet(new URL(jwksUri), { cooldownDuration: 6e4 });
|
|
15
|
+
sets.set(jwksUri, keys);
|
|
16
|
+
}
|
|
17
|
+
return keys;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
async function verifyIdToken(idToken, keys, check) {
|
|
21
|
+
let payload;
|
|
22
|
+
try {
|
|
23
|
+
({ payload } = await jwtVerify(idToken, keys, {
|
|
24
|
+
algorithms: signingAlgorithms(check.algorithms),
|
|
25
|
+
issuer: check.issuer,
|
|
26
|
+
audience: check.clientId,
|
|
27
|
+
requiredClaims: ["sub", "iat", "exp"],
|
|
28
|
+
clockTolerance: 30
|
|
29
|
+
}));
|
|
30
|
+
} catch (cause) {
|
|
31
|
+
throw new IdpError("invalid_id_token", "The ID token didn't verify.", { cause });
|
|
32
|
+
}
|
|
33
|
+
const audiences = Array.isArray(payload.aud) ? payload.aud : [payload.aud];
|
|
34
|
+
if ((audiences.length > 1 || payload.azp !== void 0) && payload.azp !== check.clientId) {
|
|
35
|
+
throw new IdpError("invalid_id_token", "The ID token was issued to another party (azp).");
|
|
36
|
+
}
|
|
37
|
+
if (check.nonce !== void 0) {
|
|
38
|
+
if (typeof payload.nonce !== "string" || !sameString(payload.nonce, check.nonce)) {
|
|
39
|
+
throw new IdpError(
|
|
40
|
+
"invalid_id_token",
|
|
41
|
+
"The ID token's nonce isn't the one this sign-in sent."
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return payload;
|
|
46
|
+
}
|
|
47
|
+
export {
|
|
48
|
+
createKeys,
|
|
49
|
+
signingAlgorithms,
|
|
50
|
+
verifyIdToken
|
|
51
|
+
};
|
|
@@ -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;
|