@hyper.authio/express 0.7.0 → 0.8.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 +17 -0
- package/dist/index.cjs +19 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -2
- package/dist/index.d.ts +23 -2
- package/dist/index.js +20 -2
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -85,6 +85,23 @@ Register the URL as the client's back-channel logout URI in Authio. The handler
|
|
|
85
85
|
`express.urlencoded()`), validates it with `client.validateLogoutToken`, answers `200`, `400` (`invalid_request`) for
|
|
86
86
|
a token that does not validate, and always sends `Cache-Control: no-store`.
|
|
87
87
|
|
|
88
|
+
## Front-channel logout
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
app.get(
|
|
92
|
+
"/auth/frontchannel-logout",
|
|
93
|
+
authioFrontchannelLogout({
|
|
94
|
+
client,
|
|
95
|
+
onLogout: async ({ sessionId }, request) => (sessionId ? sessions.destroyWhere({ sessionId }) : sessions.destroy(request)),
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Register the URL as the client's front-channel logout URI in Authio, with the session required so Authio sends `iss`
|
|
101
|
+
and `sid`. Authio loads it in an iframe when a session ends; the handler checks `iss` with
|
|
102
|
+
`client.validateFrontChannelLogout`, answers `200` with an empty page, `400` (`invalid_request`) for another issuer or a
|
|
103
|
+
`sid` without `iss`, and always sends `Cache-Control: no-store`.
|
|
104
|
+
|
|
88
105
|
## License
|
|
89
106
|
|
|
90
107
|
MIT
|
package/dist/index.cjs
CHANGED
|
@@ -101,6 +101,24 @@ function authioBackchannelLogout(options) {
|
|
|
101
101
|
}).catch(next);
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
|
+
function authioFrontchannelLogout(options) {
|
|
105
|
+
return (request, response, next) => {
|
|
106
|
+
response.set("Cache-Control", "no-store");
|
|
107
|
+
Promise.resolve().then(async () => {
|
|
108
|
+
let logout;
|
|
109
|
+
try {
|
|
110
|
+
logout = options.client.validateFrontChannelLogout(new URL(request.originalUrl ?? request.url, "http://localhost").searchParams);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
options.onError?.(error);
|
|
113
|
+
if (!(error instanceof client.AuthioCallbackError)) throw error;
|
|
114
|
+
response.status(400).json({ error: "invalid_request", error_description: error.message });
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
await options.onLogout(logout, request);
|
|
118
|
+
response.status(200).type("text/html; charset=utf-8").send('<!doctype html><html><head><meta charset="utf-8"><title>Authio</title></head><body></body></html>');
|
|
119
|
+
}).catch(next);
|
|
120
|
+
};
|
|
121
|
+
}
|
|
104
122
|
function sendAuthioError(response, error, options = {}) {
|
|
105
123
|
const challenge = error.wwwAuthenticate;
|
|
106
124
|
if (challenge) response.append("WWW-Authenticate", challenge);
|
|
@@ -164,6 +182,7 @@ exports.allOf = allOf;
|
|
|
164
182
|
exports.anyOf = anyOf;
|
|
165
183
|
exports.authioBackchannelLogout = authioBackchannelLogout;
|
|
166
184
|
exports.authioBearer = authioBearer;
|
|
185
|
+
exports.authioFrontchannelLogout = authioFrontchannelLogout;
|
|
167
186
|
exports.authioProtectedResourceMetadata = authioProtectedResourceMetadata;
|
|
168
187
|
exports.authioWebhooks = authioWebhooks;
|
|
169
188
|
exports.requireAuthenticationLevel = requireAuthenticationLevel;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +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","protectedResourceMetadataPath","AuthioTokenValidationError","toProblemDetails"],"mappings":";;;;;;AAiFA,IAAM,gBAAA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ValidationError,\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"]}
|
|
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","protectedResourceMetadataPath","AuthioTokenValidationError","AuthioCallbackError","toProblemDetails"],"mappings":";;;;;;AAmFA,IAAM,gBAAA,mBAAmB,MAAA,CAAO,GAAA,CAAI,yBAAyB,CAAA;AAUtD,SAAS,YAAA,CAAa,QAAA,EAAkD,OAAA,GAA+B,EAAC,EAAmB;AAChI,EAAA,MAAM,WAAW,UAAA,CAAW,QAAQ,CAAA,GAAI,QAAA,GAAWA,4BAAqB,QAAQ,CAAA;AAEhF,EAAA,OAAO,CAAC,OAAA,EAAS,QA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,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.cjs","sourcesContent":["import type { NextFunction, Request, RequestHandler, Response } from \"express\";\nimport {\n AuthioCallbackError,\n AuthioTokenValidationError,\n type AuthioCallerKind,\n type AuthioClient,\n type AuthioFrontChannelLogout,\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 * Options of `authioFrontchannelLogout`.\n */\nexport interface AuthioFrontchannelLogoutOptions {\n /** The client Authio ends the sessions of (its issuer is checked against `iss`). */\n client: AuthioClient;\n /**\n * Ends the sessions the request names: the Authio session `sessionId`, or the browser's own session (read from the\n * request's cookies) when Authio sent none. Session storage is the application's.\n */\n onLogout: (logout: AuthioFrontChannelLogout, request: Request) => void | Promise<void>;\n /** Called when a request is refused. */\n onError?: (error: unknown) => void;\n}\n\n/**\n * The front-channel logout endpoint (OpenID Connect Front-Channel Logout 1.0), the page Authio loads in an iframe when\n * a session ends: checks `iss` and `sid` with the client and calls `onLogout`. Answers `200` with an empty page, `400`\n * (`invalid_request`) for another issuer or a `sid` without `iss`, and `500` when `onLogout` throws; always\n * `Cache-Control: no-store`. Register its URL as the client's front-channel logout URI, with the session required.\n */\nexport function authioFrontchannelLogout(options: AuthioFrontchannelLogoutOptions): RequestHandler {\n return (request, response, next) => {\n response.set(\"Cache-Control\", \"no-store\");\n\n Promise.resolve()\n .then(async () => {\n let logout: AuthioFrontChannelLogout;\n\n try {\n logout = options.client.validateFrontChannelLogout(new URL(request.originalUrl ?? request.url, \"http://localhost\").searchParams);\n } catch (error) {\n options.onError?.(error);\n if (!(error instanceof AuthioCallbackError)) 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).type(\"text/html; charset=utf-8\").send('<!doctype html><html><head><meta charset=\"utf-8\"><title>Authio</title></head><body></body></html>');\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
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, RequestHandler, Response } from 'express';
|
|
2
|
-
import { AuthioClient, AuthioLogoutToken, AuthioWebhookDataByType, AuthioTypedWebhookEvent, AuthioCallerKind, AuthioResourceAccessSource } from '@hyper.authio/client';
|
|
2
|
+
import { AuthioClient, AuthioLogoutToken, AuthioFrontChannelLogout, AuthioWebhookDataByType, AuthioTypedWebhookEvent, AuthioCallerKind, AuthioResourceAccessSource } from '@hyper.authio/client';
|
|
3
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
4
|
export { AuthioMatch, AuthioRule, AuthioUser, AuthioVerifier, AuthioVerifierOptions } from '@hyper.authio/server';
|
|
5
5
|
|
|
@@ -131,9 +131,30 @@ interface AuthioBackchannelLogoutOptions {
|
|
|
131
131
|
* no-store`. Works with or without `express.urlencoded()` in front of it.
|
|
132
132
|
*/
|
|
133
133
|
declare function authioBackchannelLogout(options: AuthioBackchannelLogoutOptions): RequestHandler;
|
|
134
|
+
/**
|
|
135
|
+
* Options of `authioFrontchannelLogout`.
|
|
136
|
+
*/
|
|
137
|
+
interface AuthioFrontchannelLogoutOptions {
|
|
138
|
+
/** The client Authio ends the sessions of (its issuer is checked against `iss`). */
|
|
139
|
+
client: AuthioClient;
|
|
140
|
+
/**
|
|
141
|
+
* Ends the sessions the request names: the Authio session `sessionId`, or the browser's own session (read from the
|
|
142
|
+
* request's cookies) when Authio sent none. Session storage is the application's.
|
|
143
|
+
*/
|
|
144
|
+
onLogout: (logout: AuthioFrontChannelLogout, request: Request) => void | Promise<void>;
|
|
145
|
+
/** Called when a request is refused. */
|
|
146
|
+
onError?: (error: unknown) => void;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The front-channel logout endpoint (OpenID Connect Front-Channel Logout 1.0), the page Authio loads in an iframe when
|
|
150
|
+
* a session ends: checks `iss` and `sid` with the client and calls `onLogout`. Answers `200` with an empty page, `400`
|
|
151
|
+
* (`invalid_request`) for another issuer or a `sid` without `iss`, and `500` when `onLogout` throws; always
|
|
152
|
+
* `Cache-Control: no-store`. Register its URL as the client's front-channel logout URI, with the session required.
|
|
153
|
+
*/
|
|
154
|
+
declare function authioFrontchannelLogout(options: AuthioFrontchannelLogoutOptions): RequestHandler;
|
|
134
155
|
/**
|
|
135
156
|
* Answers a refusal: `WWW-Authenticate`, the status, and problem details when asked.
|
|
136
157
|
*/
|
|
137
158
|
declare function sendAuthioError(response: Response, error: AuthioBearerError | AuthioForbiddenError, options?: AuthioResponseOptions): void;
|
|
138
159
|
|
|
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 };
|
|
160
|
+
export { type AuthioBackchannelLogoutOptions, type AuthioBearerOptions, type AuthioFrontchannelLogoutOptions, type AuthioGuard, type AuthioProtectedResourceMetadataRouteOptions, type AuthioRequestContext, type AuthioResponseOptions, type AuthioWebhookRoute, allOf, anyOf, authioBackchannelLogout, authioBearer, authioFrontchannelLogout, authioProtectedResourceMetadata, authioWebhooks, requireAuthenticationLevel, requireAuthenticationMethod, requireAuthio, requireCallerKind, requireClaim, requireClient, requireGroup, requireOrganization, requirePermission, requireRecentAuthentication, requireRole, requireScope, sendAuthioError };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, RequestHandler, Response } from 'express';
|
|
2
|
-
import { AuthioClient, AuthioLogoutToken, AuthioWebhookDataByType, AuthioTypedWebhookEvent, AuthioCallerKind, AuthioResourceAccessSource } from '@hyper.authio/client';
|
|
2
|
+
import { AuthioClient, AuthioLogoutToken, AuthioFrontChannelLogout, AuthioWebhookDataByType, AuthioTypedWebhookEvent, AuthioCallerKind, AuthioResourceAccessSource } from '@hyper.authio/client';
|
|
3
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
4
|
export { AuthioMatch, AuthioRule, AuthioUser, AuthioVerifier, AuthioVerifierOptions } from '@hyper.authio/server';
|
|
5
5
|
|
|
@@ -131,9 +131,30 @@ interface AuthioBackchannelLogoutOptions {
|
|
|
131
131
|
* no-store`. Works with or without `express.urlencoded()` in front of it.
|
|
132
132
|
*/
|
|
133
133
|
declare function authioBackchannelLogout(options: AuthioBackchannelLogoutOptions): RequestHandler;
|
|
134
|
+
/**
|
|
135
|
+
* Options of `authioFrontchannelLogout`.
|
|
136
|
+
*/
|
|
137
|
+
interface AuthioFrontchannelLogoutOptions {
|
|
138
|
+
/** The client Authio ends the sessions of (its issuer is checked against `iss`). */
|
|
139
|
+
client: AuthioClient;
|
|
140
|
+
/**
|
|
141
|
+
* Ends the sessions the request names: the Authio session `sessionId`, or the browser's own session (read from the
|
|
142
|
+
* request's cookies) when Authio sent none. Session storage is the application's.
|
|
143
|
+
*/
|
|
144
|
+
onLogout: (logout: AuthioFrontChannelLogout, request: Request) => void | Promise<void>;
|
|
145
|
+
/** Called when a request is refused. */
|
|
146
|
+
onError?: (error: unknown) => void;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The front-channel logout endpoint (OpenID Connect Front-Channel Logout 1.0), the page Authio loads in an iframe when
|
|
150
|
+
* a session ends: checks `iss` and `sid` with the client and calls `onLogout`. Answers `200` with an empty page, `400`
|
|
151
|
+
* (`invalid_request`) for another issuer or a `sid` without `iss`, and `500` when `onLogout` throws; always
|
|
152
|
+
* `Cache-Control: no-store`. Register its URL as the client's front-channel logout URI, with the session required.
|
|
153
|
+
*/
|
|
154
|
+
declare function authioFrontchannelLogout(options: AuthioFrontchannelLogoutOptions): RequestHandler;
|
|
134
155
|
/**
|
|
135
156
|
* Answers a refusal: `WWW-Authenticate`, the status, and problem details when asked.
|
|
136
157
|
*/
|
|
137
158
|
declare function sendAuthioError(response: Response, error: AuthioBearerError | AuthioForbiddenError, options?: AuthioResponseOptions): void;
|
|
138
159
|
|
|
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 };
|
|
160
|
+
export { type AuthioBackchannelLogoutOptions, type AuthioBearerOptions, type AuthioFrontchannelLogoutOptions, type AuthioGuard, type AuthioProtectedResourceMetadataRouteOptions, type AuthioRequestContext, type AuthioResponseOptions, type AuthioWebhookRoute, allOf, anyOf, authioBackchannelLogout, authioBearer, authioFrontchannelLogout, authioProtectedResourceMetadata, authioWebhooks, requireAuthenticationLevel, requireAuthenticationMethod, requireAuthio, requireCallerKind, requireClaim, requireClient, requireGroup, requireOrganization, requirePermission, requireRecentAuthentication, requireRole, requireScope, sendAuthioError };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AuthioTokenValidationError } from '@hyper.authio/client';
|
|
1
|
+
import { AuthioTokenValidationError, AuthioCallbackError } from '@hyper.authio/client';
|
|
2
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
3
|
|
|
4
4
|
// src/index.ts
|
|
@@ -99,6 +99,24 @@ function authioBackchannelLogout(options) {
|
|
|
99
99
|
}).catch(next);
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
|
+
function authioFrontchannelLogout(options) {
|
|
103
|
+
return (request, response, next) => {
|
|
104
|
+
response.set("Cache-Control", "no-store");
|
|
105
|
+
Promise.resolve().then(async () => {
|
|
106
|
+
let logout;
|
|
107
|
+
try {
|
|
108
|
+
logout = options.client.validateFrontChannelLogout(new URL(request.originalUrl ?? request.url, "http://localhost").searchParams);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
options.onError?.(error);
|
|
111
|
+
if (!(error instanceof AuthioCallbackError)) throw error;
|
|
112
|
+
response.status(400).json({ error: "invalid_request", error_description: error.message });
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
await options.onLogout(logout, request);
|
|
116
|
+
response.status(200).type("text/html; charset=utf-8").send('<!doctype html><html><head><meta charset="utf-8"><title>Authio</title></head><body></body></html>');
|
|
117
|
+
}).catch(next);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
102
120
|
function sendAuthioError(response, error, options = {}) {
|
|
103
121
|
const challenge = error.wwwAuthenticate;
|
|
104
122
|
if (challenge) response.append("WWW-Authenticate", challenge);
|
|
@@ -158,6 +176,6 @@ function isVerifier(value) {
|
|
|
158
176
|
return typeof value.verify === "function";
|
|
159
177
|
}
|
|
160
178
|
|
|
161
|
-
export { allOf, anyOf, authioBackchannelLogout, authioBearer, authioProtectedResourceMetadata, authioWebhooks, requireAuthenticationLevel, requireAuthenticationMethod, requireAuthio, requireCallerKind, requireClaim, requireClient, requireGroup, requireOrganization, requirePermission, requireRecentAuthentication, requireRole, requireScope, sendAuthioError };
|
|
179
|
+
export { allOf, anyOf, authioBackchannelLogout, authioBearer, authioFrontchannelLogout, authioProtectedResourceMetadata, authioWebhooks, requireAuthenticationLevel, requireAuthenticationMethod, requireAuthio, requireCallerKind, requireClaim, requireClient, requireGroup, requireOrganization, requirePermission, requireRecentAuthentication, requireRole, requireScope, sendAuthioError };
|
|
162
180
|
//# sourceMappingURL=index.js.map
|
|
163
181
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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,MAAM,WAAW,UAAA,CAAW,QAAQ,CAAA,GAAI,QAAA,GAAW,qBAAqB,QAAQ,CAAA;AAEhF,EAAA,OAAO,CAAC,OAAA,EAAS,QAAA,EAAU,IAAA,KAAS;AAClC,IAAC,OAAA,CAAgC,gBAAgB,CAAA,GAAI,EAAE,gBAAgB,OAAA,CAAQ,cAAA,EAAgB,UAAA,EAAY,OAAA,CAAQ,UAAA,EAAW;AAE9H,IAAA,MAAM,MAAA,GAAS,QAAQ,OAAA,CAAQ,aAAA;AAE/B,IAAA,IAAI,OAAA,CAAQ,QAAA,IAAY,CAAC,MAAA,EAAQ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AA,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"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["scopeRule","allOfRule","roleRule","permissionRule","groupRule","claimRule","organizationRule","authenticationLevelRule","authenticationMethodRule","recentAuthenticationRule","callerKindRule","clientRule","anyOfRule"],"mappings":";;;;AAmFA,IAAM,gBAAA,mBAAmB,MAAA,CAAO,GAAA,CAAI,yBAAyB,CAAA;AAUtD,SAAS,YAAA,CAAa,QAAA,EAAkD,OAAA,GAA+B,EAAC,EAAmB;AAChI,EAAA,MAAM,WAAW,UAAA,CAAW,QAAQ,CAAA,GAAI,QAAA,GAAW,qBAAqB,QAAQ,CAAA;AAEhF,EAAA,OAAO,CAAC,OAAA,EAAS,QAAA,EAAU,IAAA,KAAS;AAClC,IAAC,OAAA,CAAgC,gBAAgB,CAAA,GAAI,EAAE,gBAAgB,OAAA,CAAQ,cAAA,EAAgB,UAAA,EAAY,OAAA,CAAQ,UAAA,EAAW;AAE9H,IAAA,MAAM,MAAA,GAAS,QAAQ,OAAA,CAAQ,aAAA;AAE/B,IAAA,IAAI,OAAA,CAAQ,QAAA,IAAY,CAAC,MAAA,EAAQ;AAC/B,MAAA,IAAA,EAAK;AACL,MAAA;AAAA,IACF;AAEA,IAAA,QAAA,CACG,yBAAA,CAA0B,MAAM,CAAA,CAChC,IAAA,CAAK,CAAC,IAAA,KAAS;AACd,MAAA,OAAA,CAAQ,MAAA,GAAS,EAAE,IAAA,EAAM,KAAA,EAAO,KAAK,KAAA,EAAO;AAE5C,MAAA,MAAM,OAAA,GAAU,QAAA,CAAS,cAAA,CAAe,MAAA,GAAS,CAAA,GAAIA,cAAA,CAAU,QAAA,CAAS,cAAA,EAAgB,KAAK,CAAA,CAAE,KAAA,CAAM,IAAI,CAAA,GAAI,MAAA;AAE7G,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} from \"express\";\nimport {\n AuthioCallbackError,\n AuthioTokenValidationError,\n type AuthioCallerKind,\n type AuthioClient,\n type AuthioFrontChannelLogout,\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 * Options of `authioFrontchannelLogout`.\n */\nexport interface AuthioFrontchannelLogoutOptions {\n /** The client Authio ends the sessions of (its issuer is checked against `iss`). */\n client: AuthioClient;\n /**\n * Ends the sessions the request names: the Authio session `sessionId`, or the browser's own session (read from the\n * request's cookies) when Authio sent none. Session storage is the application's.\n */\n onLogout: (logout: AuthioFrontChannelLogout, request: Request) => void | Promise<void>;\n /** Called when a request is refused. */\n onError?: (error: unknown) => void;\n}\n\n/**\n * The front-channel logout endpoint (OpenID Connect Front-Channel Logout 1.0), the page Authio loads in an iframe when\n * a session ends: checks `iss` and `sid` with the client and calls `onLogout`. Answers `200` with an empty page, `400`\n * (`invalid_request`) for another issuer or a `sid` without `iss`, and `500` when `onLogout` throws; always\n * `Cache-Control: no-store`. Register its URL as the client's front-channel logout URI, with the session required.\n */\nexport function authioFrontchannelLogout(options: AuthioFrontchannelLogoutOptions): RequestHandler {\n return (request, response, next) => {\n response.set(\"Cache-Control\", \"no-store\");\n\n Promise.resolve()\n .then(async () => {\n let logout: AuthioFrontChannelLogout;\n\n try {\n logout = options.client.validateFrontChannelLogout(new URL(request.originalUrl ?? request.url, \"http://localhost\").searchParams);\n } catch (error) {\n options.onError?.(error);\n if (!(error instanceof AuthioCallbackError)) 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).type(\"text/html; charset=utf-8\").send('<!doctype html><html><head><meta charset=\"utf-8\"><title>Authio</title></head><body></body></html>');\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,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper.authio/express",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Express middleware for Authio: bearer validation, scope/role/permission/group/claim/organization/acr/amr/client guards, and signed webhook routes.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"build": "tsup"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@hyper.authio/client": "0.
|
|
36
|
-
"@hyper.authio/server": "0.
|
|
35
|
+
"@hyper.authio/client": "0.8.0",
|
|
36
|
+
"@hyper.authio/server": "0.8.0"
|
|
37
37
|
},
|
|
38
38
|
"keywords": [
|
|
39
39
|
"authio",
|