nextidp 0.0.1 → 0.1.2

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.
Files changed (97) hide show
  1. package/CHANGELOG.md +85 -0
  2. package/README.md +434 -4
  3. package/dist/cjs/access-token.d.ts +56 -0
  4. package/dist/cjs/access-token.js +196 -0
  5. package/dist/cjs/config.d.ts +128 -0
  6. package/dist/cjs/config.js +233 -0
  7. package/dist/cjs/cookies.d.ts +23 -0
  8. package/dist/cjs/cookies.js +118 -0
  9. package/dist/cjs/discovery.d.ts +24 -0
  10. package/dist/cjs/discovery.js +91 -0
  11. package/dist/cjs/errors.d.ts +63 -0
  12. package/dist/cjs/errors.js +69 -0
  13. package/dist/cjs/express.d.ts +71 -0
  14. package/dist/cjs/express.js +114 -0
  15. package/dist/cjs/http.d.ts +10 -0
  16. package/dist/cjs/http.js +68 -0
  17. package/dist/cjs/id-token.d.ts +19 -0
  18. package/dist/cjs/id-token.js +77 -0
  19. package/dist/cjs/idp.d.ts +81 -0
  20. package/dist/cjs/idp.js +482 -0
  21. package/dist/cjs/index.d.ts +9 -0
  22. package/dist/cjs/index.js +53 -0
  23. package/dist/cjs/logout-token.d.ts +13 -0
  24. package/dist/cjs/logout-token.js +63 -0
  25. package/dist/cjs/nestjs.d.ts +53 -0
  26. package/dist/cjs/nestjs.js +142 -0
  27. package/dist/cjs/next.d.ts +61 -0
  28. package/dist/cjs/next.js +118 -0
  29. package/dist/cjs/node-adapter.d.ts +16 -0
  30. package/dist/cjs/node-adapter.js +76 -0
  31. package/dist/cjs/node-http.d.ts +22 -0
  32. package/dist/cjs/node-http.js +127 -0
  33. package/dist/cjs/node.d.ts +50 -0
  34. package/dist/cjs/node.js +57 -0
  35. package/dist/cjs/package.json +1 -0
  36. package/dist/cjs/pkce.d.ts +6 -0
  37. package/dist/cjs/pkce.js +49 -0
  38. package/dist/cjs/return-path.d.ts +6 -0
  39. package/dist/cjs/return-path.js +41 -0
  40. package/dist/cjs/sealed.d.ts +12 -0
  41. package/dist/cjs/sealed.js +78 -0
  42. package/dist/cjs/service.d.ts +61 -0
  43. package/dist/cjs/service.js +173 -0
  44. package/dist/cjs/session.d.ts +51 -0
  45. package/dist/cjs/session.js +97 -0
  46. package/dist/cjs/store.d.ts +58 -0
  47. package/dist/cjs/store.js +145 -0
  48. package/dist/cjs/tokens.d.ts +38 -0
  49. package/dist/cjs/tokens.js +138 -0
  50. package/dist/esm/access-token.d.ts +56 -0
  51. package/dist/esm/access-token.js +156 -0
  52. package/dist/esm/config.d.ts +128 -0
  53. package/dist/esm/config.js +206 -0
  54. package/dist/esm/cookies.d.ts +23 -0
  55. package/dist/esm/cookies.js +89 -0
  56. package/dist/esm/discovery.d.ts +24 -0
  57. package/dist/esm/discovery.js +56 -0
  58. package/dist/esm/errors.d.ts +63 -0
  59. package/dist/esm/errors.js +41 -0
  60. package/dist/esm/express.d.ts +71 -0
  61. package/dist/esm/express.js +90 -0
  62. package/dist/esm/http.d.ts +10 -0
  63. package/dist/esm/http.js +44 -0
  64. package/dist/esm/id-token.d.ts +19 -0
  65. package/dist/esm/id-token.js +51 -0
  66. package/dist/esm/idp.d.ts +81 -0
  67. package/dist/esm/idp.js +488 -0
  68. package/dist/esm/index.d.ts +9 -0
  69. package/dist/esm/index.js +30 -0
  70. package/dist/esm/logout-token.d.ts +13 -0
  71. package/dist/esm/logout-token.js +39 -0
  72. package/dist/esm/nestjs.d.ts +53 -0
  73. package/dist/esm/nestjs.js +118 -0
  74. package/dist/esm/next.d.ts +61 -0
  75. package/dist/esm/next.js +94 -0
  76. package/dist/esm/node-adapter.d.ts +16 -0
  77. package/dist/esm/node-adapter.js +60 -0
  78. package/dist/esm/node-http.d.ts +22 -0
  79. package/dist/esm/node-http.js +96 -0
  80. package/dist/esm/node.d.ts +50 -0
  81. package/dist/esm/node.js +33 -0
  82. package/dist/esm/package.json +1 -0
  83. package/dist/esm/pkce.d.ts +6 -0
  84. package/dist/esm/pkce.js +23 -0
  85. package/dist/esm/return-path.d.ts +6 -0
  86. package/dist/esm/return-path.js +17 -0
  87. package/dist/esm/sealed.d.ts +12 -0
  88. package/dist/esm/sealed.js +54 -0
  89. package/dist/esm/service.d.ts +61 -0
  90. package/dist/esm/service.js +148 -0
  91. package/dist/esm/session.d.ts +51 -0
  92. package/dist/esm/session.js +58 -0
  93. package/dist/esm/store.d.ts +58 -0
  94. package/dist/esm/store.js +119 -0
  95. package/dist/esm/tokens.d.ts +38 -0
  96. package/dist/esm/tokens.js +100 -0
  97. package/package.json +130 -5
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Why a sign-in didn't finish. The app is sent to its `errorPath` with `?error=<code>`, so it
3
+ * can say what happened; these are the only codes it ever gets there.
4
+ */
5
+ export type SignInErrorCode =
6
+ /** The person chose Deny on the auth server. */
7
+ "access_denied"
8
+ /** The sign-in took longer than 10 minutes, or its cookie was lost. */
9
+ | "expired"
10
+ /** The answer wasn't for a sign-in this browser started. */
11
+ | "state_mismatch"
12
+ /** The auth server refused the code, or answered with an error. */
13
+ | "token_failed"
14
+ /** The ID token's signature, issuer, audience, expiry or nonce was wrong. */
15
+ | "invalid_id_token"
16
+ /** The refresh token was refused: used, revoked or expired. */
17
+ | "refresh_failed";
18
+ export declare const SIGN_IN_ERROR_CODES: readonly SignInErrorCode[];
19
+ export declare function isSignInErrorCode(value: unknown): value is SignInErrorCode;
20
+ export type IdpErrorCode = SignInErrorCode
21
+ /** A setting is missing or wrong: the message names it. */
22
+ | "misconfigured"
23
+ /** The auth server's discovery document couldn't be fetched or isn't valid. */
24
+ | "discovery_failed"
25
+ /** The userinfo endpoint refused the access token or didn't answer. */
26
+ | "userinfo_failed"
27
+ /** An API was called without an access token (401, no error code in the challenge). */
28
+ | "missing_token"
29
+ /** The access token is expired, revoked, for another API, or not valid at all (401). */
30
+ | "invalid_token"
31
+ /** The access token is valid but lacks a scope this route needs (403). */
32
+ | "insufficient_scope"
33
+ /** The token endpoint refused this service's client credentials, or didn't answer. */
34
+ | "service_token_failed"
35
+ /** A back-channel logout request's token didn't verify, or isn't a logout token. */
36
+ | "invalid_logout_token";
37
+ /** The three codes an API answers a caller with; see `tokenErrorResponse`. */
38
+ export type TokenErrorCode = "missing_token" | "invalid_token" | "insufficient_scope";
39
+ export declare function isTokenError(error: unknown): error is IdpError & {
40
+ code: TokenErrorCode;
41
+ };
42
+ /**
43
+ * Every error nextidp throws. Its message never holds a token, a secret or a claim.
44
+ * Check it with `isIdpError`: `instanceof` can fail when an app loads both the ES module and
45
+ * the CommonJS build.
46
+ */
47
+ export declare class IdpError extends Error {
48
+ readonly name = "IdpError";
49
+ readonly code: IdpErrorCode;
50
+ /**
51
+ * The HTTP status the auth server answered with, when there was one; for a token error,
52
+ * the status to answer the caller with (401 or 403).
53
+ */
54
+ readonly status: number | undefined;
55
+ /** For insufficient_scope: the scopes the route needs, for the WWW-Authenticate challenge. */
56
+ readonly scopes: readonly string[] | undefined;
57
+ constructor(code: IdpErrorCode, message: string, options?: {
58
+ status?: number;
59
+ cause?: unknown;
60
+ scopes?: readonly string[];
61
+ });
62
+ }
63
+ export declare function isIdpError(error: unknown): error is IdpError;
@@ -0,0 +1,41 @@
1
+ const SIGN_IN_ERROR_CODES = [
2
+ "access_denied",
3
+ "expired",
4
+ "state_mismatch",
5
+ "token_failed",
6
+ "invalid_id_token",
7
+ "refresh_failed"
8
+ ];
9
+ function isSignInErrorCode(value) {
10
+ return typeof value === "string" && SIGN_IN_ERROR_CODES.includes(value);
11
+ }
12
+ function isTokenError(error) {
13
+ return isIdpError(error) && (error.code === "missing_token" || error.code === "invalid_token" || error.code === "insufficient_scope");
14
+ }
15
+ class IdpError extends Error {
16
+ name = "IdpError";
17
+ code;
18
+ /**
19
+ * The HTTP status the auth server answered with, when there was one; for a token error,
20
+ * the status to answer the caller with (401 or 403).
21
+ */
22
+ status;
23
+ /** For insufficient_scope: the scopes the route needs, for the WWW-Authenticate challenge. */
24
+ scopes;
25
+ constructor(code, message, options) {
26
+ super(message, options?.cause === void 0 ? void 0 : { cause: options.cause });
27
+ this.code = code;
28
+ this.status = options?.status;
29
+ this.scopes = options?.scopes;
30
+ }
31
+ }
32
+ function isIdpError(error) {
33
+ return error instanceof Error && error.name === "IdpError" && typeof error.code === "string";
34
+ }
35
+ export {
36
+ IdpError,
37
+ SIGN_IN_ERROR_CODES,
38
+ isIdpError,
39
+ isSignInErrorCode,
40
+ isTokenError
41
+ };
@@ -0,0 +1,71 @@
1
+ import type { IncomingMessage, ServerResponse } from "node:http";
2
+ import { type AccessToken, type TokenRequirements } from "./access-token.js";
3
+ import type { IdpOptions } from "./config.js";
4
+ import { type Idp } from "./idp.js";
5
+ import { type SessionOptions } from "./node-adapter.js";
6
+ import type { Session } from "./session.js";
7
+ /**
8
+ * A request nextidp's middleware saw: `req.idp.session` after `auth.express()` (null when
9
+ * signed out), `req.idp.token` after `requireToken`.
10
+ */
11
+ export type IdpRequest = IncomingMessage & {
12
+ idp?: {
13
+ session?: Session | null;
14
+ token?: AccessToken;
15
+ };
16
+ };
17
+ export type Middleware = (req: IncomingMessage, res: ServerResponse, next: (error?: unknown) => void) => void;
18
+ export type ExpressIdp = Idp & {
19
+ /**
20
+ * The sign-in middleware, once, before your routes: `app.use(auth.express())`. It answers
21
+ * nextidp's routes under `basePath`, and for every other request reads the session
22
+ * (refreshing the access token when it's about to expire) for `sessionOf(req)`.
23
+ */
24
+ express(options?: SessionOptions): Middleware;
25
+ /**
26
+ * Lets a request through only with a session. Signed out, a GET or HEAD goes to sign in and
27
+ * comes back after; anything else gets 401.
28
+ *
29
+ * ```ts
30
+ * app.get("/dashboard", auth.requireSession(), (req, res) => {
31
+ * res.render("dashboard", { user: auth.sessionOf(req)?.user });
32
+ * });
33
+ * ```
34
+ */
35
+ requireSession(): Middleware;
36
+ /** The session `express()` read for this request, or null when signed out. */
37
+ sessionOf(req: IncomingMessage): Session | null;
38
+ /**
39
+ * Middleware that lets a request through only with a valid access token that has every
40
+ * scope in `requirements`. A refused one gets 401 or 403 with WWW-Authenticate (RFC 6750);
41
+ * a token that can't be checked now (the auth server is down) goes to `next(error)`.
42
+ *
43
+ * ```ts
44
+ * app.get("/orders", auth.requireToken({ scopes: ["orders:read"] }), (req, res) => {
45
+ * res.json(ordersOf(auth.tokenOf(req)?.subject));
46
+ * });
47
+ * ```
48
+ */
49
+ requireToken(requirements?: TokenRequirements): Middleware;
50
+ /** The access token `requireToken` checked for this request. */
51
+ tokenOf(req: IncomingMessage): AccessToken | undefined;
52
+ };
53
+ /**
54
+ * The client for an Express app or API. An app that signs people in needs the sign-in
55
+ * settings (`IDP_ISSUER`, `IDP_CLIENT_ID`, `IDP_APP_URL`, `IDP_SECRET`); an API only `issuer`
56
+ * and `audience`:
57
+ *
58
+ * ```ts
59
+ * import { idp } from "nextidp/express";
60
+ * export const auth = idp(); // the IDP_* environment variables, read on first use
61
+ * ```
62
+ */
63
+ export declare function idp(options?: IdpOptions | (() => IdpOptions)): ExpressIdp;
64
+ export type { AccessToken, TokenRequirements } from "./access-token.js";
65
+ export type { ApiConfig, IdpConfig, IdpOptions } from "./config.js";
66
+ export { IdpError, type IdpErrorCode, isIdpError, isSignInErrorCode, isTokenError, SIGN_IN_ERROR_CODES, type SignInErrorCode, type TokenErrorCode, } from "./errors.js";
67
+ export type { Idp, Refreshed } from "./idp.js";
68
+ export type { SessionOptions } from "./node-adapter.js";
69
+ export type { InterceptableClient, ServiceTokenOptions, UseServiceTokenOptions, } from "./service.js";
70
+ export type { IdpUser, Session } from "./session.js";
71
+ export { memoryStore, type SessionStore } from "./store.js";
@@ -0,0 +1,90 @@
1
+ import { tokenRefusal } from "./access-token.js";
2
+ import { isTokenError } from "./errors.js";
3
+ import { createIdp } from "./idp.js";
4
+ import { nodeAdapter } from "./node-adapter.js";
5
+ function authorizationOf(req) {
6
+ const value = req.headers.authorization;
7
+ return Array.isArray(value) ? void 0 : value;
8
+ }
9
+ function remember(req, found) {
10
+ const request = req;
11
+ request.idp = { ...request.idp, ...found };
12
+ }
13
+ function idp(options) {
14
+ const core = createIdp(options);
15
+ const node = nodeAdapter(core);
16
+ return {
17
+ ...core,
18
+ express(sessionOptions = {}) {
19
+ return (req, res, next) => {
20
+ const run = async () => {
21
+ if (await node.handle(req, res)) return true;
22
+ remember(req, { session: await node.session(req, res, sessionOptions) });
23
+ return false;
24
+ };
25
+ run().then((answered) => {
26
+ if (!answered) next();
27
+ }, next);
28
+ };
29
+ },
30
+ requireSession() {
31
+ return (req, res, next) => {
32
+ node.session(req, res).then((session) => {
33
+ if (!session) {
34
+ node.turnAway(req, res);
35
+ return;
36
+ }
37
+ remember(req, { session });
38
+ next();
39
+ }, next);
40
+ };
41
+ },
42
+ sessionOf(req) {
43
+ const session = req.idp?.session;
44
+ if (session === void 0) {
45
+ throw new Error(
46
+ "nextidp: no session was read for this request. Mount auth.express() first."
47
+ );
48
+ }
49
+ return session;
50
+ },
51
+ requireToken(requirements = {}) {
52
+ return (req, res, next) => {
53
+ core.verifyBearer(authorizationOf(req), requirements).then(
54
+ (token) => {
55
+ remember(req, { token });
56
+ next();
57
+ },
58
+ (error) => {
59
+ if (!isTokenError(error)) {
60
+ next(error);
61
+ return;
62
+ }
63
+ const refusal = tokenRefusal(error);
64
+ res.statusCode = refusal.status;
65
+ for (const [name, value] of Object.entries(refusal.headers)) res.setHeader(name, value);
66
+ res.end(JSON.stringify(refusal.body));
67
+ }
68
+ );
69
+ };
70
+ },
71
+ tokenOf: (req) => req.idp?.token
72
+ };
73
+ }
74
+ import {
75
+ IdpError,
76
+ isIdpError,
77
+ isSignInErrorCode,
78
+ isTokenError as isTokenError2,
79
+ SIGN_IN_ERROR_CODES
80
+ } from "./errors.js";
81
+ import { memoryStore } from "./store.js";
82
+ export {
83
+ IdpError,
84
+ SIGN_IN_ERROR_CODES,
85
+ idp,
86
+ isIdpError,
87
+ isSignInErrorCode,
88
+ isTokenError2 as isTokenError,
89
+ memoryStore
90
+ };
@@ -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;
@@ -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;