@schemavaults/auth-server-sdk 0.30.0 → 0.31.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 +48 -0
- package/dist/cli.cjs +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/openapi-operations/create-schemavaults-auth-resolvers.d.ts +106 -0
- package/dist/openapi-operations/create-schemavaults-auth-resolvers.js +274 -0
- package/dist/openapi-operations/create-schemavaults-auth-resolvers.js.map +1 -0
- package/dist/openapi-operations/index.d.ts +3 -0
- package/dist/openapi-operations/index.js +3 -0
- package/dist/openapi-operations/index.js.map +1 -0
- package/dist/openapi-operations/read-cookie.d.ts +13 -0
- package/dist/openapi-operations/read-cookie.js +33 -0
- package/dist/openapi-operations/read-cookie.js.map +1 -0
- package/package.json +16 -2
package/README.md
CHANGED
|
@@ -47,3 +47,51 @@ Keysets are still looked up by the API server id; only the `aud` comparison
|
|
|
47
47
|
changes, and only for tokens whose header names one of the listed URLs
|
|
48
48
|
(`resolveExpectedTokenAudience()`). Tokens with any other `aud` fail as
|
|
49
49
|
before.
|
|
50
|
+
|
|
51
|
+
## Auth resolvers for `@schemavaults/openapi-operations`
|
|
52
|
+
|
|
53
|
+
Resource servers that declare their API with
|
|
54
|
+
[`@schemavaults/openapi-operations`](../openapi-operations) need credential
|
|
55
|
+
resolvers for the SchemaVaults access-token schemes
|
|
56
|
+
(`schemaVaultsAccessTokenBearerScheme`, `schemaVaultsAccessTokenCookieScheme(...)`).
|
|
57
|
+
`createSchemaVaultsAuthResolvers()` returns them, keyed by scheme name, built on the
|
|
58
|
+
same `RouteGuardFactory` + remote JWKS verification `withAuthenticatedApiRouteGuard`
|
|
59
|
+
uses. `@schemavaults/openapi-operations` is an optional peer dependency: install it
|
|
60
|
+
alongside this package to use the sub-export.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
// src/lib/api/app.ts
|
|
64
|
+
import { createSchemaVaultsAuthResolvers } from "@schemavaults/auth-server-sdk/openapi-operations";
|
|
65
|
+
import { createOperationsAppFactory } from "@schemavaults/openapi-operations";
|
|
66
|
+
|
|
67
|
+
export const api = createOperationsAppFactory<ApiContext, UserData>({
|
|
68
|
+
operations,
|
|
69
|
+
authResolvers: createSchemaVaultsAuthResolvers<ApiContext>({
|
|
70
|
+
// all optional; environment-derived defaults are read on first request
|
|
71
|
+
apiServerId, // default SCHEMAVAULTS_API_SERVER_ID
|
|
72
|
+
environment, // default getAppEnvironment()
|
|
73
|
+
acceptedAudiences: ["https://mcp.example.com/mcp"], // RFC 8707 resource URLs (see above)
|
|
74
|
+
isTokenRevoked, // optional revocation hook
|
|
75
|
+
}),
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Per request:
|
|
80
|
+
|
|
81
|
+
- `Authorization: Bearer <token>` (`schemavaults-access-token`): a malformed header is
|
|
82
|
+
401 `invalid_request` with `WWW-Authenticate`; no header → the next scheme is tried.
|
|
83
|
+
- The access-token cookie `access_token_<api_server_id>`
|
|
84
|
+
(`schemavaults-access-token-cookie`): accepts the JSON `{ token, exp, ... }` value the
|
|
85
|
+
auth provider writes (skipped once `exp` has passed) and a raw JWT.
|
|
86
|
+
- A token that fails verification is 401 `invalid_token`; a revoked one (`isTokenRevoked`)
|
|
87
|
+
401 `token_revoked`; a disabled account 403 `account_disabled`.
|
|
88
|
+
- A missing `SCHEMAVAULTS_AUTH_JWKS_ACCESS_PRIVATE_KEY` (or API server id / auth server
|
|
89
|
+
URL) is 500 `auth_not_configured` with a message naming it, never a silent pass. With no
|
|
90
|
+
credential at all the operations runtime still answers 401 + `WWW-Authenticate`.
|
|
91
|
+
- The principal is `{ scheme, user, isAdmin: user.admin === true, scope,
|
|
92
|
+
getOrganizationRole }`; `getOrganizationRole` validates the id and asks the auth server
|
|
93
|
+
(`isUserInOrganization`) with the JWKS access key.
|
|
94
|
+
|
|
95
|
+
`AccessTokenCookieName(apiServerId)` / `AccessTokenExpiryCookieName(apiServerId)` are
|
|
96
|
+
exported from the package index too, so the cookie scheme can be declared without a
|
|
97
|
+
direct dependency on `@schemavaults/auth-common`.
|
package/dist/cli.cjs
CHANGED
|
@@ -89,7 +89,7 @@ var init_resolve_codegen_templates_directory = __esm({
|
|
|
89
89
|
|
|
90
90
|
// src/NextjsAppDirectoryPlugin/codegen-marker.ts
|
|
91
91
|
function getCodegenMarkerComment() {
|
|
92
|
-
const version = true ? "0.
|
|
92
|
+
const version = true ? "0.31.0" : "unknown";
|
|
93
93
|
return `${CODEGEN_MARKER_PREFIX}${version}`;
|
|
94
94
|
}
|
|
95
95
|
function hasCodegenMarker(firstLine) {
|
|
@@ -331,7 +331,7 @@ async function main() {
|
|
|
331
331
|
return;
|
|
332
332
|
}
|
|
333
333
|
if (args.includes("--version") || args.includes("-v")) {
|
|
334
|
-
console.log(`${PACKAGE_NAME}@${"0.
|
|
334
|
+
console.log(`${PACKAGE_NAME}@${"0.31.0"}`);
|
|
335
335
|
return;
|
|
336
336
|
}
|
|
337
337
|
const command = args.find((arg) => !arg.startsWith("-")) ?? "codegen";
|
package/dist/index.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export { getSchemavaultsClientApplicationId, getSchemavaultsClientApplicationId
|
|
|
19
19
|
export type { AppId } from "@schemavaults/app-definitions";
|
|
20
20
|
export { appIdSchema } from "@schemavaults/app-definitions";
|
|
21
21
|
export { RefreshTokenCookieName, RefreshTokenExpiryCookieName, } from "./RefreshTokenCookieNames";
|
|
22
|
+
export { AccessTokenCookieName, AccessTokenExpiryCookieName, } from "./AccessTokenCookieNames";
|
|
22
23
|
export { default as getStringByteSize } from "./getStringByteSize";
|
|
23
24
|
export { redirectToLogin } from "./redirect-to-login";
|
|
24
25
|
export type * from "./redirect-with-error";
|
package/dist/index.js
CHANGED
|
@@ -14,6 +14,7 @@ export { getSchemavaultsClientApplicationId, getSchemavaultsClientApplicationId
|
|
|
14
14
|
export { appIdSchema } from "@schemavaults/app-definitions";
|
|
15
15
|
// Cookie Names
|
|
16
16
|
export { RefreshTokenCookieName, RefreshTokenExpiryCookieName, } from "./RefreshTokenCookieNames";
|
|
17
|
+
export { AccessTokenCookieName, AccessTokenExpiryCookieName, } from "./AccessTokenCookieNames";
|
|
17
18
|
export { default as getStringByteSize } from "./getStringByteSize";
|
|
18
19
|
export { redirectToLogin } from "./redirect-to-login";
|
|
19
20
|
// App Environment
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAG7B,cAAc,gBAAgB,CAAC;AAG/B,cAAc,iBAAiB,CAAC;AAGhC,cAAc,yBAAyB,CAAC;AAGxC,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAG1D,OAAO,EACL,qBAAqB,EACrB,cAAc,GACf,MAAM,qCAAqC,CAAC;AAG7C,OAAO,wBAAwB,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,wBAAwB,EAAE,CAAC;AAEpC,iBAAiB;AACjB,OAAO,EACL,0BAA0B,EAC1B,0BAA0B,IAAI,0BAA0B,EACzD,MAAM,sCAAsC,CAAC;AAE9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAElE,yBAAyB;AACzB,OAAO,EACL,kCAAkC,EAClC,kCAAkC,IAAI,kCAAkC,EACzE,MAAM,8CAA8C,CAAC;AAEtD,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAE5D,eAAe;AACf,OAAO,EACL,sBAAsB,EACtB,4BAA4B,GAC7B,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAG7B,cAAc,gBAAgB,CAAC;AAG/B,cAAc,iBAAiB,CAAC;AAGhC,cAAc,y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5B,qBAAqB,EACrB,gCAAgC,EAChC,yCAAyC,EACzC,qCAAqC,EACrC,+BAA+B,EAC/B,oCAAoC,GACrC,MAAM,yCAAyC,CAAC;AAmBjD,8BAA8B;AAC9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAEjE,kDAAkD;AAClD,OAAO,EAAE,OAAO,IAAI,4BAA4B,EAAE,MAAM,wCAAwC,CAAC;AAEjG,OAAO,EAAE,wBAAwB,EAAE,MAAM,gCAAgC,CAAC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { type ApiServerId, type SchemaVaultsAppEnvironment } from "@schemavaults/app-definitions";
|
|
2
|
+
import type { UserData } from "@schemavaults/auth-common";
|
|
3
|
+
import { type AuthResolvers } from "@schemavaults/openapi-operations";
|
|
4
|
+
import type { IJwtKeyManager } from "../JwtKeyManager";
|
|
5
|
+
import type { IsTokenRevokedFn } from "../route_guards/token-revocation";
|
|
6
|
+
/** Scheme names the resolvers are registered under. */
|
|
7
|
+
export declare const SCHEMAVAULTS_ACCESS_TOKEN_BEARER_SCHEME_NAME: "schemavaults-access-token";
|
|
8
|
+
export declare const SCHEMAVAULTS_ACCESS_TOKEN_COOKIE_SCHEME_NAME: "schemavaults-access-token-cookie";
|
|
9
|
+
/** Error codes of the responses the resolvers produce. */
|
|
10
|
+
export declare const SCHEMAVAULTS_AUTH_RESOLVER_ERROR_CODES: {
|
|
11
|
+
readonly invalidRequest: "invalid_request";
|
|
12
|
+
readonly invalidToken: "invalid_token";
|
|
13
|
+
readonly tokenRevoked: "token_revoked";
|
|
14
|
+
readonly accountDisabled: "account_disabled";
|
|
15
|
+
readonly notConfigured: "auth_not_configured";
|
|
16
|
+
};
|
|
17
|
+
export interface CreateSchemaVaultsAuthResolversOptions {
|
|
18
|
+
/**
|
|
19
|
+
* The API server id access tokens must be issued for (their `aud`), and
|
|
20
|
+
* whose access-token cookie (`AccessTokenCookieName(apiServerId)`) is
|
|
21
|
+
* read. Default: `SCHEMAVAULTS_API_SERVER_ID` from the environment, read
|
|
22
|
+
* on first use.
|
|
23
|
+
*/
|
|
24
|
+
readonly apiServerId?: ApiServerId;
|
|
25
|
+
/** Default: `getAppEnvironment()`, read on first use. */
|
|
26
|
+
readonly environment?: SchemaVaultsAppEnvironment;
|
|
27
|
+
/**
|
|
28
|
+
* RFC 8707 resource URL(s) this resource server is known by; access
|
|
29
|
+
* tokens whose `aud` is one of them are accepted too. See
|
|
30
|
+
* `RouteGuardFactory`'s `accepted_audiences`.
|
|
31
|
+
*/
|
|
32
|
+
readonly acceptedAudiences?: readonly string[];
|
|
33
|
+
/**
|
|
34
|
+
* Revocation check run against every token that verified. A revoked
|
|
35
|
+
* token is refused with 401 (`token_revoked`); a hook that throws fails
|
|
36
|
+
* closed. Omit for the claims-only fast path.
|
|
37
|
+
*/
|
|
38
|
+
readonly isTokenRevoked?: IsTokenRevokedFn;
|
|
39
|
+
/**
|
|
40
|
+
* JWT key manager used to verify tokens. Default: a `RemoteJwtKeyManager`
|
|
41
|
+
* fetching the auth server's JWKS with the
|
|
42
|
+
* `SCHEMAVAULTS_AUTH_JWKS_ACCESS_PRIVATE_KEY` from the environment.
|
|
43
|
+
*/
|
|
44
|
+
readonly jwtKeysManager?: IJwtKeyManager;
|
|
45
|
+
/**
|
|
46
|
+
* Base URL of the auth server organization membership is looked up on.
|
|
47
|
+
* Default: `getSchemaVaultsAuthServerUrl()` for the app environment.
|
|
48
|
+
*/
|
|
49
|
+
readonly authServerUrl?: string;
|
|
50
|
+
/** Verbose `console.log` diagnostics (rejected tokens, guard details). Default false. */
|
|
51
|
+
readonly debug?: boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The JWT inside the access-token cookie: the JSON `{ token, exp, ... }`
|
|
55
|
+
* value the auth provider writes (skipped once `exp`, unix milliseconds,
|
|
56
|
+
* has passed), or a raw JWT. Null when the cookie carries no usable token.
|
|
57
|
+
*/
|
|
58
|
+
export declare function accessTokenFromCookieValue(value: string): string | null;
|
|
59
|
+
/**
|
|
60
|
+
* Parses `Authorization: Bearer <token>`. `absent` when there is no header,
|
|
61
|
+
* `malformed` when it is not a bearer credential with a single token.
|
|
62
|
+
*/
|
|
63
|
+
export declare function bearerTokenFromAuthorizationHeader(header: string | undefined): {
|
|
64
|
+
kind: "absent";
|
|
65
|
+
} | {
|
|
66
|
+
kind: "malformed";
|
|
67
|
+
} | {
|
|
68
|
+
kind: "token";
|
|
69
|
+
token: string;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Credential resolvers for the SchemaVaults access-token schemes of
|
|
73
|
+
* `@schemavaults/openapi-operations`, for resource servers: verifies
|
|
74
|
+
* `Authorization: Bearer <access token>` (`schemavaults-access-token`) and
|
|
75
|
+
* the first-party access-token cookie `access_token_<api_server_id>`
|
|
76
|
+
* (`schemavaults-access-token-cookie`) against the auth server's JWKS
|
|
77
|
+
* through `RouteGuardFactory`, exactly like `withAuthenticatedApiRouteGuard`
|
|
78
|
+
* does for classic route handlers.
|
|
79
|
+
*
|
|
80
|
+
* Per request each resolver:
|
|
81
|
+
* - returns `null` when its credential is absent (the runtime tries the
|
|
82
|
+
* next accepted scheme, then answers 401 + `WWW-Authenticate`);
|
|
83
|
+
* - answers 401 (`invalid_request`) for a malformed Authorization header;
|
|
84
|
+
* - answers 401 (`invalid_token` / `token_revoked`) for a credential that
|
|
85
|
+
* does not verify, has expired, or was revoked (`isTokenRevoked`);
|
|
86
|
+
* - answers 403 (`account_disabled`) for a disabled account;
|
|
87
|
+
* - answers 500 (`auth_not_configured`) with a clear message when
|
|
88
|
+
* `SCHEMAVAULTS_AUTH_JWKS_ACCESS_PRIVATE_KEY` (or the API server id /
|
|
89
|
+
* auth server URL) is missing: a token is never waved through silently;
|
|
90
|
+
* - otherwise resolves `{ scheme, user, isAdmin: user.admin === true,
|
|
91
|
+
* scope, getOrganizationRole }`, where `getOrganizationRole` asks the
|
|
92
|
+
* auth server (`isUserInOrganization`) with the JWKS access key.
|
|
93
|
+
*
|
|
94
|
+
* Environment-derived settings are read on first use, not when the
|
|
95
|
+
* resolvers are created, so importing the module that builds them (e.g.
|
|
96
|
+
* from an OpenAPI generation script) needs no environment.
|
|
97
|
+
*
|
|
98
|
+
* ```ts
|
|
99
|
+
* export const api = createOperationsAppFactory<Ctx, UserData>({
|
|
100
|
+
* operations,
|
|
101
|
+
* authResolvers: createSchemaVaultsAuthResolvers<Ctx>(),
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export declare function createSchemaVaultsAuthResolvers<TContext = unknown>(options?: CreateSchemaVaultsAuthResolversOptions): AuthResolvers<UserData, TContext>;
|
|
106
|
+
export default createSchemaVaultsAuthResolvers;
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { apiServerIdSchema, getAppEnvironment, } from "@schemavaults/app-definitions";
|
|
2
|
+
import { organizationIdSchema, } from "@schemavaults/auth-common/organizations";
|
|
3
|
+
import { OperationError, schemaVaultsAccessTokenBearerScheme, } from "@schemavaults/openapi-operations";
|
|
4
|
+
import { AccessTokenCookieName } from "../AccessTokenCookieNames";
|
|
5
|
+
import getSchemavaultsApiServerId from "../env/get-schemavaults-api-server-id";
|
|
6
|
+
import getSchemaVaultsAuthServerUrl from "../env/get-schemavaults-auth-server-url";
|
|
7
|
+
import loadJwksAccessPrivateKey, { JWKS_ACCESS_PRIVATE_KEY_ENV_VAR_NAME, } from "../env/loadJwksAccessPrivateKey";
|
|
8
|
+
import isUserInOrganization from "../isUserInOrganization";
|
|
9
|
+
import RouteGuardFactory from "../route_guards/route-guard-factory";
|
|
10
|
+
import { readCookie } from "./read-cookie";
|
|
11
|
+
/** Scheme names the resolvers are registered under. */
|
|
12
|
+
export const SCHEMAVAULTS_ACCESS_TOKEN_BEARER_SCHEME_NAME = schemaVaultsAccessTokenBearerScheme.name;
|
|
13
|
+
export const SCHEMAVAULTS_ACCESS_TOKEN_COOKIE_SCHEME_NAME = "schemavaults-access-token-cookie";
|
|
14
|
+
/** Error codes of the responses the resolvers produce. */
|
|
15
|
+
export const SCHEMAVAULTS_AUTH_RESOLVER_ERROR_CODES = {
|
|
16
|
+
invalidRequest: "invalid_request",
|
|
17
|
+
invalidToken: "invalid_token",
|
|
18
|
+
tokenRevoked: "token_revoked",
|
|
19
|
+
accountDisabled: "account_disabled",
|
|
20
|
+
notConfigured: "auth_not_configured",
|
|
21
|
+
};
|
|
22
|
+
function challenge(scheme, error) {
|
|
23
|
+
const base = scheme.challenge?.startsWith("Bearer")
|
|
24
|
+
? scheme.challenge
|
|
25
|
+
: schemaVaultsAccessTokenBearerScheme.challenge ?? "Bearer";
|
|
26
|
+
return `${base}, error="${error}"`;
|
|
27
|
+
}
|
|
28
|
+
function unauthorized(scheme, error, message, challengeError = "invalid_token") {
|
|
29
|
+
return new OperationError(401, { error, message }, { "WWW-Authenticate": challenge(scheme, challengeError) });
|
|
30
|
+
}
|
|
31
|
+
function notConfigured(detail, cause) {
|
|
32
|
+
console.error(`[createSchemaVaultsAuthResolvers] Access tokens cannot be verified: ${detail}`, ...(cause !== undefined ? [cause] : []));
|
|
33
|
+
return new OperationError(500, {
|
|
34
|
+
error: SCHEMAVAULTS_AUTH_RESOLVER_ERROR_CODES.notConfigured,
|
|
35
|
+
message: `Authentication is not configured on this server: ${detail}`,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The JWT inside the access-token cookie: the JSON `{ token, exp, ... }`
|
|
40
|
+
* value the auth provider writes (skipped once `exp`, unix milliseconds,
|
|
41
|
+
* has passed), or a raw JWT. Null when the cookie carries no usable token.
|
|
42
|
+
*/
|
|
43
|
+
export function accessTokenFromCookieValue(value) {
|
|
44
|
+
const trimmed = value.trim();
|
|
45
|
+
if (trimmed.length === 0)
|
|
46
|
+
return null;
|
|
47
|
+
if (trimmed.startsWith("{")) {
|
|
48
|
+
let parsed;
|
|
49
|
+
try {
|
|
50
|
+
parsed = JSON.parse(trimmed);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
if (typeof parsed !== "object" || parsed === null)
|
|
56
|
+
return null;
|
|
57
|
+
const { token, exp } = parsed;
|
|
58
|
+
if (typeof token !== "string" || token.length === 0)
|
|
59
|
+
return null;
|
|
60
|
+
if (typeof exp === "number" && Date.now() >= exp)
|
|
61
|
+
return null;
|
|
62
|
+
return token;
|
|
63
|
+
}
|
|
64
|
+
return trimmed;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Parses `Authorization: Bearer <token>`. `absent` when there is no header,
|
|
68
|
+
* `malformed` when it is not a bearer credential with a single token.
|
|
69
|
+
*/
|
|
70
|
+
export function bearerTokenFromAuthorizationHeader(header) {
|
|
71
|
+
if (typeof header !== "string" || header.trim().length === 0)
|
|
72
|
+
return { kind: "absent" };
|
|
73
|
+
const parts = header.trim().split(/\s+/);
|
|
74
|
+
if (parts.length !== 2 || parts[0]?.toLowerCase() !== "bearer" || !parts[1]) {
|
|
75
|
+
return { kind: "malformed" };
|
|
76
|
+
}
|
|
77
|
+
return { kind: "token", token: parts[1] };
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Credential resolvers for the SchemaVaults access-token schemes of
|
|
81
|
+
* `@schemavaults/openapi-operations`, for resource servers: verifies
|
|
82
|
+
* `Authorization: Bearer <access token>` (`schemavaults-access-token`) and
|
|
83
|
+
* the first-party access-token cookie `access_token_<api_server_id>`
|
|
84
|
+
* (`schemavaults-access-token-cookie`) against the auth server's JWKS
|
|
85
|
+
* through `RouteGuardFactory`, exactly like `withAuthenticatedApiRouteGuard`
|
|
86
|
+
* does for classic route handlers.
|
|
87
|
+
*
|
|
88
|
+
* Per request each resolver:
|
|
89
|
+
* - returns `null` when its credential is absent (the runtime tries the
|
|
90
|
+
* next accepted scheme, then answers 401 + `WWW-Authenticate`);
|
|
91
|
+
* - answers 401 (`invalid_request`) for a malformed Authorization header;
|
|
92
|
+
* - answers 401 (`invalid_token` / `token_revoked`) for a credential that
|
|
93
|
+
* does not verify, has expired, or was revoked (`isTokenRevoked`);
|
|
94
|
+
* - answers 403 (`account_disabled`) for a disabled account;
|
|
95
|
+
* - answers 500 (`auth_not_configured`) with a clear message when
|
|
96
|
+
* `SCHEMAVAULTS_AUTH_JWKS_ACCESS_PRIVATE_KEY` (or the API server id /
|
|
97
|
+
* auth server URL) is missing: a token is never waved through silently;
|
|
98
|
+
* - otherwise resolves `{ scheme, user, isAdmin: user.admin === true,
|
|
99
|
+
* scope, getOrganizationRole }`, where `getOrganizationRole` asks the
|
|
100
|
+
* auth server (`isUserInOrganization`) with the JWKS access key.
|
|
101
|
+
*
|
|
102
|
+
* Environment-derived settings are read on first use, not when the
|
|
103
|
+
* resolvers are created, so importing the module that builds them (e.g.
|
|
104
|
+
* from an OpenAPI generation script) needs no environment.
|
|
105
|
+
*
|
|
106
|
+
* ```ts
|
|
107
|
+
* export const api = createOperationsAppFactory<Ctx, UserData>({
|
|
108
|
+
* operations,
|
|
109
|
+
* authResolvers: createSchemaVaultsAuthResolvers<Ctx>(),
|
|
110
|
+
* });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export function createSchemaVaultsAuthResolvers(options = {}) {
|
|
114
|
+
const debug = options.debug ?? false;
|
|
115
|
+
let apiServerId = options.apiServerId;
|
|
116
|
+
const resolveApiServerId = () => {
|
|
117
|
+
if (apiServerId === undefined) {
|
|
118
|
+
try {
|
|
119
|
+
apiServerId = getSchemavaultsApiServerId();
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
throw notConfigured("the API server id could not be determined (SCHEMAVAULTS_API_SERVER_ID)", e);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (!apiServerIdSchema.safeParse(apiServerId).success) {
|
|
126
|
+
throw notConfigured(`'${String(apiServerId)}' is not a valid API server id`);
|
|
127
|
+
}
|
|
128
|
+
return apiServerId;
|
|
129
|
+
};
|
|
130
|
+
let environment = options.environment;
|
|
131
|
+
const resolveEnvironment = () => {
|
|
132
|
+
if (environment === undefined) {
|
|
133
|
+
try {
|
|
134
|
+
environment = getAppEnvironment();
|
|
135
|
+
}
|
|
136
|
+
catch (e) {
|
|
137
|
+
throw notConfigured("the app environment could not be determined", e);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return environment;
|
|
141
|
+
};
|
|
142
|
+
let authServerUrl = options.authServerUrl;
|
|
143
|
+
const resolveAuthServerUrl = () => {
|
|
144
|
+
if (authServerUrl === undefined) {
|
|
145
|
+
try {
|
|
146
|
+
authServerUrl = getSchemaVaultsAuthServerUrl();
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
throw notConfigured("the auth server URL could not be determined", e);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return authServerUrl;
|
|
153
|
+
};
|
|
154
|
+
let factory;
|
|
155
|
+
let keysManager = options.jwtKeysManager;
|
|
156
|
+
const resolveFactory = () => {
|
|
157
|
+
if (factory === undefined) {
|
|
158
|
+
const env = resolveEnvironment();
|
|
159
|
+
try {
|
|
160
|
+
factory = new RouteGuardFactory({
|
|
161
|
+
environment: env,
|
|
162
|
+
jwt_keys_manager: keysManager,
|
|
163
|
+
is_token_revoked: options.isTokenRevoked,
|
|
164
|
+
accepted_audiences: options.acceptedAudiences,
|
|
165
|
+
debug,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
catch (e) {
|
|
169
|
+
throw notConfigured("the JWT key manager could not be created", e);
|
|
170
|
+
}
|
|
171
|
+
if (keysManager === undefined) {
|
|
172
|
+
// The factory built the default RemoteJwtKeyManager; mirror its
|
|
173
|
+
// configuration check so a missing JWKS access key is a 500 up
|
|
174
|
+
// front instead of a failed key fetch reported as a bad token.
|
|
175
|
+
keysManager = {
|
|
176
|
+
loadJwks: () => Promise.reject(new Error("unused")),
|
|
177
|
+
isConfigured: () => typeof process.env[JWKS_ACCESS_PRIVATE_KEY_ENV_VAR_NAME] === "string" &&
|
|
178
|
+
process.env[JWKS_ACCESS_PRIVATE_KEY_ENV_VAR_NAME].length > 0,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (!keysManager?.isConfigured()) {
|
|
183
|
+
throw notConfigured(options.jwtKeysManager
|
|
184
|
+
? "the JWT key manager reports it is not configured"
|
|
185
|
+
: `the '${JWKS_ACCESS_PRIVATE_KEY_ENV_VAR_NAME}' environment variable is not set`);
|
|
186
|
+
}
|
|
187
|
+
return factory;
|
|
188
|
+
};
|
|
189
|
+
let jwksAccessPrivateKey;
|
|
190
|
+
const resolveJwksAccessPrivateKey = () => {
|
|
191
|
+
jwksAccessPrivateKey ??= loadJwksAccessPrivateKey().catch((e) => {
|
|
192
|
+
jwksAccessPrivateKey = undefined;
|
|
193
|
+
throw e;
|
|
194
|
+
});
|
|
195
|
+
return jwksAccessPrivateKey;
|
|
196
|
+
};
|
|
197
|
+
async function principalFromTokenSource(scheme, source) {
|
|
198
|
+
const guardFactory = resolveFactory();
|
|
199
|
+
const audience = resolveApiServerId();
|
|
200
|
+
let guard;
|
|
201
|
+
try {
|
|
202
|
+
guard = await guardFactory.createGuardFromTokenSources("authenticated", [source], audience);
|
|
203
|
+
}
|
|
204
|
+
catch (e) {
|
|
205
|
+
if (debug) {
|
|
206
|
+
console.warn(`[createSchemaVaultsAuthResolvers] ${source.sourceHint ?? source.type} did not verify:`, e);
|
|
207
|
+
}
|
|
208
|
+
throw unauthorized(scheme, SCHEMAVAULTS_AUTH_RESOLVER_ERROR_CODES.invalidToken, "The access token is invalid or has expired");
|
|
209
|
+
}
|
|
210
|
+
if (guard.revoked === true) {
|
|
211
|
+
throw unauthorized(scheme, SCHEMAVAULTS_AUTH_RESOLVER_ERROR_CODES.tokenRevoked, "The access token has been revoked");
|
|
212
|
+
}
|
|
213
|
+
const user = guard.user;
|
|
214
|
+
if (!user) {
|
|
215
|
+
throw unauthorized(scheme, SCHEMAVAULTS_AUTH_RESOLVER_ERROR_CODES.invalidToken, "The access token is invalid or has expired");
|
|
216
|
+
}
|
|
217
|
+
if (user.disabled) {
|
|
218
|
+
throw new OperationError(403, {
|
|
219
|
+
error: SCHEMAVAULTS_AUTH_RESOLVER_ERROR_CODES.accountDisabled,
|
|
220
|
+
message: "Your account is disabled",
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
scheme: scheme.name,
|
|
225
|
+
user,
|
|
226
|
+
isAdmin: user.admin === true,
|
|
227
|
+
scope: guard.scope,
|
|
228
|
+
getOrganizationRole: async (organizationId) => {
|
|
229
|
+
const parsed = organizationIdSchema.safeParse(organizationId);
|
|
230
|
+
if (!parsed.success)
|
|
231
|
+
return false;
|
|
232
|
+
return isUserInOrganization(resolveAuthServerUrl(), audience, await resolveJwksAccessPrivateKey(), user.uid, parsed.data);
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
/** `Authorization: Bearer <access token>` */
|
|
237
|
+
const bearer = async (c, scheme) => {
|
|
238
|
+
const parsed = bearerTokenFromAuthorizationHeader(c.req.header("authorization"));
|
|
239
|
+
if (parsed.kind === "absent")
|
|
240
|
+
return null;
|
|
241
|
+
if (parsed.kind === "malformed") {
|
|
242
|
+
throw unauthorized(scheme, SCHEMAVAULTS_AUTH_RESOLVER_ERROR_CODES.invalidRequest, "The Authorization header must be 'Bearer <access token>'", "invalid_request");
|
|
243
|
+
}
|
|
244
|
+
return principalFromTokenSource(scheme, {
|
|
245
|
+
sourceHint: "Access Token from Authorization Bearer header",
|
|
246
|
+
type: "access",
|
|
247
|
+
token: parsed.token,
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
/** The first-party access-token cookie `access_token_<api_server_id>`. */
|
|
251
|
+
const cookie = async (c, scheme) => {
|
|
252
|
+
const cookieHeader = c.req.header("cookie");
|
|
253
|
+
if (typeof cookieHeader !== "string" || cookieHeader.length === 0)
|
|
254
|
+
return null;
|
|
255
|
+
const cookieName = AccessTokenCookieName(resolveApiServerId());
|
|
256
|
+
const value = readCookie(cookieHeader, cookieName);
|
|
257
|
+
if (value === undefined)
|
|
258
|
+
return null;
|
|
259
|
+
const token = accessTokenFromCookieValue(value);
|
|
260
|
+
if (token === null)
|
|
261
|
+
return null;
|
|
262
|
+
return principalFromTokenSource(scheme, {
|
|
263
|
+
sourceHint: `Access Token from cookie '${cookieName}'`,
|
|
264
|
+
type: "access",
|
|
265
|
+
token,
|
|
266
|
+
});
|
|
267
|
+
};
|
|
268
|
+
return {
|
|
269
|
+
[SCHEMAVAULTS_ACCESS_TOKEN_BEARER_SCHEME_NAME]: bearer,
|
|
270
|
+
[SCHEMAVAULTS_ACCESS_TOKEN_COOKIE_SCHEME_NAME]: cookie,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
export default createSchemaVaultsAuthResolvers;
|
|
274
|
+
//# sourceMappingURL=create-schemavaults-auth-resolvers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-schemavaults-auth-resolvers.js","sourceRoot":"","sources":["../../src/openapi-operations/create-schemavaults-auth-resolvers.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,iBAAiB,EACjB,iBAAiB,GAElB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,oBAAoB,GAErB,MAAM,yCAAyC,CAAC;AAEjD,OAAO,EACL,cAAc,EACd,mCAAmC,GAMpC,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,0BAA0B,MAAM,sCAAsC,CAAC;AAC9E,OAAO,4BAA4B,MAAM,wCAAwC,CAAC;AAClF,OAAO,wBAAwB,EAAE,EAC/B,oCAAoC,GACrC,MAAM,gCAAgC,CAAC;AACxC,OAAO,oBAAoB,MAAM,wBAAwB,CAAC;AAG1D,OAAO,iBAAiB,MAAM,oCAAoC,CAAC;AAEnE,OAAO,EAAE,UAAU,EAAE,MAAM,e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EN_BEARER_SCHEME_NAME, SCHEMAVAULTS_ACCESS_TOKEN_COOKIE_SCHEME_NAME, SCHEMAVAULTS_AUTH_RESOLVER_ERROR_CODES, } from "./create-schemavaults-auth-resolvers";
|
|
2
|
+
export type { CreateSchemaVaultsAuthResolversOptions } from "./create-schemavaults-auth-resolvers";
|
|
3
|
+
export { readCookie } from "./read-cookie";
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { createSchemaVaultsAuthResolvers, createSchemaVaultsAuthResolvers as default, accessTokenFromCookieValue, bearerTokenFromAuthorizationHeader, SCHEMAVAULTS_ACCESS_TOKEN_BEARER_SCHEME_NAME, SCHEMAVAULTS_ACCESS_TOKEN_COOKIE_SCHEME_NAME, SCHEMAVAULTS_AUTH_RESOLVER_ERROR_CODES, } from "./create-schemavaults-auth-resolvers";
|
|
2
|
+
export { readCookie } from "./read-cookie";
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/openapi-operations/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,+BAA+B,EAC/B,+BAA+B,IAAI,OAAO,EAC1C,0BAA0B,EAC1B,kCAAkC,EAClC,4CAA4C,EAC5C,4CAA4C,EAC5C,sCAAsC,GACvC,MAAM,sCAAsC,CAAC;AAE9C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lenient cookie lookup on the raw `Cookie` header.
|
|
3
|
+
*
|
|
4
|
+
* Hono's `getCookie()` follows RFC 6265 strictly and drops any cookie whose
|
|
5
|
+
* value contains a double quote, comma, semicolon or backslash. The access
|
|
6
|
+
* token cookie the auth provider writes is a URL-encoded JSON blob (which
|
|
7
|
+
* strict parsers accept), but deployments that set it as raw JSON would be
|
|
8
|
+
* silently unauthenticated; so the token cookies are read leniently: first
|
|
9
|
+
* `name=` match wins, the value is URL-decoded when it decodes, and
|
|
10
|
+
* returned verbatim otherwise.
|
|
11
|
+
*/
|
|
12
|
+
export declare function readCookie(cookieHeader: string | null | undefined, name: string): string | undefined;
|
|
13
|
+
export default readCookie;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lenient cookie lookup on the raw `Cookie` header.
|
|
3
|
+
*
|
|
4
|
+
* Hono's `getCookie()` follows RFC 6265 strictly and drops any cookie whose
|
|
5
|
+
* value contains a double quote, comma, semicolon or backslash. The access
|
|
6
|
+
* token cookie the auth provider writes is a URL-encoded JSON blob (which
|
|
7
|
+
* strict parsers accept), but deployments that set it as raw JSON would be
|
|
8
|
+
* silently unauthenticated; so the token cookies are read leniently: first
|
|
9
|
+
* `name=` match wins, the value is URL-decoded when it decodes, and
|
|
10
|
+
* returned verbatim otherwise.
|
|
11
|
+
*/
|
|
12
|
+
export function readCookie(cookieHeader, name) {
|
|
13
|
+
if (typeof cookieHeader !== "string" || cookieHeader.length === 0) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
for (const part of cookieHeader.split(";")) {
|
|
17
|
+
const separator = part.indexOf("=");
|
|
18
|
+
if (separator === -1)
|
|
19
|
+
continue;
|
|
20
|
+
if (part.slice(0, separator).trim() !== name)
|
|
21
|
+
continue;
|
|
22
|
+
const raw = part.slice(separator + 1).trim();
|
|
23
|
+
try {
|
|
24
|
+
return decodeURIComponent(raw);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return raw;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
export default readCookie;
|
|
33
|
+
//# sourceMappingURL=read-cookie.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-cookie.js","sourceRoot":"","sources":["../../src/openapi-operations/read-cookie.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,UAAU,CACxB,YAAuC,EACvC,IAAY;IAEZ,IAAI,OAAO,YAAY,KAAK,QAAQ,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAClE,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,KAAK,MAAM,IAAI,IAAI,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QAC3C,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACpC,IAAI,SAAS,KAAK,CAAC,CAAC;YAAE,SAAS;QAC/B,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,IAAI,EAAE,KAAK,IAAI;YAAE,SAAS;QACvD,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAC7C,IAAI,CAAC;YACH,OAAO,kBAAkB,CAAC,GAAG,CAAC,CAAC;QACjC,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,GAAG,CAAC;QACb,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,eAAe,UAAU,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schemavaults/auth-server-sdk",
|
|
3
3
|
"description": "TypeScript SDK for building authenticated endpoints/middlewares for the Auth Server and Resource Servers",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.31.0",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"private": false,
|
|
7
7
|
"repository": {
|
|
@@ -45,7 +45,8 @@
|
|
|
45
45
|
"globals": "16.5.0",
|
|
46
46
|
"@typescript-eslint/eslint-plugin": "8.48.1",
|
|
47
47
|
"@typescript-eslint/parser": "8.48.1",
|
|
48
|
-
"esbuild": "0.27.3"
|
|
48
|
+
"esbuild": "0.27.3",
|
|
49
|
+
"@schemavaults/openapi-operations": "0.4.0"
|
|
49
50
|
},
|
|
50
51
|
"publishConfig": {
|
|
51
52
|
"access": "public"
|
|
@@ -151,6 +152,19 @@
|
|
|
151
152
|
"types": "./dist/cli.d.ts",
|
|
152
153
|
"import": "./dist/cli.js",
|
|
153
154
|
"require": "./dist/cli.cjs"
|
|
155
|
+
},
|
|
156
|
+
"./openapi-operations": {
|
|
157
|
+
"types": "./dist/openapi-operations/index.d.ts",
|
|
158
|
+
"import": "./dist/openapi-operations/index.js",
|
|
159
|
+
"require": "./dist/openapi-operations/index.js"
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
"peerDependencies": {
|
|
163
|
+
"@schemavaults/openapi-operations": "0.4.0"
|
|
164
|
+
},
|
|
165
|
+
"peerDependenciesMeta": {
|
|
166
|
+
"@schemavaults/openapi-operations": {
|
|
167
|
+
"optional": true
|
|
154
168
|
}
|
|
155
169
|
}
|
|
156
170
|
}
|