@hyper.authio/express 0.0.0-stage → 0.7.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/README.md +89 -2
- package/dist/index.cjs +183 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +139 -0
- package/dist/index.d.ts +139 -0
- package/dist/index.js +163 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -4
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hyper.authio/express
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Protect an Express 4 or 5 API with Authio: bearer validation, guards for every form of permission and for the caller
|
|
4
|
+
kind, step-up challenges, protected resource metadata (RFC 9728), a back-channel logout endpoint, and a signed webhook
|
|
5
|
+
route. Built on [`@hyper.authio/server`](../server/README.md).
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @hyper.authio/express
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import express from "express";
|
|
13
|
+
import {
|
|
14
|
+
anyOf,
|
|
15
|
+
authioBearer,
|
|
16
|
+
authioWebhooks,
|
|
17
|
+
requireAuthenticationLevel,
|
|
18
|
+
requireAuthio,
|
|
19
|
+
requireGroup,
|
|
20
|
+
requireOrganization,
|
|
21
|
+
requirePermission,
|
|
22
|
+
requireRole,
|
|
23
|
+
requireScope,
|
|
24
|
+
} from "@hyper.authio/express";
|
|
25
|
+
|
|
26
|
+
const app = express();
|
|
27
|
+
|
|
28
|
+
// Before any JSON parser: the signature is checked over the raw body.
|
|
29
|
+
app.post(
|
|
30
|
+
"/webhooks/authio",
|
|
31
|
+
authioWebhooks({ secret: process.env.AUTHIO_WEBHOOK_SECRET }).on("user_deleted", async (event) => {
|
|
32
|
+
await forget(event.data.id);
|
|
33
|
+
}),
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
app.use(express.json());
|
|
37
|
+
app.use("/api", authioBearer({ issuer: "https://auth.example.com/realms/acme", audiences: "https://orders.example.com" }));
|
|
38
|
+
|
|
39
|
+
app.get("/api/me", (req, res) => res.json(req.authio!.user));
|
|
40
|
+
app.get("/api/orders", requireScope("orders:read"), requirePermission("orders", "read"), listOrders);
|
|
41
|
+
app.delete("/api/orders/:id", requireRole("orders-admin", { sources: ["user"] }), deleteOrder);
|
|
42
|
+
app.get("/api/finance", requireGroup("finance"), financeReport);
|
|
43
|
+
app.post("/api/payouts", requireAuthio(requirePermission("payouts", "create"), anyOf(requireAuthenticationLevel(2), requireOrganization("acme"))), payout);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- `authioBearer(verifierOrOptions, { optional?, problemDetails?, passErrors? })` validates the token and puts
|
|
47
|
+
`{ user, token }` on `req.authio`; it answers `401` with `WWW-Authenticate`. With `optional`, a request without a
|
|
48
|
+
token goes through unauthenticated.
|
|
49
|
+
- `requireScope`, `requireRole`, `requirePermission`, `requireGroup`, `requireClaim`, `requireOrganization`,
|
|
50
|
+
`requireAuthenticationLevel`, `requireAuthenticationMethod`, `requireRecentAuthentication`, `requireCallerKind` and
|
|
51
|
+
`requireClient` answer `403`, with `insufficient_scope` or `insufficient_user_authentication` (with `acr_values` and
|
|
52
|
+
`max_age`, RFC 9470) in `WWW-Authenticate` when that is the reason.
|
|
53
|
+
- With `requiredScopes` on the verifier, `authioBearer` itself answers `403 insufficient_scope` when a token lacks one.
|
|
54
|
+
- Every guard is also a rule: `requireAuthio(...)`, `allOf(...)` and `anyOf(...)` combine guards of this package and
|
|
55
|
+
rules of `@hyper.authio/server` alike, and are guards themselves.
|
|
56
|
+
- `problemDetails: true` adds an RFC 9457 body; `passErrors: true` hands `AuthioBearerError` / `AuthioForbiddenError`
|
|
57
|
+
to `next(error)` for your own error handler. `sendAuthioError(res, error)` answers one by hand.
|
|
58
|
+
- `authioWebhooks(options)` reads the raw body itself (or takes `express.raw()`'s buffer), and answers `200` handled
|
|
59
|
+
or duplicate, `400`, `401`, `413` or `500`.
|
|
60
|
+
|
|
61
|
+
## Protected resource metadata (RFC 9728)
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
const verifier = createAuthioVerifier({ issuer, resource: "https://orders.example.com/api", resourceMetadata: true });
|
|
65
|
+
|
|
66
|
+
app.use(authioProtectedResourceMetadata({ verifier, resourceName: "Orders" })); // GET /.well-known/oauth-protected-resource/api
|
|
67
|
+
app.use("/api", authioBearer(verifier)); // 401: Bearer resource_metadata="https://orders.example.com/.well-known/oauth-protected-resource/api"
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Back-channel logout
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
const client = createAuthioClient({ issuer, clientId: "storefront", clientSecret });
|
|
74
|
+
|
|
75
|
+
app.post(
|
|
76
|
+
"/auth/backchannel-logout",
|
|
77
|
+
authioBackchannelLogout({
|
|
78
|
+
client,
|
|
79
|
+
onLogout: async ({ subject, sessionId }) => sessions.destroyWhere({ subject, sessionId }), // your session store
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Register the URL as the client's back-channel logout URI in Authio. The handler reads `logout_token` (with or without
|
|
85
|
+
`express.urlencoded()`), validates it with `client.validateLogoutToken`, answers `200`, `400` (`invalid_request`) for
|
|
86
|
+
a token that does not validate, and always sends `Cache-Control: no-store`.
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var client = require('@hyper.authio/client');
|
|
4
|
+
var server = require('@hyper.authio/server');
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
var responseSettings = /* @__PURE__ */ Symbol.for("authio.express.response");
|
|
8
|
+
function authioBearer(verifier, options = {}) {
|
|
9
|
+
const resolved = isVerifier(verifier) ? verifier : server.createAuthioVerifier(verifier);
|
|
10
|
+
return (request, response, next) => {
|
|
11
|
+
request[responseSettings] = { problemDetails: options.problemDetails, passErrors: options.passErrors };
|
|
12
|
+
const header = request.headers.authorization;
|
|
13
|
+
if (options.optional && !header) {
|
|
14
|
+
next();
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
resolved.verifyAuthorizationHeader(header).then((user) => {
|
|
18
|
+
request.authio = { user, token: user.token };
|
|
19
|
+
const failure = resolved.requiredScopes.length > 0 ? server.requireScope(resolved.requiredScopes, "all").check(user) : void 0;
|
|
20
|
+
if (failure) return refuse(request, response, next, new server.AuthioForbiddenError(failure), options);
|
|
21
|
+
next();
|
|
22
|
+
}).catch((error) => refuse(request, response, next, error, options));
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function requireAuthio(...rules) {
|
|
26
|
+
if (rules.length === 0) throw new TypeError("requireAuthio needs at least one rule.");
|
|
27
|
+
const rule = rules.length === 1 ? rules[0] : server.allOf(...rules);
|
|
28
|
+
const guard = (request, response, next) => {
|
|
29
|
+
const user = request.authio?.user;
|
|
30
|
+
if (!user) return refuse(request, response, next, new server.AuthioBearerError(void 0, { error: void 0 }));
|
|
31
|
+
const failure = server.authorize(user, ...rules);
|
|
32
|
+
if (failure) return refuse(request, response, next, new server.AuthioForbiddenError(failure));
|
|
33
|
+
next();
|
|
34
|
+
};
|
|
35
|
+
return Object.assign(guard, { description: rule.description, check: (user) => server.authorize(user, ...rules) });
|
|
36
|
+
}
|
|
37
|
+
var requireScope = (scopes, match) => requireAuthio(server.requireScope(scopes, match));
|
|
38
|
+
var requireRole = (roles, options) => requireAuthio(server.requireRole(roles, options));
|
|
39
|
+
var requirePermission = (resource, method, options) => requireAuthio(server.requirePermission(resource, method, options));
|
|
40
|
+
var requireGroup = (groups, match) => requireAuthio(server.requireGroup(groups, match));
|
|
41
|
+
var requireClaim = (type, ...values) => requireAuthio(server.requireClaim(type, ...values));
|
|
42
|
+
var requireOrganization = (organization) => requireAuthio(server.requireOrganization(organization));
|
|
43
|
+
var requireAuthenticationLevel = (minimumLevel, options) => requireAuthio(server.requireAuthenticationLevel(minimumLevel, options));
|
|
44
|
+
var requireAuthenticationMethod = (methods, match, options) => requireAuthio(server.requireAuthenticationMethod(methods, match, options));
|
|
45
|
+
var requireRecentAuthentication = (maxAge, options) => requireAuthio(server.requireRecentAuthentication(maxAge, options));
|
|
46
|
+
var requireCallerKind = (kinds) => requireAuthio(server.requireCallerKind(kinds));
|
|
47
|
+
var requireClient = (clientIds) => requireAuthio(server.requireClient(clientIds));
|
|
48
|
+
var allOf = (...rules) => requireAuthio(server.allOf(...rules));
|
|
49
|
+
var anyOf = (...rules) => requireAuthio(server.anyOf(...rules));
|
|
50
|
+
function authioWebhooks(options) {
|
|
51
|
+
const webhooks = server.createAuthioWebhooks(options);
|
|
52
|
+
const route = ((request, response, next) => {
|
|
53
|
+
readRawBody(request, webhooks.maxBodySize).then(async (body) => {
|
|
54
|
+
if (body === void 0) {
|
|
55
|
+
response.status(413).end();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const outcome = await webhooks.handle({ body, headers: request.headers });
|
|
59
|
+
response.status(outcome.status).end();
|
|
60
|
+
}).catch(next);
|
|
61
|
+
});
|
|
62
|
+
route.on = ((type, handler) => {
|
|
63
|
+
webhooks.on(type, handler);
|
|
64
|
+
return route;
|
|
65
|
+
});
|
|
66
|
+
return route;
|
|
67
|
+
}
|
|
68
|
+
function authioProtectedResourceMetadata(options) {
|
|
69
|
+
const settings = "verifier" in options ? {
|
|
70
|
+
...options,
|
|
71
|
+
resource: requireResource(options.verifier),
|
|
72
|
+
issuer: options.verifier.connection.issuer,
|
|
73
|
+
scopesSupported: options.scopesSupported ?? (options.verifier.requiredScopes.length > 0 ? options.verifier.requiredScopes : void 0)
|
|
74
|
+
} : options;
|
|
75
|
+
const document = JSON.stringify(server.createProtectedResourceMetadata(settings));
|
|
76
|
+
const path = server.protectedResourceMetadataPath(settings.resource);
|
|
77
|
+
return (request, response, next) => {
|
|
78
|
+
if (request.method !== "GET" && request.method !== "HEAD" || request.path.replace(/\/+$/, "") !== path) return next();
|
|
79
|
+
response.set("Cache-Control", "public, max-age=3600").type("application/json").send(document);
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function authioBackchannelLogout(options) {
|
|
83
|
+
return (request, response, next) => {
|
|
84
|
+
response.set("Cache-Control", "no-store");
|
|
85
|
+
readLogoutToken(request).then(async (logoutToken) => {
|
|
86
|
+
if (!logoutToken) {
|
|
87
|
+
response.status(400).json({ error: "invalid_request", error_description: "The request carries no logout_token." });
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
let logout;
|
|
91
|
+
try {
|
|
92
|
+
logout = await options.client.validateLogoutToken(logoutToken);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
options.onError?.(error);
|
|
95
|
+
if (!(error instanceof client.AuthioTokenValidationError)) throw error;
|
|
96
|
+
response.status(400).json({ error: "invalid_request", error_description: error.message });
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
await options.onLogout(logout, request);
|
|
100
|
+
response.status(200).end();
|
|
101
|
+
}).catch(next);
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function sendAuthioError(response, error, options = {}) {
|
|
105
|
+
const challenge = error.wwwAuthenticate;
|
|
106
|
+
if (challenge) response.append("WWW-Authenticate", challenge);
|
|
107
|
+
response.status(error.status);
|
|
108
|
+
if (options.problemDetails) response.type("application/problem+json").send(JSON.stringify(server.toProblemDetails(error)));
|
|
109
|
+
else response.end();
|
|
110
|
+
}
|
|
111
|
+
function refuse(request, response, next, error, options) {
|
|
112
|
+
const settings = options ?? request[responseSettings] ?? {};
|
|
113
|
+
if (!(error instanceof server.AuthioBearerError) && !(error instanceof server.AuthioForbiddenError)) return next(error);
|
|
114
|
+
if (settings.passErrors) return next(error);
|
|
115
|
+
sendAuthioError(response, error, settings);
|
|
116
|
+
}
|
|
117
|
+
function readRawBody(request, maxBodySize) {
|
|
118
|
+
const parsed = request.body;
|
|
119
|
+
if (Buffer.isBuffer(parsed)) return Promise.resolve(parsed.byteLength > maxBodySize ? void 0 : new Uint8Array(parsed));
|
|
120
|
+
if (typeof parsed === "string") return Promise.resolve(new TextEncoder().encode(parsed));
|
|
121
|
+
if (request.readableEnded || parsed !== void 0 && parsed !== null && typeof parsed === "object" && Object.keys(parsed).length > 0)
|
|
122
|
+
return Promise.reject(
|
|
123
|
+
new Error("The Authio webhook route needs the raw body: mount it before express.json(), or use express.raw({ type: '*/*' }) on it.")
|
|
124
|
+
);
|
|
125
|
+
const declared = Number(request.headers["content-length"]);
|
|
126
|
+
if (Number.isFinite(declared) && declared > maxBodySize) return Promise.resolve(void 0);
|
|
127
|
+
return new Promise((resolve, reject) => {
|
|
128
|
+
const chunks = [];
|
|
129
|
+
let size = 0;
|
|
130
|
+
let done = false;
|
|
131
|
+
request.on("data", (chunk) => {
|
|
132
|
+
if (done) return;
|
|
133
|
+
size += chunk.byteLength;
|
|
134
|
+
if (size > maxBodySize) {
|
|
135
|
+
done = true;
|
|
136
|
+
request.pause();
|
|
137
|
+
resolve(void 0);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
chunks.push(chunk);
|
|
141
|
+
});
|
|
142
|
+
request.on("end", () => {
|
|
143
|
+
if (!done) resolve(new Uint8Array(Buffer.concat(chunks)));
|
|
144
|
+
});
|
|
145
|
+
request.on("error", reject);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
async function readLogoutToken(request) {
|
|
149
|
+
const parsed = request.body;
|
|
150
|
+
if (parsed && typeof parsed === "object" && !Buffer.isBuffer(parsed) && typeof parsed.logout_token === "string")
|
|
151
|
+
return parsed.logout_token;
|
|
152
|
+
const raw = Buffer.isBuffer(parsed) ? new Uint8Array(parsed) : typeof parsed === "string" ? new TextEncoder().encode(parsed) : request.readableEnded ? void 0 : await readRawBody(request, 64 * 1024);
|
|
153
|
+
return raw ? new URLSearchParams(new TextDecoder().decode(raw)).get("logout_token") ?? void 0 : void 0;
|
|
154
|
+
}
|
|
155
|
+
function requireResource(verifier) {
|
|
156
|
+
if (!verifier.resource) throw new TypeError("authioProtectedResourceMetadata needs a verifier configured with resource.");
|
|
157
|
+
return verifier.resource;
|
|
158
|
+
}
|
|
159
|
+
function isVerifier(value) {
|
|
160
|
+
return typeof value.verify === "function";
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
exports.allOf = allOf;
|
|
164
|
+
exports.anyOf = anyOf;
|
|
165
|
+
exports.authioBackchannelLogout = authioBackchannelLogout;
|
|
166
|
+
exports.authioBearer = authioBearer;
|
|
167
|
+
exports.authioProtectedResourceMetadata = authioProtectedResourceMetadata;
|
|
168
|
+
exports.authioWebhooks = authioWebhooks;
|
|
169
|
+
exports.requireAuthenticationLevel = requireAuthenticationLevel;
|
|
170
|
+
exports.requireAuthenticationMethod = requireAuthenticationMethod;
|
|
171
|
+
exports.requireAuthio = requireAuthio;
|
|
172
|
+
exports.requireCallerKind = requireCallerKind;
|
|
173
|
+
exports.requireClaim = requireClaim;
|
|
174
|
+
exports.requireClient = requireClient;
|
|
175
|
+
exports.requireGroup = requireGroup;
|
|
176
|
+
exports.requireOrganization = requireOrganization;
|
|
177
|
+
exports.requirePermission = requirePermission;
|
|
178
|
+
exports.requireRecentAuthentication = requireRecentAuthentication;
|
|
179
|
+
exports.requireRole = requireRole;
|
|
180
|
+
exports.requireScope = requireScope;
|
|
181
|
+
exports.sendAuthioError = sendAuthioError;
|
|
182
|
+
//# sourceMappingURL=index.cjs.map
|
|
183
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["createAuthioVerifier","scopeRule","AuthioForbiddenError","allOfRule","AuthioBearerError","authorize","roleRule","permissionRule","groupRule","claimRule","organizationRule","authenticationLevelRule","authenticationMethodRule","recentAuthenticationRule","callerKindRule","clientRule","anyOfRule","createAuthioWebhooks","createProtectedResourceMetadata","protectedResourceMetadataP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sourcesContent":["import type { NextFunction, Request, RequestHandler, Response } from \"express\";\nimport {\n AuthioTokenValidationError,\n type AuthioCallerKind,\n type AuthioClient,\n type AuthioLogoutToken,\n type AuthioResourceAccessSource,\n type AuthioTypedWebhookEvent,\n type AuthioWebhookDataByType,\n} from \"@hyper.authio/client\";\nimport {\n allOf as allOfRule,\n anyOf as anyOfRule,\n authorize,\n AuthioBearerError,\n AuthioForbiddenError,\n createAuthioVerifier,\n createAuthioWebhooks,\n createProtectedResourceMetadata,\n protectedResourceMetadataPath,\n requireAuthenticationLevel as authenticationLevelRule,\n requireAuthenticationMethod as authenticationMethodRule,\n requireCallerKind as callerKindRule,\n requireClaim as claimRule,\n requireClient as clientRule,\n requireGroup as groupRule,\n requireOrganization as organizationRule,\n requirePermission as permissionRule,\n requireRecentAuthentication as recentAuthenticationRule,\n requireRole as roleRule,\n requireScope as scopeRule,\n toProblemDetails,\n type AuthioProtectedResourceMetadataOptions,\n type AuthioMatch,\n type AuthioRule,\n type AuthioRuleFailure,\n type AuthioUser,\n type AuthioVerifier,\n type AuthioVerifierOptions,\n type AuthioWebhookHandler,\n type AuthioWebhookOptions,\n} from \"@hyper.authio/server\";\n\n/**\n * What `authioBearer` puts on the request.\n */\nexport interface AuthioRequestContext {\n /** The caller. */\n readonly user: AuthioUser;\n /** The access token the caller was read from. */\n readonly token: string;\n}\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace Express {\n interface Request {\n /** The Authio caller, once `authioBearer` validated the token. */\n authio?: AuthioRequestContext;\n }\n }\n}\n\n/**\n * How refusals are answered.\n */\nexport interface AuthioResponseOptions {\n /** Answers `401` and `403` with problem details (RFC 9457) as well as the `WWW-Authenticate` header. */\n problemDetails?: boolean;\n /** Passes refusals to `next(error)` instead of answering, for an application's own error handler. */\n passErrors?: boolean;\n}\n\n/**\n * Options of `authioBearer`.\n */\nexport interface AuthioBearerOptions extends AuthioResponseOptions {\n /** Lets a request without a token through, unauthenticated; a token that is present must still be valid. */\n optional?: boolean;\n}\n\nconst responseSettings = Symbol.for(\"authio.express.response\");\n\ntype RequestWithSettings = Request & { [responseSettings]?: AuthioResponseOptions };\n\n/**\n * Validates the `Authorization: Bearer` token and puts the caller on `req.authio`. Answers `401` with the\n * `WWW-Authenticate` challenge when the token is missing or invalid.\n *\n * @param verifier A verifier, or its options.\n */\nexport function authioBearer(verifier: AuthioVerifier | AuthioVerifierOptions, options: AuthioBearerOptions = {}): RequestHandler {\n const resolved = isVerifier(verifier) ? verifier : createAuthioVerifier(verifier);\n\n return (request, response, next) => {\n (request as RequestWithSettings)[responseSettings] = { problemDetails: options.problemDetails, passErrors: options.passErrors };\n\n const header = request.headers.authorization;\n\n if (options.optional && !header) {\n next();\n return;\n }\n\n resolved\n .verifyAuthorizationHeader(header)\n .then((user) => {\n request.authio = { user, token: user.token! };\n\n const failure = resolved.requiredScopes.length > 0 ? scopeRule(resolved.requiredScopes, \"all\").check(user) : undefined;\n\n if (failure) return refuse(request, response, next, new AuthioForbiddenError(failure as AuthioRuleFailure), options);\n\n next();\n })\n .catch((error: unknown) => refuse(request, response, next, error, options));\n };\n}\n\n/**\n * An Express guard that is also an Authio rule: use it as middleware, or pass it to `requireAuthio`, `allOf` and\n * `anyOf` like any rule of `@hyper.authio/server`.\n */\nexport type AuthioGuard = RequestHandler & AuthioRule;\n\n/**\n * Requires every rule (guards of this package or rules of `@hyper.authio/server`); answers `401` without a caller and\n * `403` with the joined refusal otherwise.\n */\nexport function requireAuthio(...rules: AuthioRule[]): AuthioGuard {\n if (rules.length === 0) throw new TypeError(\"requireAuthio needs at least one rule.\");\n\n const rule = rules.length === 1 ? rules[0]! : allOfRule(...rules);\n const guard: RequestHandler = (request, response, next) => {\n const user = request.authio?.user;\n\n if (!user) return refuse(request, response, next, new AuthioBearerError(undefined, { error: undefined }));\n\n const failure = authorize(user, ...rules);\n\n if (failure) return refuse(request, response, next, new AuthioForbiddenError(failure));\n\n next();\n };\n\n return Object.assign(guard, { description: rule.description, check: (user: AuthioUser) => authorize(user, ...rules) });\n}\n\n/** Requires granted scopes. */\nexport const requireScope = (scopes: string | readonly string[], match?: AuthioMatch): AuthioGuard => requireAuthio(scopeRule(scopes, match));\n/** Requires roles, from any source or only from the given `*_resource_access` claims. */\nexport const requireRole = (\n roles: string | readonly string[],\n options?: { match?: AuthioMatch; sources?: readonly AuthioResourceAccessSource[] },\n): AuthioGuard => requireAuthio(roleRule(roles, options));\n/** Requires a permission (`method` on `resource`). */\nexport const requirePermission = (resource: string, method: string, options?: { sources?: readonly AuthioResourceAccessSource[] }): AuthioGuard =>\n requireAuthio(permissionRule(resource, method, options));\n/** Requires groups. */\nexport const requireGroup = (groups: string | readonly string[], match?: AuthioMatch): AuthioGuard => requireAuthio(groupRule(groups, match));\n/** Requires a claim, or one of its values. */\nexport const requireClaim = (type: string, ...values: string[]): AuthioGuard => requireAuthio(claimRule(type, ...values));\n/** Requires an organization context, or a given organization. */\nexport const requireOrganization = (organization?: string): AuthioGuard => requireAuthio(organizationRule(organization));\n/** Requires a minimum authentication level (`acr`), and optionally a sign-in at most `maxAge` seconds old. */\nexport const requireAuthenticationLevel = (minimumLevel: number, options?: Parameters<typeof authenticationLevelRule>[1]): AuthioGuard =>\n requireAuthio(authenticationLevelRule(minimumLevel, options));\n/** Requires authentication methods (`amr`). */\nexport const requireAuthenticationMethod = (methods: string | readonly string[], match?: AuthioMatch, options?: { acrValues?: string }): AuthioGuard =>\n requireAuthio(authenticationMethodRule(methods, match, options));\n/** Requires a sign-in at most `maxAge` seconds old (`auth_time`). */\nexport const requireRecentAuthentication = (maxAge: number, options?: Parameters<typeof recentAuthenticationRule>[1]): AuthioGuard =>\n requireAuthio(recentAuthenticationRule(maxAge, options));\n/** Requires the caller to be of one of the kinds: `user`, `client`, `consumer`, `account_credential`. */\nexport const requireCallerKind = (kinds: AuthioCallerKind | readonly AuthioCallerKind[]): AuthioGuard => requireAuthio(callerKindRule(kinds));\n/** Requires the token to have been issued to one of the clients. */\nexport const requireClient = (clientIds: string | readonly string[]): AuthioGuard => requireAuthio(clientRule(clientIds));\n/** Requires every rule. */\nexport const allOf = (...rules: AuthioRule[]): AuthioGuard => requireAuthio(allOfRule(...rules));\n/** Requires at least one rule. */\nexport const anyOf = (...rules: AuthioRule[]): AuthioGuard => requireAuthio(anyOfRule(...rules));\n\n/**\n * A webhook route: verifies the signature over the raw body, deduplicates and dispatches. Mount it before any JSON\n * body parser, or behind `express.raw()`.\n */\nexport interface AuthioWebhookRoute extends RequestHandler {\n on<T extends keyof AuthioWebhookDataByType>(type: T, handler: AuthioWebhookHandler<AuthioTypedWebhookEvent<T>>): AuthioWebhookRoute;\n on(type: string, handler: AuthioWebhookHandler): AuthioWebhookRoute;\n}\n\n/**\n * Creates a webhook route: `app.post(\"/webhooks/authio\", authioWebhooks({ secret }).on(\"user_created\", …))`.\n * Answers `200` handled or duplicate, `400` unreadable, `401` bad signature, `413` too large, `500` handler failure.\n */\nexport function authioWebhooks(options: AuthioWebhookOptions): AuthioWebhookRoute {\n const webhooks = createAuthioWebhooks(options);\n\n const route = ((request: Request, response: Response, next: NextFunction) => {\n readRawBody(request, webhooks.maxBodySize)\n .then(async (body) => {\n if (body === undefined) {\n response.status(413).end();\n return;\n }\n\n const outcome = await webhooks.handle({ body, headers: request.headers });\n\n response.status(outcome.status).end();\n })\n .catch(next);\n }) as AuthioWebhookRoute;\n\n route.on = ((type: string, handler: AuthioWebhookHandler) => {\n webhooks.on(type, handler);\n return route;\n }) as AuthioWebhookRoute[\"on\"];\n\n return route;\n}\n\n/**\n * Options of `authioProtectedResourceMetadata`: the metadata itself, or a verifier configured with `resource`.\n */\nexport type AuthioProtectedResourceMetadataRouteOptions =\n | AuthioProtectedResourceMetadataOptions\n | (Omit<AuthioProtectedResourceMetadataOptions, \"resource\" | \"issuer\" | \"authority\" | \"realm\"> & { verifier: AuthioVerifier });\n\n/**\n * Serves the API's protected resource metadata (RFC 9728) at `/.well-known/oauth-protected-resource{path of the\n * resource}`: `app.use(authioProtectedResourceMetadata({ verifier }))`. Every other request goes on to `next`.\n */\nexport function authioProtectedResourceMetadata(options: AuthioProtectedResourceMetadataRouteOptions): RequestHandler {\n const settings: AuthioProtectedResourceMetadataOptions =\n \"verifier\" in options\n ? {\n ...options,\n resource: requireResource(options.verifier),\n issuer: options.verifier.connection.issuer,\n scopesSupported: options.scopesSupported ?? (options.verifier.requiredScopes.length > 0 ? options.verifier.requiredScopes : undefined),\n }\n : options;\n const document = JSON.stringify(createProtectedResourceMetadata(settings));\n const path = protectedResourceMetadataPath(settings.resource);\n\n return (request, response, next) => {\n if ((request.method !== \"GET\" && request.method !== \"HEAD\") || request.path.replace(/\\/+$/, \"\") !== path) return next();\n\n response.set(\"Cache-Control\", \"public, max-age=3600\").type(\"application/json\").send(document);\n };\n}\n\n/**\n * Options of `authioBackchannelLogout`.\n */\nexport interface AuthioBackchannelLogoutOptions {\n /** The client the logout tokens are addressed to (its id is their audience). */\n client: AuthioClient;\n /**\n * Ends the sessions the token names: every session of `subject`, or the one `sessionId` names. Session storage is\n * the application's.\n */\n onLogout: (logout: AuthioLogoutToken, request: Request) => void | Promise<void>;\n /** Called when a token is refused. */\n onError?: (error: unknown) => void;\n}\n\n/**\n * The back-channel logout endpoint (OpenID Connect Back-Channel Logout 1.0): reads `logout_token` from the\n * form-encoded body, validates it with the client and calls `onLogout`. Answers `200` on success, `400`\n * (`invalid_request`) for a token that does not validate, and `500` when `onLogout` throws; always `Cache-Control:\n * no-store`. Works with or without `express.urlencoded()` in front of it.\n */\nexport function authioBackchannelLogout(options: AuthioBackchannelLogoutOptions): RequestHandler {\n return (request, response, next) => {\n response.set(\"Cache-Control\", \"no-store\");\n\n readLogoutToken(request)\n .then(async (logoutToken) => {\n if (!logoutToken) {\n response.status(400).json({ error: \"invalid_request\", error_description: \"The request carries no logout_token.\" });\n return;\n }\n\n let logout: AuthioLogoutToken;\n\n try {\n logout = await options.client.validateLogoutToken(logoutToken);\n } catch (error) {\n options.onError?.(error);\n if (!(error instanceof AuthioTokenValidationError)) throw error;\n response.status(400).json({ error: \"invalid_request\", error_description: error.message });\n return;\n }\n\n await options.onLogout(logout, request);\n response.status(200).end();\n })\n .catch(next);\n };\n}\n\n/**\n * Answers a refusal: `WWW-Authenticate`, the status, and problem details when asked.\n */\nexport function sendAuthioError(response: Response, error: AuthioBearerError | AuthioForbiddenError, options: AuthioResponseOptions = {}): void {\n const challenge = error.wwwAuthenticate;\n\n if (challenge) response.append(\"WWW-Authenticate\", challenge);\n\n response.status(error.status);\n\n if (options.problemDetails) response.type(\"application/problem+json\").send(JSON.stringify(toProblemDetails(error)));\n else response.end();\n}\n\nfunction refuse(request: Request, response: Response, next: NextFunction, error: unknown, options?: AuthioResponseOptions): void {\n const settings = options ?? (request as RequestWithSettings)[responseSettings] ?? {};\n\n if (!(error instanceof AuthioBearerError) && !(error instanceof AuthioForbiddenError)) return next(error);\n if (settings.passErrors) return next(error);\n\n sendAuthioError(response, error, settings);\n}\n\nfunction readRawBody(request: Request, maxBodySize: number): Promise<Uint8Array | undefined> {\n const parsed = (request as Request & { body?: unknown }).body;\n\n if (Buffer.isBuffer(parsed)) return Promise.resolve(parsed.byteLength > maxBodySize ? undefined : new Uint8Array(parsed));\n if (typeof parsed === \"string\") return Promise.resolve(new TextEncoder().encode(parsed));\n\n if (request.readableEnded || (parsed !== undefined && parsed !== null && typeof parsed === \"object\" && Object.keys(parsed).length > 0))\n return Promise.reject(\n new Error(\"The Authio webhook route needs the raw body: mount it before express.json(), or use express.raw({ type: '*/*' }) on it.\"),\n );\n\n const declared = Number(request.headers[\"content-length\"]);\n\n if (Number.isFinite(declared) && declared > maxBodySize) return Promise.resolve(undefined);\n\n return new Promise((resolve, reject) => {\n const chunks: Buffer[] = [];\n let size = 0;\n let done = false;\n\n request.on(\"data\", (chunk: Buffer) => {\n if (done) return;\n\n size += chunk.byteLength;\n\n if (size > maxBodySize) {\n done = true;\n request.pause();\n resolve(undefined);\n return;\n }\n\n chunks.push(chunk);\n });\n request.on(\"end\", () => {\n if (!done) resolve(new Uint8Array(Buffer.concat(chunks)));\n });\n request.on(\"error\", reject);\n });\n}\n\nasync function readLogoutToken(request: Request): Promise<string | undefined> {\n const parsed = (request as Request & { body?: unknown }).body;\n\n if (parsed && typeof parsed === \"object\" && !Buffer.isBuffer(parsed) && typeof (parsed as Record<string, unknown>).logout_token === \"string\")\n return (parsed as Record<string, string>).logout_token;\n\n const raw = Buffer.isBuffer(parsed)\n ? new Uint8Array(parsed)\n : typeof parsed === \"string\"\n ? new TextEncoder().encode(parsed)\n : request.readableEnded\n ? undefined\n : await readRawBody(request, 64 * 1024);\n\n return raw ? (new URLSearchParams(new TextDecoder().decode(raw)).get(\"logout_token\") ?? undefined) : undefined;\n}\n\nfunction requireResource(verifier: AuthioVerifier): string {\n if (!verifier.resource) throw new TypeError(\"authioProtectedResourceMetadata needs a verifier configured with resource.\");\n\n return verifier.resource;\n}\n\nfunction isVerifier(value: AuthioVerifier | AuthioVerifierOptions): value is AuthioVerifier {\n return typeof (value as AuthioVerifier).verify === \"function\";\n}\n\nexport type { AuthioMatch, AuthioRule, AuthioUser, AuthioVerifier, AuthioVerifierOptions } from \"@hyper.authio/server\";\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Request, RequestHandler, Response } from 'express';
|
|
2
|
+
import { AuthioClient, AuthioLogoutToken, AuthioWebhookDataByType, AuthioTypedWebhookEvent, AuthioCallerKind, AuthioResourceAccessSource } from '@hyper.authio/client';
|
|
3
|
+
import { AuthioUser, AuthioRule, AuthioProtectedResourceMetadataOptions, AuthioVerifier, AuthioWebhookHandler, AuthioVerifierOptions, AuthioWebhookOptions, requireAuthenticationLevel as requireAuthenticationLevel$1, AuthioMatch, requireRecentAuthentication as requireRecentAuthentication$1, AuthioBearerError, AuthioForbiddenError } from '@hyper.authio/server';
|
|
4
|
+
export { AuthioMatch, AuthioRule, AuthioUser, AuthioVerifier, AuthioVerifierOptions } from '@hyper.authio/server';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What `authioBearer` puts on the request.
|
|
8
|
+
*/
|
|
9
|
+
interface AuthioRequestContext {
|
|
10
|
+
/** The caller. */
|
|
11
|
+
readonly user: AuthioUser;
|
|
12
|
+
/** The access token the caller was read from. */
|
|
13
|
+
readonly token: string;
|
|
14
|
+
}
|
|
15
|
+
declare global {
|
|
16
|
+
namespace Express {
|
|
17
|
+
interface Request {
|
|
18
|
+
/** The Authio caller, once `authioBearer` validated the token. */
|
|
19
|
+
authio?: AuthioRequestContext;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* How refusals are answered.
|
|
25
|
+
*/
|
|
26
|
+
interface AuthioResponseOptions {
|
|
27
|
+
/** Answers `401` and `403` with problem details (RFC 9457) as well as the `WWW-Authenticate` header. */
|
|
28
|
+
problemDetails?: boolean;
|
|
29
|
+
/** Passes refusals to `next(error)` instead of answering, for an application's own error handler. */
|
|
30
|
+
passErrors?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Options of `authioBearer`.
|
|
34
|
+
*/
|
|
35
|
+
interface AuthioBearerOptions extends AuthioResponseOptions {
|
|
36
|
+
/** Lets a request without a token through, unauthenticated; a token that is present must still be valid. */
|
|
37
|
+
optional?: boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Validates the `Authorization: Bearer` token and puts the caller on `req.authio`. Answers `401` with the
|
|
41
|
+
* `WWW-Authenticate` challenge when the token is missing or invalid.
|
|
42
|
+
*
|
|
43
|
+
* @param verifier A verifier, or its options.
|
|
44
|
+
*/
|
|
45
|
+
declare function authioBearer(verifier: AuthioVerifier | AuthioVerifierOptions, options?: AuthioBearerOptions): RequestHandler;
|
|
46
|
+
/**
|
|
47
|
+
* An Express guard that is also an Authio rule: use it as middleware, or pass it to `requireAuthio`, `allOf` and
|
|
48
|
+
* `anyOf` like any rule of `@hyper.authio/server`.
|
|
49
|
+
*/
|
|
50
|
+
type AuthioGuard = RequestHandler & AuthioRule;
|
|
51
|
+
/**
|
|
52
|
+
* Requires every rule (guards of this package or rules of `@hyper.authio/server`); answers `401` without a caller and
|
|
53
|
+
* `403` with the joined refusal otherwise.
|
|
54
|
+
*/
|
|
55
|
+
declare function requireAuthio(...rules: AuthioRule[]): AuthioGuard;
|
|
56
|
+
/** Requires granted scopes. */
|
|
57
|
+
declare const requireScope: (scopes: string | readonly string[], match?: AuthioMatch) => AuthioGuard;
|
|
58
|
+
/** Requires roles, from any source or only from the given `*_resource_access` claims. */
|
|
59
|
+
declare const requireRole: (roles: string | readonly string[], options?: {
|
|
60
|
+
match?: AuthioMatch;
|
|
61
|
+
sources?: readonly AuthioResourceAccessSource[];
|
|
62
|
+
}) => AuthioGuard;
|
|
63
|
+
/** Requires a permission (`method` on `resource`). */
|
|
64
|
+
declare const requirePermission: (resource: string, method: string, options?: {
|
|
65
|
+
sources?: readonly AuthioResourceAccessSource[];
|
|
66
|
+
}) => AuthioGuard;
|
|
67
|
+
/** Requires groups. */
|
|
68
|
+
declare const requireGroup: (groups: string | readonly string[], match?: AuthioMatch) => AuthioGuard;
|
|
69
|
+
/** Requires a claim, or one of its values. */
|
|
70
|
+
declare const requireClaim: (type: string, ...values: string[]) => AuthioGuard;
|
|
71
|
+
/** Requires an organization context, or a given organization. */
|
|
72
|
+
declare const requireOrganization: (organization?: string) => AuthioGuard;
|
|
73
|
+
/** Requires a minimum authentication level (`acr`), and optionally a sign-in at most `maxAge` seconds old. */
|
|
74
|
+
declare const requireAuthenticationLevel: (minimumLevel: number, options?: Parameters<typeof requireAuthenticationLevel$1>[1]) => AuthioGuard;
|
|
75
|
+
/** Requires authentication methods (`amr`). */
|
|
76
|
+
declare const requireAuthenticationMethod: (methods: string | readonly string[], match?: AuthioMatch, options?: {
|
|
77
|
+
acrValues?: string;
|
|
78
|
+
}) => AuthioGuard;
|
|
79
|
+
/** Requires a sign-in at most `maxAge` seconds old (`auth_time`). */
|
|
80
|
+
declare const requireRecentAuthentication: (maxAge: number, options?: Parameters<typeof requireRecentAuthentication$1>[1]) => AuthioGuard;
|
|
81
|
+
/** Requires the caller to be of one of the kinds: `user`, `client`, `consumer`, `account_credential`. */
|
|
82
|
+
declare const requireCallerKind: (kinds: AuthioCallerKind | readonly AuthioCallerKind[]) => AuthioGuard;
|
|
83
|
+
/** Requires the token to have been issued to one of the clients. */
|
|
84
|
+
declare const requireClient: (clientIds: string | readonly string[]) => AuthioGuard;
|
|
85
|
+
/** Requires every rule. */
|
|
86
|
+
declare const allOf: (...rules: AuthioRule[]) => AuthioGuard;
|
|
87
|
+
/** Requires at least one rule. */
|
|
88
|
+
declare const anyOf: (...rules: AuthioRule[]) => AuthioGuard;
|
|
89
|
+
/**
|
|
90
|
+
* A webhook route: verifies the signature over the raw body, deduplicates and dispatches. Mount it before any JSON
|
|
91
|
+
* body parser, or behind `express.raw()`.
|
|
92
|
+
*/
|
|
93
|
+
interface AuthioWebhookRoute extends RequestHandler {
|
|
94
|
+
on<T extends keyof AuthioWebhookDataByType>(type: T, handler: AuthioWebhookHandler<AuthioTypedWebhookEvent<T>>): AuthioWebhookRoute;
|
|
95
|
+
on(type: string, handler: AuthioWebhookHandler): AuthioWebhookRoute;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Creates a webhook route: `app.post("/webhooks/authio", authioWebhooks({ secret }).on("user_created", …))`.
|
|
99
|
+
* Answers `200` handled or duplicate, `400` unreadable, `401` bad signature, `413` too large, `500` handler failure.
|
|
100
|
+
*/
|
|
101
|
+
declare function authioWebhooks(options: AuthioWebhookOptions): AuthioWebhookRoute;
|
|
102
|
+
/**
|
|
103
|
+
* Options of `authioProtectedResourceMetadata`: the metadata itself, or a verifier configured with `resource`.
|
|
104
|
+
*/
|
|
105
|
+
type AuthioProtectedResourceMetadataRouteOptions = AuthioProtectedResourceMetadataOptions | (Omit<AuthioProtectedResourceMetadataOptions, "resource" | "issuer" | "authority" | "realm"> & {
|
|
106
|
+
verifier: AuthioVerifier;
|
|
107
|
+
});
|
|
108
|
+
/**
|
|
109
|
+
* Serves the API's protected resource metadata (RFC 9728) at `/.well-known/oauth-protected-resource{path of the
|
|
110
|
+
* resource}`: `app.use(authioProtectedResourceMetadata({ verifier }))`. Every other request goes on to `next`.
|
|
111
|
+
*/
|
|
112
|
+
declare function authioProtectedResourceMetadata(options: AuthioProtectedResourceMetadataRouteOptions): RequestHandler;
|
|
113
|
+
/**
|
|
114
|
+
* Options of `authioBackchannelLogout`.
|
|
115
|
+
*/
|
|
116
|
+
interface AuthioBackchannelLogoutOptions {
|
|
117
|
+
/** The client the logout tokens are addressed to (its id is their audience). */
|
|
118
|
+
client: AuthioClient;
|
|
119
|
+
/**
|
|
120
|
+
* Ends the sessions the token names: every session of `subject`, or the one `sessionId` names. Session storage is
|
|
121
|
+
* the application's.
|
|
122
|
+
*/
|
|
123
|
+
onLogout: (logout: AuthioLogoutToken, request: Request) => void | Promise<void>;
|
|
124
|
+
/** Called when a token is refused. */
|
|
125
|
+
onError?: (error: unknown) => void;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The back-channel logout endpoint (OpenID Connect Back-Channel Logout 1.0): reads `logout_token` from the
|
|
129
|
+
* form-encoded body, validates it with the client and calls `onLogout`. Answers `200` on success, `400`
|
|
130
|
+
* (`invalid_request`) for a token that does not validate, and `500` when `onLogout` throws; always `Cache-Control:
|
|
131
|
+
* no-store`. Works with or without `express.urlencoded()` in front of it.
|
|
132
|
+
*/
|
|
133
|
+
declare function authioBackchannelLogout(options: AuthioBackchannelLogoutOptions): RequestHandler;
|
|
134
|
+
/**
|
|
135
|
+
* Answers a refusal: `WWW-Authenticate`, the status, and problem details when asked.
|
|
136
|
+
*/
|
|
137
|
+
declare function sendAuthioError(response: Response, error: AuthioBearerError | AuthioForbiddenError, options?: AuthioResponseOptions): void;
|
|
138
|
+
|
|
139
|
+
export { type AuthioBackchannelLogoutOptions, type AuthioBearerOptions, type AuthioGuard, type AuthioProtectedResourceMetadataRouteOptions, type AuthioRequestContext, type AuthioResponseOptions, type AuthioWebhookRoute, allOf, anyOf, authioBackchannelLogout, authioBearer, authioProtectedResourceMetadata, authioWebhooks, requireAuthenticationLevel, requireAuthenticationMethod, requireAuthio, requireCallerKind, requireClaim, requireClient, requireGroup, requireOrganization, requirePermission, requireRecentAuthentication, requireRole, requireScope, sendAuthioError };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Request, RequestHandler, Response } from 'express';
|
|
2
|
+
import { AuthioClient, AuthioLogoutToken, AuthioWebhookDataByType, AuthioTypedWebhookEvent, AuthioCallerKind, AuthioResourceAccessSource } from '@hyper.authio/client';
|
|
3
|
+
import { AuthioUser, AuthioRule, AuthioProtectedResourceMetadataOptions, AuthioVerifier, AuthioWebhookHandler, AuthioVerifierOptions, AuthioWebhookOptions, requireAuthenticationLevel as requireAuthenticationLevel$1, AuthioMatch, requireRecentAuthentication as requireRecentAuthentication$1, AuthioBearerError, AuthioForbiddenError } from '@hyper.authio/server';
|
|
4
|
+
export { AuthioMatch, AuthioRule, AuthioUser, AuthioVerifier, AuthioVerifierOptions } from '@hyper.authio/server';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What `authioBearer` puts on the request.
|
|
8
|
+
*/
|
|
9
|
+
interface AuthioRequestContext {
|
|
10
|
+
/** The caller. */
|
|
11
|
+
readonly user: AuthioUser;
|
|
12
|
+
/** The access token the caller was read from. */
|
|
13
|
+
readonly token: string;
|
|
14
|
+
}
|
|
15
|
+
declare global {
|
|
16
|
+
namespace Express {
|
|
17
|
+
interface Request {
|
|
18
|
+
/** The Authio caller, once `authioBearer` validated the token. */
|
|
19
|
+
authio?: AuthioRequestContext;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* How refusals are answered.
|
|
25
|
+
*/
|
|
26
|
+
interface AuthioResponseOptions {
|
|
27
|
+
/** Answers `401` and `403` with problem details (RFC 9457) as well as the `WWW-Authenticate` header. */
|
|
28
|
+
problemDetails?: boolean;
|
|
29
|
+
/** Passes refusals to `next(error)` instead of answering, for an application's own error handler. */
|
|
30
|
+
passErrors?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Options of `authioBearer`.
|
|
34
|
+
*/
|
|
35
|
+
interface AuthioBearerOptions extends AuthioResponseOptions {
|
|
36
|
+
/** Lets a request without a token through, unauthenticated; a token that is present must still be valid. */
|
|
37
|
+
optional?: boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Validates the `Authorization: Bearer` token and puts the caller on `req.authio`. Answers `401` with the
|
|
41
|
+
* `WWW-Authenticate` challenge when the token is missing or invalid.
|
|
42
|
+
*
|
|
43
|
+
* @param verifier A verifier, or its options.
|
|
44
|
+
*/
|
|
45
|
+
declare function authioBearer(verifier: AuthioVerifier | AuthioVerifierOptions, options?: AuthioBearerOptions): RequestHandler;
|
|
46
|
+
/**
|
|
47
|
+
* An Express guard that is also an Authio rule: use it as middleware, or pass it to `requireAuthio`, `allOf` and
|
|
48
|
+
* `anyOf` like any rule of `@hyper.authio/server`.
|
|
49
|
+
*/
|
|
50
|
+
type AuthioGuard = RequestHandler & AuthioRule;
|
|
51
|
+
/**
|
|
52
|
+
* Requires every rule (guards of this package or rules of `@hyper.authio/server`); answers `401` without a caller and
|
|
53
|
+
* `403` with the joined refusal otherwise.
|
|
54
|
+
*/
|
|
55
|
+
declare function requireAuthio(...rules: AuthioRule[]): AuthioGuard;
|
|
56
|
+
/** Requires granted scopes. */
|
|
57
|
+
declare const requireScope: (scopes: string | readonly string[], match?: AuthioMatch) => AuthioGuard;
|
|
58
|
+
/** Requires roles, from any source or only from the given `*_resource_access` claims. */
|
|
59
|
+
declare const requireRole: (roles: string | readonly string[], options?: {
|
|
60
|
+
match?: AuthioMatch;
|
|
61
|
+
sources?: readonly AuthioResourceAccessSource[];
|
|
62
|
+
}) => AuthioGuard;
|
|
63
|
+
/** Requires a permission (`method` on `resource`). */
|
|
64
|
+
declare const requirePermission: (resource: string, method: string, options?: {
|
|
65
|
+
sources?: readonly AuthioResourceAccessSource[];
|
|
66
|
+
}) => AuthioGuard;
|
|
67
|
+
/** Requires groups. */
|
|
68
|
+
declare const requireGroup: (groups: string | readonly string[], match?: AuthioMatch) => AuthioGuard;
|
|
69
|
+
/** Requires a claim, or one of its values. */
|
|
70
|
+
declare const requireClaim: (type: string, ...values: string[]) => AuthioGuard;
|
|
71
|
+
/** Requires an organization context, or a given organization. */
|
|
72
|
+
declare const requireOrganization: (organization?: string) => AuthioGuard;
|
|
73
|
+
/** Requires a minimum authentication level (`acr`), and optionally a sign-in at most `maxAge` seconds old. */
|
|
74
|
+
declare const requireAuthenticationLevel: (minimumLevel: number, options?: Parameters<typeof requireAuthenticationLevel$1>[1]) => AuthioGuard;
|
|
75
|
+
/** Requires authentication methods (`amr`). */
|
|
76
|
+
declare const requireAuthenticationMethod: (methods: string | readonly string[], match?: AuthioMatch, options?: {
|
|
77
|
+
acrValues?: string;
|
|
78
|
+
}) => AuthioGuard;
|
|
79
|
+
/** Requires a sign-in at most `maxAge` seconds old (`auth_time`). */
|
|
80
|
+
declare const requireRecentAuthentication: (maxAge: number, options?: Parameters<typeof requireRecentAuthentication$1>[1]) => AuthioGuard;
|
|
81
|
+
/** Requires the caller to be of one of the kinds: `user`, `client`, `consumer`, `account_credential`. */
|
|
82
|
+
declare const requireCallerKind: (kinds: AuthioCallerKind | readonly AuthioCallerKind[]) => AuthioGuard;
|
|
83
|
+
/** Requires the token to have been issued to one of the clients. */
|
|
84
|
+
declare const requireClient: (clientIds: string | readonly string[]) => AuthioGuard;
|
|
85
|
+
/** Requires every rule. */
|
|
86
|
+
declare const allOf: (...rules: AuthioRule[]) => AuthioGuard;
|
|
87
|
+
/** Requires at least one rule. */
|
|
88
|
+
declare const anyOf: (...rules: AuthioRule[]) => AuthioGuard;
|
|
89
|
+
/**
|
|
90
|
+
* A webhook route: verifies the signature over the raw body, deduplicates and dispatches. Mount it before any JSON
|
|
91
|
+
* body parser, or behind `express.raw()`.
|
|
92
|
+
*/
|
|
93
|
+
interface AuthioWebhookRoute extends RequestHandler {
|
|
94
|
+
on<T extends keyof AuthioWebhookDataByType>(type: T, handler: AuthioWebhookHandler<AuthioTypedWebhookEvent<T>>): AuthioWebhookRoute;
|
|
95
|
+
on(type: string, handler: AuthioWebhookHandler): AuthioWebhookRoute;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Creates a webhook route: `app.post("/webhooks/authio", authioWebhooks({ secret }).on("user_created", …))`.
|
|
99
|
+
* Answers `200` handled or duplicate, `400` unreadable, `401` bad signature, `413` too large, `500` handler failure.
|
|
100
|
+
*/
|
|
101
|
+
declare function authioWebhooks(options: AuthioWebhookOptions): AuthioWebhookRoute;
|
|
102
|
+
/**
|
|
103
|
+
* Options of `authioProtectedResourceMetadata`: the metadata itself, or a verifier configured with `resource`.
|
|
104
|
+
*/
|
|
105
|
+
type AuthioProtectedResourceMetadataRouteOptions = AuthioProtectedResourceMetadataOptions | (Omit<AuthioProtectedResourceMetadataOptions, "resource" | "issuer" | "authority" | "realm"> & {
|
|
106
|
+
verifier: AuthioVerifier;
|
|
107
|
+
});
|
|
108
|
+
/**
|
|
109
|
+
* Serves the API's protected resource metadata (RFC 9728) at `/.well-known/oauth-protected-resource{path of the
|
|
110
|
+
* resource}`: `app.use(authioProtectedResourceMetadata({ verifier }))`. Every other request goes on to `next`.
|
|
111
|
+
*/
|
|
112
|
+
declare function authioProtectedResourceMetadata(options: AuthioProtectedResourceMetadataRouteOptions): RequestHandler;
|
|
113
|
+
/**
|
|
114
|
+
* Options of `authioBackchannelLogout`.
|
|
115
|
+
*/
|
|
116
|
+
interface AuthioBackchannelLogoutOptions {
|
|
117
|
+
/** The client the logout tokens are addressed to (its id is their audience). */
|
|
118
|
+
client: AuthioClient;
|
|
119
|
+
/**
|
|
120
|
+
* Ends the sessions the token names: every session of `subject`, or the one `sessionId` names. Session storage is
|
|
121
|
+
* the application's.
|
|
122
|
+
*/
|
|
123
|
+
onLogout: (logout: AuthioLogoutToken, request: Request) => void | Promise<void>;
|
|
124
|
+
/** Called when a token is refused. */
|
|
125
|
+
onError?: (error: unknown) => void;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The back-channel logout endpoint (OpenID Connect Back-Channel Logout 1.0): reads `logout_token` from the
|
|
129
|
+
* form-encoded body, validates it with the client and calls `onLogout`. Answers `200` on success, `400`
|
|
130
|
+
* (`invalid_request`) for a token that does not validate, and `500` when `onLogout` throws; always `Cache-Control:
|
|
131
|
+
* no-store`. Works with or without `express.urlencoded()` in front of it.
|
|
132
|
+
*/
|
|
133
|
+
declare function authioBackchannelLogout(options: AuthioBackchannelLogoutOptions): RequestHandler;
|
|
134
|
+
/**
|
|
135
|
+
* Answers a refusal: `WWW-Authenticate`, the status, and problem details when asked.
|
|
136
|
+
*/
|
|
137
|
+
declare function sendAuthioError(response: Response, error: AuthioBearerError | AuthioForbiddenError, options?: AuthioResponseOptions): void;
|
|
138
|
+
|
|
139
|
+
export { type AuthioBackchannelLogoutOptions, type AuthioBearerOptions, type AuthioGuard, type AuthioProtectedResourceMetadataRouteOptions, type AuthioRequestContext, type AuthioResponseOptions, type AuthioWebhookRoute, allOf, anyOf, authioBackchannelLogout, authioBearer, authioProtectedResourceMetadata, authioWebhooks, requireAuthenticationLevel, requireAuthenticationMethod, requireAuthio, requireCallerKind, requireClaim, requireClient, requireGroup, requireOrganization, requirePermission, requireRecentAuthentication, requireRole, requireScope, sendAuthioError };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { AuthioTokenValidationError } from '@hyper.authio/client';
|
|
2
|
+
import { createAuthioVerifier, requireScope as requireScope$1, AuthioForbiddenError, allOf as allOf$1, authorize, requireRole as requireRole$1, requirePermission as requirePermission$1, requireGroup as requireGroup$1, requireClaim as requireClaim$1, requireOrganization as requireOrganization$1, requireAuthenticationLevel as requireAuthenticationLevel$1, requireAuthenticationMethod as requireAuthenticationMethod$1, requireRecentAuthentication as requireRecentAuthentication$1, requireCallerKind as requireCallerKind$1, requireClient as requireClient$1, anyOf as anyOf$1, createAuthioWebhooks, createProtectedResourceMetadata, protectedResourceMetadataPath, toProblemDetails, AuthioBearerError } from '@hyper.authio/server';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
var responseSettings = /* @__PURE__ */ Symbol.for("authio.express.response");
|
|
6
|
+
function authioBearer(verifier, options = {}) {
|
|
7
|
+
const resolved = isVerifier(verifier) ? verifier : createAuthioVerifier(verifier);
|
|
8
|
+
return (request, response, next) => {
|
|
9
|
+
request[responseSettings] = { problemDetails: options.problemDetails, passErrors: options.passErrors };
|
|
10
|
+
const header = request.headers.authorization;
|
|
11
|
+
if (options.optional && !header) {
|
|
12
|
+
next();
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
resolved.verifyAuthorizationHeader(header).then((user) => {
|
|
16
|
+
request.authio = { user, token: user.token };
|
|
17
|
+
const failure = resolved.requiredScopes.length > 0 ? requireScope$1(resolved.requiredScopes, "all").check(user) : void 0;
|
|
18
|
+
if (failure) return refuse(request, response, next, new AuthioForbiddenError(failure), options);
|
|
19
|
+
next();
|
|
20
|
+
}).catch((error) => refuse(request, response, next, error, options));
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function requireAuthio(...rules) {
|
|
24
|
+
if (rules.length === 0) throw new TypeError("requireAuthio needs at least one rule.");
|
|
25
|
+
const rule = rules.length === 1 ? rules[0] : allOf$1(...rules);
|
|
26
|
+
const guard = (request, response, next) => {
|
|
27
|
+
const user = request.authio?.user;
|
|
28
|
+
if (!user) return refuse(request, response, next, new AuthioBearerError(void 0, { error: void 0 }));
|
|
29
|
+
const failure = authorize(user, ...rules);
|
|
30
|
+
if (failure) return refuse(request, response, next, new AuthioForbiddenError(failure));
|
|
31
|
+
next();
|
|
32
|
+
};
|
|
33
|
+
return Object.assign(guard, { description: rule.description, check: (user) => authorize(user, ...rules) });
|
|
34
|
+
}
|
|
35
|
+
var requireScope = (scopes, match) => requireAuthio(requireScope$1(scopes, match));
|
|
36
|
+
var requireRole = (roles, options) => requireAuthio(requireRole$1(roles, options));
|
|
37
|
+
var requirePermission = (resource, method, options) => requireAuthio(requirePermission$1(resource, method, options));
|
|
38
|
+
var requireGroup = (groups, match) => requireAuthio(requireGroup$1(groups, match));
|
|
39
|
+
var requireClaim = (type, ...values) => requireAuthio(requireClaim$1(type, ...values));
|
|
40
|
+
var requireOrganization = (organization) => requireAuthio(requireOrganization$1(organization));
|
|
41
|
+
var requireAuthenticationLevel = (minimumLevel, options) => requireAuthio(requireAuthenticationLevel$1(minimumLevel, options));
|
|
42
|
+
var requireAuthenticationMethod = (methods, match, options) => requireAuthio(requireAuthenticationMethod$1(methods, match, options));
|
|
43
|
+
var requireRecentAuthentication = (maxAge, options) => requireAuthio(requireRecentAuthentication$1(maxAge, options));
|
|
44
|
+
var requireCallerKind = (kinds) => requireAuthio(requireCallerKind$1(kinds));
|
|
45
|
+
var requireClient = (clientIds) => requireAuthio(requireClient$1(clientIds));
|
|
46
|
+
var allOf = (...rules) => requireAuthio(allOf$1(...rules));
|
|
47
|
+
var anyOf = (...rules) => requireAuthio(anyOf$1(...rules));
|
|
48
|
+
function authioWebhooks(options) {
|
|
49
|
+
const webhooks = createAuthioWebhooks(options);
|
|
50
|
+
const route = ((request, response, next) => {
|
|
51
|
+
readRawBody(request, webhooks.maxBodySize).then(async (body) => {
|
|
52
|
+
if (body === void 0) {
|
|
53
|
+
response.status(413).end();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const outcome = await webhooks.handle({ body, headers: request.headers });
|
|
57
|
+
response.status(outcome.status).end();
|
|
58
|
+
}).catch(next);
|
|
59
|
+
});
|
|
60
|
+
route.on = ((type, handler) => {
|
|
61
|
+
webhooks.on(type, handler);
|
|
62
|
+
return route;
|
|
63
|
+
});
|
|
64
|
+
return route;
|
|
65
|
+
}
|
|
66
|
+
function authioProtectedResourceMetadata(options) {
|
|
67
|
+
const settings = "verifier" in options ? {
|
|
68
|
+
...options,
|
|
69
|
+
resource: requireResource(options.verifier),
|
|
70
|
+
issuer: options.verifier.connection.issuer,
|
|
71
|
+
scopesSupported: options.scopesSupported ?? (options.verifier.requiredScopes.length > 0 ? options.verifier.requiredScopes : void 0)
|
|
72
|
+
} : options;
|
|
73
|
+
const document = JSON.stringify(createProtectedResourceMetadata(settings));
|
|
74
|
+
const path = protectedResourceMetadataPath(settings.resource);
|
|
75
|
+
return (request, response, next) => {
|
|
76
|
+
if (request.method !== "GET" && request.method !== "HEAD" || request.path.replace(/\/+$/, "") !== path) return next();
|
|
77
|
+
response.set("Cache-Control", "public, max-age=3600").type("application/json").send(document);
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function authioBackchannelLogout(options) {
|
|
81
|
+
return (request, response, next) => {
|
|
82
|
+
response.set("Cache-Control", "no-store");
|
|
83
|
+
readLogoutToken(request).then(async (logoutToken) => {
|
|
84
|
+
if (!logoutToken) {
|
|
85
|
+
response.status(400).json({ error: "invalid_request", error_description: "The request carries no logout_token." });
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
let logout;
|
|
89
|
+
try {
|
|
90
|
+
logout = await options.client.validateLogoutToken(logoutToken);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
options.onError?.(error);
|
|
93
|
+
if (!(error instanceof AuthioTokenValidationError)) throw error;
|
|
94
|
+
response.status(400).json({ error: "invalid_request", error_description: error.message });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
await options.onLogout(logout, request);
|
|
98
|
+
response.status(200).end();
|
|
99
|
+
}).catch(next);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function sendAuthioError(response, error, options = {}) {
|
|
103
|
+
const challenge = error.wwwAuthenticate;
|
|
104
|
+
if (challenge) response.append("WWW-Authenticate", challenge);
|
|
105
|
+
response.status(error.status);
|
|
106
|
+
if (options.problemDetails) response.type("application/problem+json").send(JSON.stringify(toProblemDetails(error)));
|
|
107
|
+
else response.end();
|
|
108
|
+
}
|
|
109
|
+
function refuse(request, response, next, error, options) {
|
|
110
|
+
const settings = options ?? request[responseSettings] ?? {};
|
|
111
|
+
if (!(error instanceof AuthioBearerError) && !(error instanceof AuthioForbiddenError)) return next(error);
|
|
112
|
+
if (settings.passErrors) return next(error);
|
|
113
|
+
sendAuthioError(response, error, settings);
|
|
114
|
+
}
|
|
115
|
+
function readRawBody(request, maxBodySize) {
|
|
116
|
+
const parsed = request.body;
|
|
117
|
+
if (Buffer.isBuffer(parsed)) return Promise.resolve(parsed.byteLength > maxBodySize ? void 0 : new Uint8Array(parsed));
|
|
118
|
+
if (typeof parsed === "string") return Promise.resolve(new TextEncoder().encode(parsed));
|
|
119
|
+
if (request.readableEnded || parsed !== void 0 && parsed !== null && typeof parsed === "object" && Object.keys(parsed).length > 0)
|
|
120
|
+
return Promise.reject(
|
|
121
|
+
new Error("The Authio webhook route needs the raw body: mount it before express.json(), or use express.raw({ type: '*/*' }) on it.")
|
|
122
|
+
);
|
|
123
|
+
const declared = Number(request.headers["content-length"]);
|
|
124
|
+
if (Number.isFinite(declared) && declared > maxBodySize) return Promise.resolve(void 0);
|
|
125
|
+
return new Promise((resolve, reject) => {
|
|
126
|
+
const chunks = [];
|
|
127
|
+
let size = 0;
|
|
128
|
+
let done = false;
|
|
129
|
+
request.on("data", (chunk) => {
|
|
130
|
+
if (done) return;
|
|
131
|
+
size += chunk.byteLength;
|
|
132
|
+
if (size > maxBodySize) {
|
|
133
|
+
done = true;
|
|
134
|
+
request.pause();
|
|
135
|
+
resolve(void 0);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
chunks.push(chunk);
|
|
139
|
+
});
|
|
140
|
+
request.on("end", () => {
|
|
141
|
+
if (!done) resolve(new Uint8Array(Buffer.concat(chunks)));
|
|
142
|
+
});
|
|
143
|
+
request.on("error", reject);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
async function readLogoutToken(request) {
|
|
147
|
+
const parsed = request.body;
|
|
148
|
+
if (parsed && typeof parsed === "object" && !Buffer.isBuffer(parsed) && typeof parsed.logout_token === "string")
|
|
149
|
+
return parsed.logout_token;
|
|
150
|
+
const raw = Buffer.isBuffer(parsed) ? new Uint8Array(parsed) : typeof parsed === "string" ? new TextEncoder().encode(parsed) : request.readableEnded ? void 0 : await readRawBody(request, 64 * 1024);
|
|
151
|
+
return raw ? new URLSearchParams(new TextDecoder().decode(raw)).get("logout_token") ?? void 0 : void 0;
|
|
152
|
+
}
|
|
153
|
+
function requireResource(verifier) {
|
|
154
|
+
if (!verifier.resource) throw new TypeError("authioProtectedResourceMetadata needs a verifier configured with resource.");
|
|
155
|
+
return verifier.resource;
|
|
156
|
+
}
|
|
157
|
+
function isVerifier(value) {
|
|
158
|
+
return typeof value.verify === "function";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export { allOf, anyOf, authioBackchannelLogout, authioBearer, authioProtectedResourceMetadata, authioWebhooks, requireAuthenticationLevel, requireAuthenticationMethod, requireAuthio, requireCallerKind, requireClaim, requireClient, requireGroup, requireOrganization, requirePermission, requireRecentAuthentication, requireRole, requireScope, sendAuthioError };
|
|
162
|
+
//# sourceMappingURL=index.js.map
|
|
163
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["scopeRule","allOfRule","roleRule","permissionRule","groupRule","claimRule","organizationRule","authenticationLevelRule","authenticationMethodRule","recentAuthenticationRule","callerKindRule","clientRule","anyOfRule"],"mappings":";;;;AAiFA,IAAM,gBAAA,mBAAmB,MAAA,CAAO,GAAA,CAAI,yBAAyB,CAAA;AAUtD,SAAS,YAAA,CAAa,QAAA,EAAkD,OAAA,GAA+B,EAAC,EAAmB;AAChI,EAAA,MAA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,SAAS,MAAM,CAAA;AAAA,EAC5B,CAAC,CAAA;AACH;AAEA,eAAe,gBAAgB,OAAA,EAA+C;AAC5E,EAAA,MAAM,SAAU,OAAA,CAAyC,IAAA;AAEzD,EAAA,IAAI,MAAA,IAAU,OAAO,MAAA,KAAW,QAAA,IAAY,CAAC,MAAA,CAAO,QAAA,CAAS,MAAM,CAAA,IAAK,OAAQ,MAAA,CAAmC,YAAA,KAAiB,QAAA;AAClI,IAAA,OAAQ,MAAA,CAAkC,YAAA;AAE5C,EAAA,MAAM,GAAA,GAAM,MAAA,CAAO,QAAA,CAAS,MAAM,CAAA,GAC9B,IAAI,UAAA,CAAW,MAAM,CAAA,GACrB,OAAO,MAAA,KAAW,QAAA,GAChB,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,MAAM,CAAA,GAC/B,OAAA,CAAQ,aAAA,GACN,MAAA,GACA,MAAM,WAAA,CAAY,OAAA,EAAS,EAAA,GAAK,IAAI,CAAA;AAE5C,EAAA,OAAO,GAAA,GAAO,IAAI,eAAA,CAAgB,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,GAAG,CAAC,CAAA,CAAE,GAAA,CAAI,cAAc,KAAK,MAAA,GAAa,MAAA;AACvG;AAEA,SAAS,gBAAgB,QAAA,EAAkC;AACzD,EAAA,IAAI,CAAC,QAAA,CAAS,QAAA,EAAU,MAAM,IAAI,UAAU,4EAA4E,CAAA;AAExH,EAAA,OAAO,QAAA,CAAS,QAAA;AAClB;AAEA,SAAS,WAAW,KAAA,EAAwE;AAC1F,EAAA,OAAO,OAAQ,MAAyB,MAAA,KAAW,UAAA;AACrD","file":"index.js","sourcesContent":["import type { NextFunction, Request, RequestHandler, Response } from \"express\";\nimport {\n AuthioTokenValidationError,\n type AuthioCallerKind,\n type AuthioClient,\n type AuthioLogoutToken,\n type AuthioResourceAccessSource,\n type AuthioTypedWebhookEvent,\n type AuthioWebhookDataByType,\n} from \"@hyper.authio/client\";\nimport {\n allOf as allOfRule,\n anyOf as anyOfRule,\n authorize,\n AuthioBearerError,\n AuthioForbiddenError,\n createAuthioVerifier,\n createAuthioWebhooks,\n createProtectedResourceMetadata,\n protectedResourceMetadataPath,\n requireAuthenticationLevel as authenticationLevelRule,\n requireAuthenticationMethod as authenticationMethodRule,\n requireCallerKind as callerKindRule,\n requireClaim as claimRule,\n requireClient as clientRule,\n requireGroup as groupRule,\n requireOrganization as organizationRule,\n requirePermission as permissionRule,\n requireRecentAuthentication as recentAuthenticationRule,\n requireRole as roleRule,\n requireScope as scopeRule,\n toProblemDetails,\n type AuthioProtectedResourceMetadataOptions,\n type AuthioMatch,\n type AuthioRule,\n type AuthioRuleFailure,\n type AuthioUser,\n type AuthioVerifier,\n type AuthioVerifierOptions,\n type AuthioWebhookHandler,\n type AuthioWebhookOptions,\n} from \"@hyper.authio/server\";\n\n/**\n * What `authioBearer` puts on the request.\n */\nexport interface AuthioRequestContext {\n /** The caller. */\n readonly user: AuthioUser;\n /** The access token the caller was read from. */\n readonly token: string;\n}\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace Express {\n interface Request {\n /** The Authio caller, once `authioBearer` validated the token. */\n authio?: AuthioRequestContext;\n }\n }\n}\n\n/**\n * How refusals are answered.\n */\nexport interface AuthioResponseOptions {\n /** Answers `401` and `403` with problem details (RFC 9457) as well as the `WWW-Authenticate` header. */\n problemDetails?: boolean;\n /** Passes refusals to `next(error)` instead of answering, for an application's own error handler. */\n passErrors?: boolean;\n}\n\n/**\n * Options of `authioBearer`.\n */\nexport interface AuthioBearerOptions extends AuthioResponseOptions {\n /** Lets a request without a token through, unauthenticated; a token that is present must still be valid. */\n optional?: boolean;\n}\n\nconst responseSettings = Symbol.for(\"authio.express.response\");\n\ntype RequestWithSettings = Request & { [responseSettings]?: AuthioResponseOptions };\n\n/**\n * Validates the `Authorization: Bearer` token and puts the caller on `req.authio`. Answers `401` with the\n * `WWW-Authenticate` challenge when the token is missing or invalid.\n *\n * @param verifier A verifier, or its options.\n */\nexport function authioBearer(verifier: AuthioVerifier | AuthioVerifierOptions, options: AuthioBearerOptions = {}): RequestHandler {\n const resolved = isVerifier(verifier) ? verifier : createAuthioVerifier(verifier);\n\n return (request, response, next) => {\n (request as RequestWithSettings)[responseSettings] = { problemDetails: options.problemDetails, passErrors: options.passErrors };\n\n const header = request.headers.authorization;\n\n if (options.optional && !header) {\n next();\n return;\n }\n\n resolved\n .verifyAuthorizationHeader(header)\n .then((user) => {\n request.authio = { user, token: user.token! };\n\n const failure = resolved.requiredScopes.length > 0 ? scopeRule(resolved.requiredScopes, \"all\").check(user) : undefined;\n\n if (failure) return refuse(request, response, next, new AuthioForbiddenError(failure as AuthioRuleFailure), options);\n\n next();\n })\n .catch((error: unknown) => refuse(request, response, next, error, options));\n };\n}\n\n/**\n * An Express guard that is also an Authio rule: use it as middleware, or pass it to `requireAuthio`, `allOf` and\n * `anyOf` like any rule of `@hyper.authio/server`.\n */\nexport type AuthioGuard = RequestHandler & AuthioRule;\n\n/**\n * Requires every rule (guards of this package or rules of `@hyper.authio/server`); answers `401` without a caller and\n * `403` with the joined refusal otherwise.\n */\nexport function requireAuthio(...rules: AuthioRule[]): AuthioGuard {\n if (rules.length === 0) throw new TypeError(\"requireAuthio needs at least one rule.\");\n\n const rule = rules.length === 1 ? rules[0]! : allOfRule(...rules);\n const guard: RequestHandler = (request, response, next) => {\n const user = request.authio?.user;\n\n if (!user) return refuse(request, response, next, new AuthioBearerError(undefined, { error: undefined }));\n\n const failure = authorize(user, ...rules);\n\n if (failure) return refuse(request, response, next, new AuthioForbiddenError(failure));\n\n next();\n };\n\n return Object.assign(guard, { description: rule.description, check: (user: AuthioUser) => authorize(user, ...rules) });\n}\n\n/** Requires granted scopes. */\nexport const requireScope = (scopes: string | readonly string[], match?: AuthioMatch): AuthioGuard => requireAuthio(scopeRule(scopes, match));\n/** Requires roles, from any source or only from the given `*_resource_access` claims. */\nexport const requireRole = (\n roles: string | readonly string[],\n options?: { match?: AuthioMatch; sources?: readonly AuthioResourceAccessSource[] },\n): AuthioGuard => requireAuthio(roleRule(roles, options));\n/** Requires a permission (`method` on `resource`). */\nexport const requirePermission = (resource: string, method: string, options?: { sources?: readonly AuthioResourceAccessSource[] }): AuthioGuard =>\n requireAuthio(permissionRule(resource, method, options));\n/** Requires groups. */\nexport const requireGroup = (groups: string | readonly string[], match?: AuthioMatch): AuthioGuard => requireAuthio(groupRule(groups, match));\n/** Requires a claim, or one of its values. */\nexport const requireClaim = (type: string, ...values: string[]): AuthioGuard => requireAuthio(claimRule(type, ...values));\n/** Requires an organization context, or a given organization. */\nexport const requireOrganization = (organization?: string): AuthioGuard => requireAuthio(organizationRule(organization));\n/** Requires a minimum authentication level (`acr`), and optionally a sign-in at most `maxAge` seconds old. */\nexport const requireAuthenticationLevel = (minimumLevel: number, options?: Parameters<typeof authenticationLevelRule>[1]): AuthioGuard =>\n requireAuthio(authenticationLevelRule(minimumLevel, options));\n/** Requires authentication methods (`amr`). */\nexport const requireAuthenticationMethod = (methods: string | readonly string[], match?: AuthioMatch, options?: { acrValues?: string }): AuthioGuard =>\n requireAuthio(authenticationMethodRule(methods, match, options));\n/** Requires a sign-in at most `maxAge` seconds old (`auth_time`). */\nexport const requireRecentAuthentication = (maxAge: number, options?: Parameters<typeof recentAuthenticationRule>[1]): AuthioGuard =>\n requireAuthio(recentAuthenticationRule(maxAge, options));\n/** Requires the caller to be of one of the kinds: `user`, `client`, `consumer`, `account_credential`. */\nexport const requireCallerKind = (kinds: AuthioCallerKind | readonly AuthioCallerKind[]): AuthioGuard => requireAuthio(callerKindRule(kinds));\n/** Requires the token to have been issued to one of the clients. */\nexport const requireClient = (clientIds: string | readonly string[]): AuthioGuard => requireAuthio(clientRule(clientIds));\n/** Requires every rule. */\nexport const allOf = (...rules: AuthioRule[]): AuthioGuard => requireAuthio(allOfRule(...rules));\n/** Requires at least one rule. */\nexport const anyOf = (...rules: AuthioRule[]): AuthioGuard => requireAuthio(anyOfRule(...rules));\n\n/**\n * A webhook route: verifies the signature over the raw body, deduplicates and dispatches. Mount it before any JSON\n * body parser, or behind `express.raw()`.\n */\nexport interface AuthioWebhookRoute extends RequestHandler {\n on<T extends keyof AuthioWebhookDataByType>(type: T, handler: AuthioWebhookHandler<AuthioTypedWebhookEvent<T>>): AuthioWebhookRoute;\n on(type: string, handler: AuthioWebhookHandler): AuthioWebhookRoute;\n}\n\n/**\n * Creates a webhook route: `app.post(\"/webhooks/authio\", authioWebhooks({ secret }).on(\"user_created\", …))`.\n * Answers `200` handled or duplicate, `400` unreadable, `401` bad signature, `413` too large, `500` handler failure.\n */\nexport function authioWebhooks(options: AuthioWebhookOptions): AuthioWebhookRoute {\n const webhooks = createAuthioWebhooks(options);\n\n const route = ((request: Request, response: Response, next: NextFunction) => {\n readRawBody(request, webhooks.maxBodySize)\n .then(async (body) => {\n if (body === undefined) {\n response.status(413).end();\n return;\n }\n\n const outcome = await webhooks.handle({ body, headers: request.headers });\n\n response.status(outcome.status).end();\n })\n .catch(next);\n }) as AuthioWebhookRoute;\n\n route.on = ((type: string, handler: AuthioWebhookHandler) => {\n webhooks.on(type, handler);\n return route;\n }) as AuthioWebhookRoute[\"on\"];\n\n return route;\n}\n\n/**\n * Options of `authioProtectedResourceMetadata`: the metadata itself, or a verifier configured with `resource`.\n */\nexport type AuthioProtectedResourceMetadataRouteOptions =\n | AuthioProtectedResourceMetadataOptions\n | (Omit<AuthioProtectedResourceMetadataOptions, \"resource\" | \"issuer\" | \"authority\" | \"realm\"> & { verifier: AuthioVerifier });\n\n/**\n * Serves the API's protected resource metadata (RFC 9728) at `/.well-known/oauth-protected-resource{path of the\n * resource}`: `app.use(authioProtectedResourceMetadata({ verifier }))`. Every other request goes on to `next`.\n */\nexport function authioProtectedResourceMetadata(options: AuthioProtectedResourceMetadataRouteOptions): RequestHandler {\n const settings: AuthioProtectedResourceMetadataOptions =\n \"verifier\" in options\n ? {\n ...options,\n resource: requireResource(options.verifier),\n issuer: options.verifier.connection.issuer,\n scopesSupported: options.scopesSupported ?? (options.verifier.requiredScopes.length > 0 ? options.verifier.requiredScopes : undefined),\n }\n : options;\n const document = JSON.stringify(createProtectedResourceMetadata(settings));\n const path = protectedResourceMetadataPath(settings.resource);\n\n return (request, response, next) => {\n if ((request.method !== \"GET\" && request.method !== \"HEAD\") || request.path.replace(/\\/+$/, \"\") !== path) return next();\n\n response.set(\"Cache-Control\", \"public, max-age=3600\").type(\"application/json\").send(document);\n };\n}\n\n/**\n * Options of `authioBackchannelLogout`.\n */\nexport interface AuthioBackchannelLogoutOptions {\n /** The client the logout tokens are addressed to (its id is their audience). */\n client: AuthioClient;\n /**\n * Ends the sessions the token names: every session of `subject`, or the one `sessionId` names. Session storage is\n * the application's.\n */\n onLogout: (logout: AuthioLogoutToken, request: Request) => void | Promise<void>;\n /** Called when a token is refused. */\n onError?: (error: unknown) => void;\n}\n\n/**\n * The back-channel logout endpoint (OpenID Connect Back-Channel Logout 1.0): reads `logout_token` from the\n * form-encoded body, validates it with the client and calls `onLogout`. Answers `200` on success, `400`\n * (`invalid_request`) for a token that does not validate, and `500` when `onLogout` throws; always `Cache-Control:\n * no-store`. Works with or without `express.urlencoded()` in front of it.\n */\nexport function authioBackchannelLogout(options: AuthioBackchannelLogoutOptions): RequestHandler {\n return (request, response, next) => {\n response.set(\"Cache-Control\", \"no-store\");\n\n readLogoutToken(request)\n .then(async (logoutToken) => {\n if (!logoutToken) {\n response.status(400).json({ error: \"invalid_request\", error_description: \"The request carries no logout_token.\" });\n return;\n }\n\n let logout: AuthioLogoutToken;\n\n try {\n logout = await options.client.validateLogoutToken(logoutToken);\n } catch (error) {\n options.onError?.(error);\n if (!(error instanceof AuthioTokenValidationError)) throw error;\n response.status(400).json({ error: \"invalid_request\", error_description: error.message });\n return;\n }\n\n await options.onLogout(logout, request);\n response.status(200).end();\n })\n .catch(next);\n };\n}\n\n/**\n * Answers a refusal: `WWW-Authenticate`, the status, and problem details when asked.\n */\nexport function sendAuthioError(response: Response, error: AuthioBearerError | AuthioForbiddenError, options: AuthioResponseOptions = {}): void {\n const challenge = error.wwwAuthenticate;\n\n if (challenge) response.append(\"WWW-Authenticate\", challenge);\n\n response.status(error.status);\n\n if (options.problemDetails) response.type(\"application/problem+json\").send(JSON.stringify(toProblemDetails(error)));\n else response.end();\n}\n\nfunction refuse(request: Request, response: Response, next: NextFunction, error: unknown, options?: AuthioResponseOptions): void {\n const settings = options ?? (request as RequestWithSettings)[responseSettings] ?? {};\n\n if (!(error instanceof AuthioBearerError) && !(error instanceof AuthioForbiddenError)) return next(error);\n if (settings.passErrors) return next(error);\n\n sendAuthioError(response, error, settings);\n}\n\nfunction readRawBody(request: Request, maxBodySize: number): Promise<Uint8Array | undefined> {\n const parsed = (request as Request & { body?: unknown }).body;\n\n if (Buffer.isBuffer(parsed)) return Promise.resolve(parsed.byteLength > maxBodySize ? undefined : new Uint8Array(parsed));\n if (typeof parsed === \"string\") return Promise.resolve(new TextEncoder().encode(parsed));\n\n if (request.readableEnded || (parsed !== undefined && parsed !== null && typeof parsed === \"object\" && Object.keys(parsed).length > 0))\n return Promise.reject(\n new Error(\"The Authio webhook route needs the raw body: mount it before express.json(), or use express.raw({ type: '*/*' }) on it.\"),\n );\n\n const declared = Number(request.headers[\"content-length\"]);\n\n if (Number.isFinite(declared) && declared > maxBodySize) return Promise.resolve(undefined);\n\n return new Promise((resolve, reject) => {\n const chunks: Buffer[] = [];\n let size = 0;\n let done = false;\n\n request.on(\"data\", (chunk: Buffer) => {\n if (done) return;\n\n size += chunk.byteLength;\n\n if (size > maxBodySize) {\n done = true;\n request.pause();\n resolve(undefined);\n return;\n }\n\n chunks.push(chunk);\n });\n request.on(\"end\", () => {\n if (!done) resolve(new Uint8Array(Buffer.concat(chunks)));\n });\n request.on(\"error\", reject);\n });\n}\n\nasync function readLogoutToken(request: Request): Promise<string | undefined> {\n const parsed = (request as Request & { body?: unknown }).body;\n\n if (parsed && typeof parsed === \"object\" && !Buffer.isBuffer(parsed) && typeof (parsed as Record<string, unknown>).logout_token === \"string\")\n return (parsed as Record<string, string>).logout_token;\n\n const raw = Buffer.isBuffer(parsed)\n ? new Uint8Array(parsed)\n : typeof parsed === \"string\"\n ? new TextEncoder().encode(parsed)\n : request.readableEnded\n ? undefined\n : await readRawBody(request, 64 * 1024);\n\n return raw ? (new URLSearchParams(new TextDecoder().decode(raw)).get(\"logout_token\") ?? undefined) : undefined;\n}\n\nfunction requireResource(verifier: AuthioVerifier): string {\n if (!verifier.resource) throw new TypeError(\"authioProtectedResourceMetadata needs a verifier configured with resource.\");\n\n return verifier.resource;\n}\n\nfunction isVerifier(value: AuthioVerifier | AuthioVerifierOptions): value is AuthioVerifier {\n return typeof (value as AuthioVerifier).verify === \"function\";\n}\n\nexport type { AuthioMatch, AuthioRule, AuthioUser, AuthioVerifier, AuthioVerifierOptions } from \"@hyper.authio/server\";\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper.authio/express",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Express middleware for Authio: bearer validation, scope/role/permission/group/claim/organization/acr/amr/client guards, and signed webhook routes.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.cts",
|
|
19
|
+
"default": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"README.md"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=20.19"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@hyper.authio/client": "0.7.0",
|
|
36
|
+
"@hyper.authio/server": "0.7.0"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"authio",
|
|
40
|
+
"express",
|
|
41
|
+
"oauth",
|
|
42
|
+
"jwt",
|
|
43
|
+
"authorization",
|
|
44
|
+
"middleware"
|
|
45
|
+
],
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/LeonardoFerreira1209/authio-node.git",
|
|
49
|
+
"directory": "packages/express"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"express": ">=4.18 <6"
|
|
56
|
+
}
|
|
57
|
+
}
|