@willyim/idp 0.4.0 → 0.5.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/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/resource-server.d.ts +90 -0
- package/dist/src/resource-server.d.ts.map +1 -0
- package/dist/src/resource-server.js +206 -0
- package/dist/src/schemas/index.d.ts +3 -0
- package/dist/src/schemas/index.d.ts.map +1 -1
- package/dist/src/schemas/index.js +7 -0
- package/dist/src/schemas/operations.d.ts +4 -0
- package/dist/src/schemas/operations.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/src/index.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export { grants, normalizeClaims, PERMISSIONS_CLAIM, WORKSPACES_CLAIM, type Acto
|
|
|
19
19
|
export { createManagementApi, type ManagementApi, type ManagementApiOptions, } from "./api.js";
|
|
20
20
|
export { createUserKeys, readApiKey, type AuthenticatedKey, type AuthenticateOptions, type AuthenticateResult, type CreateUserApiKeyInput, type ListFilter, type MintedUserApiKey, type UserApiKey, type UserKeyCacheOptions, type UserKeys, type UserKeysOptions, type UserKeyValidation, } from "./user-keys.js";
|
|
21
21
|
export { createIdentities, type Identities, type IdentitiesOptions, type IdentityCacheOptions, type ResolvedIdentity, type IdentityResolution, } from "./identities.js";
|
|
22
|
+
export { createResourceServer, type ResourceServer, type ResourceServerOptions, type VerifiedAccessToken, type AuthResult, } from "./resource-server.js";
|
|
22
23
|
export { parseDuration, type Duration } from "./duration.js";
|
|
23
24
|
export { clearCookie, parseCookies, readCookie, serializeCookie, type CookieOptions, } from "./cookie.js";
|
|
24
25
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAE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AAK,aAAa,GACnB,MAAM,aAAa,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EACL,eAAe,EACf,UAAU,EACV,cAAc,EACd,QAAQ,EACR,gBAAgB,EAChB,KAAK,qBAAqB,EAC1B,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,gBAAgB,EACrB,KAAK,QAAQ,EACb,KAAK,MAAM,GACZ,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,SAAS,EACT,sBAAsB,EACtB,QAAQ,EACR,KAAK,GAAG,EACR,KAAK,UAAU,EACf,KAAK,OAAO,EACZ,KAAK,cAAc,GACpB,MAAM,cAAc,CAAA;AAErB,OAAO,EACL,cAAc,EACd,KAAK,kBAAkB,EACvB,KAAK,aAAa,EAClB,KAAK,YAAY,GAClB,MAAM,YAAY,CAAA;AAEnB,OAAO,EACL,MAAM,EACN,eAAe,EACf,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,KAAK,EACV,KAAK,MAAM,EACX,KAAK,SAAS,GACf,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,mBAAmB,EACnB,KAAK,aAAa,EAClB,KAAK,oBAAoB,GAC1B,MAAM,UAAU,CAAA;AAEjB,OAAO,EACL,cAAc,EACd,UAAU,EACV,KAAK,gBAAgB,EACrB,KAAK,mBAAmB,EACxB,KAAK,kBAAkB,EACvB,KAAK,qBAAqB,EAC1B,KAAK,UAAU,EACf,KAAK,gBAAgB,EACrB,KAAK,UAAU,EACf,KAAK,mBAAmB,EACxB,KAAK,QAAQ,EACb,KAAK,eAAe,EACpB,KAAK,iBAAiB,GACvB,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EACL,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,GACxB,MAAM,iBAAiB,CAAA;AAExB,OAAO,EACL,oBAAoB,EACpB,KAAK,cAAc,EACnB,KAAK,qBAAqB,EAC1B,KAAK,mBAAmB,EACxB,KAAK,UAAU,GAChB,MAAM,sBAAsB,CAAA;AAE7B,OAAO,EAAE,aAAa,EAAE,KAAK,QAAQ,EAAE,MAAM,eAAe,CAAA;AAC5D,OAAO,EACL,WAAW,EACX,YAAY,EACZ,UAAU,EACV,eAAe,EACf,KAAK,aAAa,GACnB,MAAM,aAAa,CAAA"}
|
package/dist/src/index.js
CHANGED
|
@@ -19,5 +19,6 @@ export { grants, normalizeClaims, PERMISSIONS_CLAIM, WORKSPACES_CLAIM, } from ".
|
|
|
19
19
|
export { createManagementApi, } from "./api.js";
|
|
20
20
|
export { createUserKeys, readApiKey, } from "./user-keys.js";
|
|
21
21
|
export { createIdentities, } from "./identities.js";
|
|
22
|
+
export { createResourceServer, } from "./resource-server.js";
|
|
22
23
|
export { parseDuration } from "./duration.js";
|
|
23
24
|
export { clearCookie, parseCookies, readCookie, serializeCookie, } from "./cookie.js";
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Being an OAuth 2.1 resource server against the willy.im IdP — which is what
|
|
3
|
+
* an MCP server is.
|
|
4
|
+
*
|
|
5
|
+
* The MCP authorization spec is plain OAuth with three discovery hops, and
|
|
6
|
+
* every one of them is either served by the IdP already or is a static
|
|
7
|
+
* document this module writes for you:
|
|
8
|
+
*
|
|
9
|
+
* 1. The client hits the resource without a token and gets a 401 carrying
|
|
10
|
+
* `WWW-Authenticate: Bearer resource_metadata="…"` → `challenge()`
|
|
11
|
+
* 2. It fetches that URL, the Protected Resource Metadata (RFC 9728), which
|
|
12
|
+
* names the authorization server → `metadata()`
|
|
13
|
+
* 3. It fetches the AS metadata (RFC 8414) from the IdP, registers itself
|
|
14
|
+
* (RFC 7591), runs authorization code + PKCE with `resource=` (RFC 8707),
|
|
15
|
+
* and comes back with a JWT access token whose `aud` is this resource.
|
|
16
|
+
* 4. The resource server verifies that JWT against the IdP's JWKS and reads
|
|
17
|
+
* the permissions claim → `authenticate()`
|
|
18
|
+
*
|
|
19
|
+
* Step 4 is the only one with any real code, and it is deliberately small:
|
|
20
|
+
* signature (EdDSA/Ed25519, ES256 or RS256 via WebCrypto — no jose, no deps),
|
|
21
|
+
* issuer, audience, time. The permissions on the token were computed by the
|
|
22
|
+
* IdP for the app that owns this resource (the same way a browser session's
|
|
23
|
+
* are), so the app looks nothing up — it reads a claim and matches it with
|
|
24
|
+
* `grants()` like everywhere else.
|
|
25
|
+
*
|
|
26
|
+
* The JWKS is cached and refetched once on an unknown `kid`, which is how a key
|
|
27
|
+
* rotation at the IdP is picked up without a restart.
|
|
28
|
+
*/
|
|
29
|
+
export declare const PERMISSIONS_CLAIM = "https://willy.im/permissions";
|
|
30
|
+
export declare const APP_CLAIM = "https://willy.im/app";
|
|
31
|
+
export type ResourceServerOptions = {
|
|
32
|
+
/** The IdP's OAuth issuer — `https://idp.willy.im/auth` (note the /auth). */
|
|
33
|
+
issuer: string;
|
|
34
|
+
/**
|
|
35
|
+
* This server's canonical URI, exactly as registered on the application in
|
|
36
|
+
* the IdP (`resources`) — e.g. `https://bender.romo.fyi/mcp`. It is the
|
|
37
|
+
* audience a token must carry, compared byte for byte.
|
|
38
|
+
*/
|
|
39
|
+
resource: string;
|
|
40
|
+
/** Defaults to `${issuer}/jwks`. */
|
|
41
|
+
jwksUrl?: string;
|
|
42
|
+
/** Advertised in the metadata document. Informational only. */
|
|
43
|
+
scopesSupported?: string[];
|
|
44
|
+
/** How long a fetched JWKS is reused. Default 10 minutes. */
|
|
45
|
+
jwksTtlMs?: number;
|
|
46
|
+
/** Clock skew tolerated on exp/nbf. Default 60s. */
|
|
47
|
+
leewaySeconds?: number;
|
|
48
|
+
fetch?: typeof fetch;
|
|
49
|
+
/** Clock seam, for tests. */
|
|
50
|
+
now?: () => number;
|
|
51
|
+
};
|
|
52
|
+
/** A verified access token, reduced to what an app acts on. */
|
|
53
|
+
export type VerifiedAccessToken = {
|
|
54
|
+
/** The IdP user id — the same `sub` a session or a wak_ key resolves to. */
|
|
55
|
+
sub: string;
|
|
56
|
+
/** The app the permissions are for (the owner of `resource`), when present. */
|
|
57
|
+
app: string | null;
|
|
58
|
+
/** The user's product permissions for that app. Match with `has()`/`grants()`. */
|
|
59
|
+
permissions: string[];
|
|
60
|
+
/** Space-delimited OAuth scopes on the token (openid/profile/…), if any. */
|
|
61
|
+
scopes: string[];
|
|
62
|
+
/** Everything else on the token, for a caller that needs a raw claim. */
|
|
63
|
+
claims: Record<string, unknown>;
|
|
64
|
+
};
|
|
65
|
+
export type AuthResult = {
|
|
66
|
+
ok: true;
|
|
67
|
+
token: VerifiedAccessToken;
|
|
68
|
+
} | {
|
|
69
|
+
ok: false;
|
|
70
|
+
status: 401 | 403;
|
|
71
|
+
error: string;
|
|
72
|
+
description: string;
|
|
73
|
+
};
|
|
74
|
+
export declare function createResourceServer(options: ResourceServerOptions): {
|
|
75
|
+
verify: (token: string) => Promise<AuthResult>;
|
|
76
|
+
authenticate: (request: {
|
|
77
|
+
headers: {
|
|
78
|
+
get(name: string): string | null;
|
|
79
|
+
};
|
|
80
|
+
}, init?: {
|
|
81
|
+
permissions?: string[];
|
|
82
|
+
}) => Promise<AuthResult>;
|
|
83
|
+
challenge: (error?: {
|
|
84
|
+
error: string;
|
|
85
|
+
description: string;
|
|
86
|
+
}) => string;
|
|
87
|
+
metadata: () => Record<string, unknown>;
|
|
88
|
+
};
|
|
89
|
+
export type ResourceServer = ReturnType<typeof createResourceServer>;
|
|
90
|
+
//# sourceMappingURL=resource-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resource-server.d.ts","sourceRoot":"","sources":["../../src/resource-server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAIH,eAAO,MAAM,iBAAiB,iCAAiC,CAAA;AAC/D,eAAO,MAAM,SAAS,yBAAyB,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AAE,CAAA;KAAE,SACpD;QAAE,WAAW,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,KAC/B,OAAO,CAAC,UAAU,CAAC;wBAwBK;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,KAAG,MAAM;oBAarD,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;EAU7C;AAED,MAAM,MAAM,cAAc,GAAG,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAA"}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Being an OAuth 2.1 resource server against the willy.im IdP — which is what
|
|
3
|
+
* an MCP server is.
|
|
4
|
+
*
|
|
5
|
+
* The MCP authorization spec is plain OAuth with three discovery hops, and
|
|
6
|
+
* every one of them is either served by the IdP already or is a static
|
|
7
|
+
* document this module writes for you:
|
|
8
|
+
*
|
|
9
|
+
* 1. The client hits the resource without a token and gets a 401 carrying
|
|
10
|
+
* `WWW-Authenticate: Bearer resource_metadata="…"` → `challenge()`
|
|
11
|
+
* 2. It fetches that URL, the Protected Resource Metadata (RFC 9728), which
|
|
12
|
+
* names the authorization server → `metadata()`
|
|
13
|
+
* 3. It fetches the AS metadata (RFC 8414) from the IdP, registers itself
|
|
14
|
+
* (RFC 7591), runs authorization code + PKCE with `resource=` (RFC 8707),
|
|
15
|
+
* and comes back with a JWT access token whose `aud` is this resource.
|
|
16
|
+
* 4. The resource server verifies that JWT against the IdP's JWKS and reads
|
|
17
|
+
* the permissions claim → `authenticate()`
|
|
18
|
+
*
|
|
19
|
+
* Step 4 is the only one with any real code, and it is deliberately small:
|
|
20
|
+
* signature (EdDSA/Ed25519, ES256 or RS256 via WebCrypto — no jose, no deps),
|
|
21
|
+
* issuer, audience, time. The permissions on the token were computed by the
|
|
22
|
+
* IdP for the app that owns this resource (the same way a browser session's
|
|
23
|
+
* are), so the app looks nothing up — it reads a claim and matches it with
|
|
24
|
+
* `grants()` like everywhere else.
|
|
25
|
+
*
|
|
26
|
+
* The JWKS is cached and refetched once on an unknown `kid`, which is how a key
|
|
27
|
+
* rotation at the IdP is picked up without a restart.
|
|
28
|
+
*/
|
|
29
|
+
import { grants } from "./claims.js";
|
|
30
|
+
export const PERMISSIONS_CLAIM = "https://willy.im/permissions";
|
|
31
|
+
export const APP_CLAIM = "https://willy.im/app";
|
|
32
|
+
const DEFAULT_JWKS_TTL_MS = 600_000;
|
|
33
|
+
const DEFAULT_LEEWAY_S = 60;
|
|
34
|
+
function b64urlToBytes(s) {
|
|
35
|
+
const pad = s.length % 4 === 0 ? "" : "=".repeat(4 - (s.length % 4));
|
|
36
|
+
const b = atob(s.replace(/-/g, "+").replace(/_/g, "/") + pad);
|
|
37
|
+
const out = new Uint8Array(b.length);
|
|
38
|
+
for (let i = 0; i < b.length; i++)
|
|
39
|
+
out[i] = b.charCodeAt(i);
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
function decodeJson(segment) {
|
|
43
|
+
return JSON.parse(new TextDecoder().decode(b64urlToBytes(segment)));
|
|
44
|
+
}
|
|
45
|
+
/** The WebCrypto import + verify params for the algorithms the IdP may sign with. */
|
|
46
|
+
function algParams(alg, jwk) {
|
|
47
|
+
switch (alg) {
|
|
48
|
+
case "EdDSA":
|
|
49
|
+
return { importAlg: { name: "Ed25519" }, verifyAlg: { name: "Ed25519" } };
|
|
50
|
+
case "ES256":
|
|
51
|
+
return { importAlg: { name: "ECDSA", namedCurve: "P-256" }, verifyAlg: { name: "ECDSA", hash: "SHA-256" } };
|
|
52
|
+
case "RS256":
|
|
53
|
+
return {
|
|
54
|
+
importAlg: { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
|
55
|
+
verifyAlg: { name: "RSASSA-PKCS1-v1_5" },
|
|
56
|
+
};
|
|
57
|
+
default:
|
|
58
|
+
return jwk.kty ? null : null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export function createResourceServer(options) {
|
|
62
|
+
const doFetch = options.fetch ?? fetch;
|
|
63
|
+
const now = options.now ?? (() => Date.now());
|
|
64
|
+
const jwksUrl = options.jwksUrl ?? `${options.issuer.replace(/\/$/, "")}/jwks`;
|
|
65
|
+
const jwksTtlMs = options.jwksTtlMs ?? DEFAULT_JWKS_TTL_MS;
|
|
66
|
+
const leeway = (options.leewaySeconds ?? DEFAULT_LEEWAY_S) * 1000;
|
|
67
|
+
let jwks = null;
|
|
68
|
+
async function loadJwks(force) {
|
|
69
|
+
if (!force && jwks && now() - jwks.at < jwksTtlMs)
|
|
70
|
+
return jwks.keys;
|
|
71
|
+
const res = await doFetch(jwksUrl);
|
|
72
|
+
if (!res.ok)
|
|
73
|
+
throw new Error(`jwks fetch failed: ${res.status}`);
|
|
74
|
+
const body = (await res.json());
|
|
75
|
+
jwks = { at: now(), keys: body.keys ?? [] };
|
|
76
|
+
return jwks.keys;
|
|
77
|
+
}
|
|
78
|
+
/** Find the signing key by kid, refetching ONCE on a miss to catch rotation. */
|
|
79
|
+
async function keyFor(kid) {
|
|
80
|
+
const pick = (keys) => keys.find((k) => (kid ? k.kid === kid : true)) ?? null;
|
|
81
|
+
let key = pick(await loadJwks(false));
|
|
82
|
+
if (!key)
|
|
83
|
+
key = pick(await loadJwks(true));
|
|
84
|
+
return key;
|
|
85
|
+
}
|
|
86
|
+
async function verifySignature(header, signingInput, signature) {
|
|
87
|
+
const jwk = await keyFor(header.kid);
|
|
88
|
+
if (!jwk)
|
|
89
|
+
return false;
|
|
90
|
+
const params = algParams(header.alg, jwk);
|
|
91
|
+
if (!params)
|
|
92
|
+
return false;
|
|
93
|
+
const key = await crypto.subtle.importKey("jwk", jwk, params.importAlg, false, ["verify"]);
|
|
94
|
+
return crypto.subtle.verify(params.verifyAlg, key, signature, new TextEncoder().encode(signingInput));
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Verifies a raw bearer token. Returns a discriminated result rather than
|
|
98
|
+
* throwing, so the caller owns the response shape. `insufficient_scope` is a
|
|
99
|
+
* 403; everything about the token being absent or bad is a 401.
|
|
100
|
+
*/
|
|
101
|
+
async function verify(token) {
|
|
102
|
+
const parts = token.split(".");
|
|
103
|
+
if (parts.length !== 3) {
|
|
104
|
+
return { ok: false, status: 401, error: "invalid_token", description: "Not a JWT." };
|
|
105
|
+
}
|
|
106
|
+
let header;
|
|
107
|
+
let claims;
|
|
108
|
+
try {
|
|
109
|
+
header = decodeJson(parts[0]);
|
|
110
|
+
claims = decodeJson(parts[1]);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return { ok: false, status: 401, error: "invalid_token", description: "Malformed JWT." };
|
|
114
|
+
}
|
|
115
|
+
let valid = false;
|
|
116
|
+
try {
|
|
117
|
+
valid = await verifySignature(header, `${parts[0]}.${parts[1]}`, b64urlToBytes(parts[2]));
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
valid = false;
|
|
121
|
+
}
|
|
122
|
+
if (!valid) {
|
|
123
|
+
return { ok: false, status: 401, error: "invalid_token", description: "Bad signature." };
|
|
124
|
+
}
|
|
125
|
+
if (claims.iss !== options.issuer) {
|
|
126
|
+
return { ok: false, status: 401, error: "invalid_token", description: "Wrong issuer." };
|
|
127
|
+
}
|
|
128
|
+
const aud = claims.aud;
|
|
129
|
+
const audOk = Array.isArray(aud) ? aud.includes(options.resource) : aud === options.resource;
|
|
130
|
+
if (!audOk) {
|
|
131
|
+
return { ok: false, status: 401, error: "invalid_token", description: "Token is for a different resource." };
|
|
132
|
+
}
|
|
133
|
+
const t = now();
|
|
134
|
+
if (typeof claims.exp === "number" && t > claims.exp * 1000 + leeway) {
|
|
135
|
+
return { ok: false, status: 401, error: "invalid_token", description: "Token expired." };
|
|
136
|
+
}
|
|
137
|
+
if (typeof claims.nbf === "number" && t < claims.nbf * 1000 - leeway) {
|
|
138
|
+
return { ok: false, status: 401, error: "invalid_token", description: "Token not yet valid." };
|
|
139
|
+
}
|
|
140
|
+
const permissions = Array.isArray(claims[PERMISSIONS_CLAIM])
|
|
141
|
+
? claims[PERMISSIONS_CLAIM].filter((p) => typeof p === "string")
|
|
142
|
+
: [];
|
|
143
|
+
const scopes = typeof claims.scope === "string" ? claims.scope.split(" ").filter(Boolean) : [];
|
|
144
|
+
return {
|
|
145
|
+
ok: true,
|
|
146
|
+
token: {
|
|
147
|
+
sub: String(claims.sub ?? ""),
|
|
148
|
+
app: typeof claims[APP_CLAIM] === "string" ? claims[APP_CLAIM] : null,
|
|
149
|
+
permissions,
|
|
150
|
+
scopes,
|
|
151
|
+
claims,
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The whole check off a `Request`: pull the bearer, verify it, and confirm
|
|
157
|
+
* every required permission (wildcard-aware via `grants()`). Returns a
|
|
158
|
+
* result rather than throwing.
|
|
159
|
+
*/
|
|
160
|
+
async function authenticate(request, init = {}) {
|
|
161
|
+
const authz = request.headers.get("authorization");
|
|
162
|
+
const bearer = authz && /^Bearer\s+(.+)$/i.exec(authz.trim())?.[1];
|
|
163
|
+
if (!bearer) {
|
|
164
|
+
return { ok: false, status: 401, error: "invalid_token", description: "No bearer token." };
|
|
165
|
+
}
|
|
166
|
+
const verified = await verify(bearer.trim());
|
|
167
|
+
if (!verified.ok)
|
|
168
|
+
return verified;
|
|
169
|
+
const missing = (init.permissions ?? []).filter((p) => !grants(verified.token.permissions, p));
|
|
170
|
+
if (missing.length) {
|
|
171
|
+
return {
|
|
172
|
+
ok: false,
|
|
173
|
+
status: 403,
|
|
174
|
+
error: "insufficient_scope",
|
|
175
|
+
description: `Missing permission(s): ${missing.join(", ")}.`,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
return verified;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The `WWW-Authenticate` value for a 401, pointing a client at the metadata
|
|
182
|
+
* document so it can discover how to get a token (RFC 9728 §5.1).
|
|
183
|
+
*/
|
|
184
|
+
function challenge(error) {
|
|
185
|
+
const parts = [
|
|
186
|
+
`Bearer resource_metadata="${options.resource}/.well-known/oauth-protected-resource"`,
|
|
187
|
+
];
|
|
188
|
+
if (error)
|
|
189
|
+
parts.push(`error="${error.error}"`, `error_description="${error.description}"`);
|
|
190
|
+
return parts.join(", ");
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* The Protected Resource Metadata document (RFC 9728) — served at
|
|
194
|
+
* `/.well-known/oauth-protected-resource`. It names this resource and the
|
|
195
|
+
* IdP as its authorization server; the client takes it from there.
|
|
196
|
+
*/
|
|
197
|
+
function metadata() {
|
|
198
|
+
return {
|
|
199
|
+
resource: options.resource,
|
|
200
|
+
authorization_servers: [options.issuer],
|
|
201
|
+
bearer_methods_supported: ["header"],
|
|
202
|
+
...(options.scopesSupported ? { scopes_supported: options.scopesSupported } : {}),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
return { verify, authenticate, challenge, metadata };
|
|
206
|
+
}
|
|
@@ -16,6 +16,7 @@ export declare const ApplicationSchema: z.ZodObject<{
|
|
|
16
16
|
app: z.ZodNullable<z.ZodString>;
|
|
17
17
|
allowSignup: z.ZodBoolean;
|
|
18
18
|
permissions: z.ZodArray<z.ZodString>;
|
|
19
|
+
resources: z.ZodArray<z.ZodString>;
|
|
19
20
|
redirectUris: z.ZodArray<z.ZodString>;
|
|
20
21
|
disabled: z.ZodBoolean;
|
|
21
22
|
createdAt: z.ZodString;
|
|
@@ -41,6 +42,7 @@ export declare const ApplicationListSchema: z.ZodObject<{
|
|
|
41
42
|
app: z.ZodNullable<z.ZodString>;
|
|
42
43
|
allowSignup: z.ZodBoolean;
|
|
43
44
|
permissions: z.ZodArray<z.ZodString>;
|
|
45
|
+
resources: z.ZodArray<z.ZodString>;
|
|
44
46
|
redirectUris: z.ZodArray<z.ZodString>;
|
|
45
47
|
disabled: z.ZodBoolean;
|
|
46
48
|
createdAt: z.ZodString;
|
|
@@ -66,6 +68,7 @@ export declare const UpdateApplicationInput: z.ZodObject<{
|
|
|
66
68
|
name: z.ZodOptional<z.ZodString>;
|
|
67
69
|
redirectUris: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
68
70
|
allowSignup: z.ZodOptional<z.ZodBoolean>;
|
|
71
|
+
resources: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
69
72
|
}, z.core.$strip>;
|
|
70
73
|
export declare const ClientSecretSchema: z.ZodObject<{
|
|
71
74
|
clientSecret: z.ZodString;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/schemas/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,eAAO,MAAM,iBAAiB
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/schemas/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,eAAO,MAAM,iBAAiB;;;;;;;;;;iBAY5B,CAAA;AAEF,eAAO,MAAM,UAAU;;;;;;iBAMrB,CAAA;AAEF,eAAO,MAAM,eAAe;;;;;;iBAM1B,CAAA;AAEF,eAAO,MAAM,qBAAqB;;;;;;;;;;;;iBAAyD,CAAA;AAE3F;;;GAGG;AACH,eAAO,MAAM,sBAAsB;;;;;iBAejC,CAAA;AACF,eAAO,MAAM,wBAAwB;;;;iBAInC,CAAA;AAEF,uEAAuE;AACvE,eAAO,MAAM,sBAAsB;;;;;iBAQjC,CAAA;AAEF,eAAO,MAAM,kBAAkB;;iBAE7B,CAAA;AAEF,+DAA+D;AAC/D,eAAO,MAAM,sBAAsB;;iBAEjC,CAAA;AACF,eAAO,MAAM,oBAAoB;;iBAAiD,CAAA;AAClF,eAAO,MAAM,cAAc;;;;;;;;iBAA2C,CAAA;AACtE,eAAO,MAAM,mBAAmB;;;;;;;;iBAAqD,CAAA;AAIrF,eAAO,MAAM,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;;;iBAI9B,CAAA;AAIF,eAAO,MAAM,cAAc;;;;;;;;;;;;;iBASzB,CAAA;AACF,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;iBAA8C,CAAA;AAE7E,eAAO,MAAM,mBAAmB;;;iBAG9B,CAAA;AACF,eAAO,MAAM,qBAAqB;;;;iBAIhC,CAAA"}
|
|
@@ -16,6 +16,9 @@ export const ApplicationSchema = z.object({
|
|
|
16
16
|
app: z.string().nullable().describe("Application key; consumer workspace claims are filtered by this"),
|
|
17
17
|
allowSignup: z.boolean().describe("Whether unknown users may sign themselves up"),
|
|
18
18
|
permissions: z.array(z.string()).describe("The app's declared product-permission catalog"),
|
|
19
|
+
resources: z
|
|
20
|
+
.array(z.string())
|
|
21
|
+
.describe("Protected resource URIs (e.g. the app's MCP server) — valid `resource` audiences for access tokens"),
|
|
19
22
|
redirectUris: z.array(z.string()),
|
|
20
23
|
disabled: z.boolean(),
|
|
21
24
|
createdAt: z.string().describe("ISO 8601 timestamp"),
|
|
@@ -63,6 +66,10 @@ export const UpdateApplicationInput = z.object({
|
|
|
63
66
|
name: z.string().min(1).optional(),
|
|
64
67
|
redirectUris: z.array(z.string().min(1)).min(1).optional(),
|
|
65
68
|
allowSignup: z.boolean().optional(),
|
|
69
|
+
resources: z
|
|
70
|
+
.array(z.string().url())
|
|
71
|
+
.optional()
|
|
72
|
+
.describe("Replace the app's protected resource URIs — absolute https, no fragment"),
|
|
66
73
|
});
|
|
67
74
|
export const ClientSecretSchema = z.object({
|
|
68
75
|
clientSecret: z.string().describe("Plaintext client secret — shown exactly once, never stored"),
|
|
@@ -43,6 +43,7 @@ export declare const operations: {
|
|
|
43
43
|
app: z.ZodNullable<z.ZodString>;
|
|
44
44
|
allowSignup: z.ZodBoolean;
|
|
45
45
|
permissions: z.ZodArray<z.ZodString>;
|
|
46
|
+
resources: z.ZodArray<z.ZodString>;
|
|
46
47
|
redirectUris: z.ZodArray<z.ZodString>;
|
|
47
48
|
disabled: z.ZodBoolean;
|
|
48
49
|
createdAt: z.ZodString;
|
|
@@ -79,6 +80,7 @@ export declare const operations: {
|
|
|
79
80
|
app: z.ZodNullable<z.ZodString>;
|
|
80
81
|
allowSignup: z.ZodBoolean;
|
|
81
82
|
permissions: z.ZodArray<z.ZodString>;
|
|
83
|
+
resources: z.ZodArray<z.ZodString>;
|
|
82
84
|
redirectUris: z.ZodArray<z.ZodString>;
|
|
83
85
|
disabled: z.ZodBoolean;
|
|
84
86
|
createdAt: z.ZodString;
|
|
@@ -95,6 +97,7 @@ export declare const operations: {
|
|
|
95
97
|
name: z.ZodOptional<z.ZodString>;
|
|
96
98
|
redirectUris: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
97
99
|
allowSignup: z.ZodOptional<z.ZodBoolean>;
|
|
100
|
+
resources: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
98
101
|
}, z.core.$strip>;
|
|
99
102
|
readonly successCode: "200";
|
|
100
103
|
readonly success: z.ZodObject<{
|
|
@@ -103,6 +106,7 @@ export declare const operations: {
|
|
|
103
106
|
app: z.ZodNullable<z.ZodString>;
|
|
104
107
|
allowSignup: z.ZodBoolean;
|
|
105
108
|
permissions: z.ZodArray<z.ZodString>;
|
|
109
|
+
resources: z.ZodArray<z.ZodString>;
|
|
106
110
|
redirectUris: z.ZodArray<z.ZodString>;
|
|
107
111
|
disabled: z.ZodBoolean;
|
|
108
112
|
createdAt: z.ZodString;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"operations.d.ts","sourceRoot":"","sources":["../../../src/schemas/operations.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAsCvB,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,KAAK,CAAA;AAEpE,iFAAiF;AACjF,MAAM,MAAM,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,W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MAAM,YAAY,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,SAAS,MAAM,IAAI,GAAG,CAAC,IAAI,CAAC,EAAE,SAAS,YAAY,GAC/F,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,GACvB,KAAK,CAAA;AAET,iFAAiF;AACjF,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,GAAG,MAAM,IAAI,MAAM,IAAI,IAAI,MAAM,IAAI,EAAE,GAC5F,IAAI,GAAG,cAAc,CAAC,IAAI,CAAC,GAC3B,KAAK,CAAA;AAET,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAE7E"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willyim/idp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Login for apps that don't own identity \u2014 OIDC client, server sessions, and react-router guards against the willy.im IdP",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/src/index.js",
|