@foldrun/core 9.3.0 → 9.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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/src/oauth-capabilities.d.ts +22 -0
- package/dist/src/oauth-capabilities.js +176 -0
- package/dist/src/oauth-discovery.d.ts +90 -0
- package/dist/src/oauth-discovery.js +168 -0
- package/dist/src/oauth-token.d.ts +6 -1
- package/dist/src/oauth-token.js +6 -1
- package/dist/src/secrets.d.ts +51 -0
- package/dist/src/secrets.js +128 -1
- package/dist/src/untrusted-fetch.d.ts +4 -0
- package/dist/src/untrusted-fetch.js +1 -1
- package/index.ts +1 -0
- package/package.json +1 -1
- package/src/oauth-capabilities.ts +202 -0
- package/src/oauth-discovery.ts +208 -0
- package/src/oauth-token.ts +5 -1
- package/src/secrets.ts +137 -1
- package/src/untrusted-fetch.ts +5 -1
package/dist/index.d.ts
CHANGED
|
@@ -31,6 +31,7 @@ export * from "./src/storage.ts";
|
|
|
31
31
|
export * from "./src/secrets.ts";
|
|
32
32
|
export * from "./src/oauth-presets.ts";
|
|
33
33
|
export * from "./src/oauth-token.ts";
|
|
34
|
+
export * from "./src/oauth-discovery.ts";
|
|
34
35
|
export * from "./src/secret-files.ts";
|
|
35
36
|
export * from "./src/notify.ts";
|
|
36
37
|
export * from "./src/webhook.ts";
|
package/dist/index.js
CHANGED
|
@@ -42,6 +42,7 @@ export * from "./src/storage.js";
|
|
|
42
42
|
export * from "./src/secrets.js";
|
|
43
43
|
export * from "./src/oauth-presets.js";
|
|
44
44
|
export * from "./src/oauth-token.js";
|
|
45
|
+
export * from "./src/oauth-discovery.js";
|
|
45
46
|
export * from "./src/secret-files.js";
|
|
46
47
|
export * from "./src/notify.js";
|
|
47
48
|
export * from "./src/webhook.js";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type OAuthRole = "client" | "server";
|
|
2
|
+
export type OAuthStatus = "supported" | "planned" | "refused" | "not-needed";
|
|
3
|
+
/** A test that proves a row: the repo, the file in it, and the test's name
|
|
4
|
+
* as written (a substring of the title is enough). */
|
|
5
|
+
export interface OAuthProof {
|
|
6
|
+
repo: "foldrun-core" | "foldrun-platform" | "foldrun-web" | "foldrun-cli";
|
|
7
|
+
file: string;
|
|
8
|
+
test: string;
|
|
9
|
+
}
|
|
10
|
+
export interface OAuthCapability {
|
|
11
|
+
id: string;
|
|
12
|
+
role: OAuthRole;
|
|
13
|
+
/** What it is, in a person's words. */
|
|
14
|
+
title: string;
|
|
15
|
+
/** The RFC or spec that defines it. */
|
|
16
|
+
spec: string;
|
|
17
|
+
status: OAuthStatus;
|
|
18
|
+
/** How to use it (supported), what is coming (planned), or why not. */
|
|
19
|
+
note: string;
|
|
20
|
+
proof?: OAuthProof[];
|
|
21
|
+
}
|
|
22
|
+
export declare const OAUTH_CAPABILITIES: OAuthCapability[];
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// Every kind of OAuth foldrun meets, in each role, and what foldrun does
|
|
2
|
+
// about it: supported (with the tests that prove it), planned, refused (with
|
|
3
|
+
// the reason a person sees), or not needed. The single source the customer
|
|
4
|
+
// page `foldrun-docs/oauth.md` is generated from (`node scripts/oauth-doc.mjs`)
|
|
5
|
+
// — the same pattern as frontmatter-fields.ts. tests/oauth-capabilities.test.ts
|
|
6
|
+
// fails when a supported row names a test that does not exist, or the page is
|
|
7
|
+
// stale.
|
|
8
|
+
//
|
|
9
|
+
// The roles:
|
|
10
|
+
// client an agent reaches Google, Slack, a bank — foldrun holds the token
|
|
11
|
+
// server an AI app signs in to /api/mcp — foldrun issues the token
|
|
12
|
+
const core = (file, test) => ({ repo: "foldrun-core", file, test });
|
|
13
|
+
const platform = (file, test) => ({ repo: "foldrun-platform", file, test });
|
|
14
|
+
const web = (file, test) => ({ repo: "foldrun-web", file, test });
|
|
15
|
+
const cli = (file, test) => ({ repo: "foldrun-cli", file, test });
|
|
16
|
+
const KC = "tests/e2e-oauth-keycloak.test.ts";
|
|
17
|
+
export const OAUTH_CAPABILITIES = [
|
|
18
|
+
// ------------------------------------------------------------ client
|
|
19
|
+
{
|
|
20
|
+
id: "client.authorization-code-pkce", role: "client", title: "Authorization code with PKCE (S256)", spec: "RFC 6749 §4.1, RFC 7636",
|
|
21
|
+
status: "supported", note: "Settings → Connections or `foldrun connect`. PKCE is on for every connection.",
|
|
22
|
+
proof: [platform("tests/oauth-connect.test.ts", "PKCE: on by default"), platform(KC, "authorization code + PKCE S256 + PAR"), cli("tests/cli-connect.test.ts", "connect: PKCE by default")],
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: "client.refresh-rotation", role: "client", title: "Refresh, and refresh-token rotation", spec: "RFC 6749 §6, RFC 9700 §2.2.2",
|
|
26
|
+
status: "supported", note: "Refreshed before every use; a rotated refresh token replaces the stored one.",
|
|
27
|
+
proof: [core("tests/secret-types.test.ts", "a rotated refresh token is saved where the old one was"), core("tests/oauth-provider-fixtures.test.ts", "rotating refresh tokens: every refresh returns a new one"), platform(KC, "the refresh works and rotates")],
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
id: "client.client-credentials", role: "client", title: "Client credentials (machine to machine)", spec: "RFC 6749 §4.4",
|
|
31
|
+
status: "supported", note: "The \"OAuth — client credentials\" connection, or `foldrun secrets set NAME --kind m2m`.",
|
|
32
|
+
proof: [platform(KC, "client credentials (machine to machine)")],
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
id: "client.secret-post", role: "client", title: "Client secret in the form body", spec: "RFC 6749 §2.3.1",
|
|
36
|
+
status: "supported", note: "The default (`token_auth: client_secret_post`).",
|
|
37
|
+
proof: [core("tests/oauth-token.test.ts", "client_secret_post (and absent with a secret)"), platform(KC, "client_secret_post")],
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: "client.secret-basic", role: "client", title: "Client secret as HTTP Basic", spec: "RFC 6749 §2.3.1",
|
|
41
|
+
status: "supported", note: "`token_auth: client_secret_basic`, or `--token-auth basic`. Notion, Spotify, Reddit and Zoom need it.",
|
|
42
|
+
proof: [core("tests/oauth-token.test.ts", "a Basic-only token endpoint"), platform("tests/oauth-connect.test.ts", "token_auth client_secret_basic"), platform(KC, "client_secret_basic"), cli("tests/cli-connect.test.ts", "connect --token-auth basic")],
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
id: "client.public", role: "client", title: "Public app (no secret), PKCE only", spec: "RFC 9700 §2.1.1",
|
|
46
|
+
status: "supported", note: "Leave the client secret empty.",
|
|
47
|
+
proof: [platform("tests/oauth-connect.test.ts", "a public app (no secret) connects with PKCE"), platform(KC, "a public app (no secret)")],
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
id: "client.discovery", role: "client", title: "Metadata discovery from an issuer", spec: "RFC 8414, OpenID Connect Discovery",
|
|
51
|
+
status: "supported", note: "The Issuer field's Discover, or `--issuer`.",
|
|
52
|
+
proof: [core("tests/oauth-discovery-revoke.test.ts", "an RFC 8414 server"), core("tests/oauth-discovery-revoke.test.ts", "refused: a document naming another issuer"), platform(KC, "discovery (RFC 8414 / OIDC)"), cli("tests/cli-connect.test.ts", "connect --issuer: the metadata fills the URLs")],
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: "client.iss-parameter", role: "client", title: "Issuer in the authorization response (mix-up defence)", spec: "RFC 9207",
|
|
56
|
+
status: "supported", note: "With an issuer, the `iss` that comes back must be it.",
|
|
57
|
+
proof: [platform("tests/oauth-connect.test.ts", "iss: a pinned issuer must come back exactly"), platform(KC, "a callback whose iss is not the realm is refused"), cli("tests/cli-connect.test.ts", "a callback whose iss is another server is refused")],
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
id: "client.revocation", role: "client", title: "Revocation when a connection ends", spec: "RFC 7009",
|
|
61
|
+
status: "supported", note: "Deleting a connection, or reconnecting it with another app, revokes the old grant.",
|
|
62
|
+
proof: [core("tests/oauth-discovery-revoke.test.ts", "revoking: the refresh token, then the cached access token"), web("tests/secrets-revoke.test.ts", "deleting revokes"), platform(KC, "revocation (RFC 7009)")],
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
id: "client.par", role: "client", title: "Pushed authorization requests", spec: "RFC 9126",
|
|
66
|
+
status: "supported", note: "Used whenever the provider has a PAR endpoint.",
|
|
67
|
+
proof: [platform("tests/oauth-connect.test.ts", "PAR (RFC 9126)"), platform(KC, "authorization code + PKCE S256 + PAR"), cli("tests/cli-connect.test.ts", "with a PAR endpoint")],
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
id: "client.token-response", role: "client", title: "Token responses as providers send them", spec: "RFC 6749 §5.1",
|
|
71
|
+
status: "supported", note: "JSON or form-encoded, `expires_in` as a string, Slack's `authed_user`.",
|
|
72
|
+
proof: [core("tests/oauth-token.test.ts", "Slack: a user token alone"), core("tests/oauth-token.test.ts", "a form-encoded answer"), core("tests/oauth-provider-fixtures.test.ts", "no expires_in; instance_url the agent needs"), core("tests/oauth-provider-fixtures.test.ts", "form-encoded unless asked for JSON")],
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
id: "client.jwt-bearer", role: "client", title: "JWT bearer grant (service accounts)", spec: "RFC 7523 §2.1",
|
|
76
|
+
status: "supported", note: "The service-account connection (`--kind service-account`).",
|
|
77
|
+
proof: [core("tests/secret-types.test.ts", "service account with a subject")],
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
id: "client.rar", role: "client", title: "Rich authorization requests", spec: "RFC 9396",
|
|
81
|
+
status: "supported", note: "`authorization_details` as an extra authorize parameter (a JSON value).",
|
|
82
|
+
proof: [platform("tests/oauth-connect.test.ts", "authorization_details (RFC 9396)")],
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
id: "client.private-key-jwt", role: "client", title: "Signed client assertion (private_key_jwt)", spec: "RFC 7523 §2.2",
|
|
86
|
+
status: "planned", note: "Part E: Entra certificates, Okta, FAPI.",
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
id: "client.device", role: "client", title: "Device authorization", spec: "RFC 8628",
|
|
90
|
+
status: "planned", note: "Part F: `foldrun connect --device`.",
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
id: "client.token-exchange", role: "client", title: "Token exchange", spec: "RFC 8693",
|
|
94
|
+
status: "planned", note: "Part I: a `token-exchange` secret kind. Cloud STS is covered today by the keyless identity kind.",
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
id: "client.dpop", role: "client", title: "DPoP sender-constrained tokens", spec: "RFC 9449",
|
|
98
|
+
status: "planned", note: "Part H: Bluesky/atproto, Okta DPoP.",
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
id: "client.mtls", role: "client", title: "Mutual-TLS client auth and bound tokens", spec: "RFC 8705",
|
|
102
|
+
status: "planned", note: "Part H: the client-cert secret at the token endpoint.",
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
id: "client.ciba", role: "client", title: "Client-initiated backchannel authentication", spec: "OpenID CIBA",
|
|
106
|
+
status: "not-needed", note: "No provider an agent needs uses it.",
|
|
107
|
+
},
|
|
108
|
+
// ------------------------------------------------------------ server (/api/mcp)
|
|
109
|
+
{
|
|
110
|
+
id: "server.authorization-code-pkce", role: "server", title: "Authorization code with PKCE (S256)", spec: "OAuth 2.1, MCP authorization",
|
|
111
|
+
status: "supported", note: "How claude.ai and other AI apps sign in to /api/mcp.",
|
|
112
|
+
proof: [web("tests/mcp-oauth.test.ts", "the code: PKCE, once, for its own app and redirect"), web("tests/mcp-sdk-oauth.test.ts", "the MCP SDK: 401 → discovery → registration")],
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
id: "server.refresh-rotation", role: "server", title: "Refresh rotation, reuse revokes", spec: "OAuth 2.1 §4.3.1",
|
|
116
|
+
status: "supported", note: "A refresh token used twice ends the connection.",
|
|
117
|
+
proof: [web("tests/mcp-oauth.test.ts", "refresh rotates; a refresh token used twice ends the connection")],
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
id: "server.client-auth", role: "server", title: "Client secret (Basic or body), or a public app", spec: "RFC 6749 §2.3.1",
|
|
121
|
+
status: "supported", note: "Registered apps choose `none`, `client_secret_post` or `client_secret_basic`.",
|
|
122
|
+
proof: [web("tests/mcp-oauth.test.ts", "a confidential app proves itself with its secret")],
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
id: "server.metadata", role: "server", title: "Authorization server and resource metadata", spec: "RFC 8414, RFC 9728",
|
|
126
|
+
status: "supported", note: "`/.well-known/oauth-authorization-server` and `/.well-known/oauth-protected-resource`.",
|
|
127
|
+
proof: [web("tests/mcp-oauth.test.ts", "discovery: the resource names its server")],
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
id: "server.registration", role: "server", title: "Dynamic registration and client metadata documents", spec: "RFC 7591, CIMD",
|
|
131
|
+
status: "supported", note: "An app registers itself, or names itself by an https URL.",
|
|
132
|
+
proof: [web("tests/mcp-oauth.test.ts", "registration: https or loopback redirect URIs only")],
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
id: "server.resource-indicators", role: "server", title: "Resource indicators", spec: "RFC 8707",
|
|
136
|
+
status: "supported", note: "Tokens are bound to /api/mcp.",
|
|
137
|
+
proof: [web("tests/mcp-oauth.test.ts", "the access token opens /api/mcp and its tools")],
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
id: "server.iss-parameter", role: "server", title: "Issuer in the authorization response", spec: "RFC 9207",
|
|
141
|
+
status: "supported", note: "Every answer carries `iss`.",
|
|
142
|
+
proof: [web("tests/mcp-oauth.test.ts", "the approval page's request")],
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
id: "server.revocation", role: "server", title: "Revocation endpoint", spec: "RFC 7009",
|
|
146
|
+
status: "supported", note: "`POST /api/mcp/oauth/revoke`: an app ends its own connection.",
|
|
147
|
+
proof: [web("tests/mcp-oauth.test.ts", "revocation (RFC 7009): advertised")],
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
id: "server.client-credentials", role: "server", title: "Client credentials for machine clients", spec: "RFC 6749 §4.4",
|
|
151
|
+
status: "planned", note: "Part G: machine MCP clients an admin registers.",
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
id: "server.private-key-jwt", role: "server", title: "Signed client assertion (private_key_jwt)", spec: "RFC 7523 §2.2",
|
|
155
|
+
status: "planned", note: "Part E: apps that declare `jwks` or `jwks_uri`.",
|
|
156
|
+
},
|
|
157
|
+
// ------------------------------------------------------------ refused, both roles
|
|
158
|
+
{
|
|
159
|
+
id: "refused.implicit", role: "client", title: "Implicit grant", spec: "RFC 9700 §2.1.2",
|
|
160
|
+
status: "refused", note: "Tokens in the browser's URL leak; OAuth 2.1 removes it.",
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
id: "refused.password", role: "client", title: "Resource owner password grant", spec: "RFC 9700 §2.4",
|
|
164
|
+
status: "refused", note: "foldrun never holds a person's password for another service.",
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
id: "refused.pkce-plain", role: "server", title: "PKCE `plain`", spec: "RFC 9700 §2.1.1",
|
|
168
|
+
status: "refused", note: "Only S256 is accepted: `plain` protects nothing if the request is seen.",
|
|
169
|
+
proof: [web("tests/mcp-oauth.test.ts", "the approval page's request")],
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
id: "refused.wildcard-redirect", role: "server", title: "Wildcard or prefix redirect matching", spec: "RFC 9700 §4.1",
|
|
173
|
+
status: "refused", note: "Redirect URIs match exactly (a loopback port may vary).",
|
|
174
|
+
proof: [web("tests/mcp-oauth.test.ts", "the approval page's request")],
|
|
175
|
+
},
|
|
176
|
+
];
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { TokenAuth } from "./oauth-token.ts";
|
|
2
|
+
/** What foldrun reads from a metadata document. */
|
|
3
|
+
export interface OAuthServerMetadata {
|
|
4
|
+
issuer: string;
|
|
5
|
+
authorization_endpoint?: string;
|
|
6
|
+
token_endpoint?: string;
|
|
7
|
+
revocation_endpoint?: string;
|
|
8
|
+
device_authorization_endpoint?: string;
|
|
9
|
+
pushed_authorization_request_endpoint?: string;
|
|
10
|
+
require_pushed_authorization_requests?: boolean;
|
|
11
|
+
token_endpoint_auth_methods_supported?: string[];
|
|
12
|
+
code_challenge_methods_supported?: string[];
|
|
13
|
+
authorization_response_iss_parameter_supported?: boolean;
|
|
14
|
+
dpop_signing_alg_values_supported?: string[];
|
|
15
|
+
scopes_supported?: string[];
|
|
16
|
+
jwks_uri?: string;
|
|
17
|
+
}
|
|
18
|
+
/** A discovered server, as the fields a saved app and an oauth2 secret take. */
|
|
19
|
+
export interface DiscoveredClientConfig {
|
|
20
|
+
issuer: string;
|
|
21
|
+
authorize_url?: string;
|
|
22
|
+
token_url: string;
|
|
23
|
+
revocation_url?: string;
|
|
24
|
+
device_url?: string;
|
|
25
|
+
par_url?: string;
|
|
26
|
+
/** The server requires PAR: an authorize URL without a request_uri fails. */
|
|
27
|
+
par_required?: boolean;
|
|
28
|
+
/** Whether the server sends `iss` with every authorization response
|
|
29
|
+
* (RFC 9207): when true, one that comes back without it is refused. */
|
|
30
|
+
iss_parameter: boolean;
|
|
31
|
+
/** How the client should authenticate at the token URL, given whether it
|
|
32
|
+
* has a secret and what the server supports; absent is the form body. */
|
|
33
|
+
token_auth?: TokenAuth;
|
|
34
|
+
/** Where the metadata was read from. */
|
|
35
|
+
metadata_url: string;
|
|
36
|
+
metadata: OAuthServerMetadata;
|
|
37
|
+
}
|
|
38
|
+
/** Loopback is exempt: a server on this machine (tests, a local Keycloak)
|
|
39
|
+
* crosses no wire for http to leak on. Judged on the parsed URL, never on
|
|
40
|
+
* how it starts: `http://localhost:80@evil.example` is evil.example
|
|
41
|
+
* (Codex review, 1 Nov 2026). */
|
|
42
|
+
export declare function isSecureUrl(url: string): boolean;
|
|
43
|
+
/** Every URL the metadata may be at, in the order to try. */
|
|
44
|
+
export declare function metadataUrls(issuer: string): string[];
|
|
45
|
+
/** Why an issuer cannot be discovered from, or null. */
|
|
46
|
+
export declare function issuerProblem(issuer: string): string | null;
|
|
47
|
+
type Fetcher = (url: string, init?: RequestInit) => Promise<Response>;
|
|
48
|
+
/**
|
|
49
|
+
* Read the authorization server's metadata. `fetcher` is how the caller
|
|
50
|
+
* reaches the network (the platform's SSRF-guarded fetchPublic, the CLI's
|
|
51
|
+
* fetch). Throws with every place tried when none answers.
|
|
52
|
+
*/
|
|
53
|
+
export declare function fetchOAuthMetadata(issuer: string, fetcher: Fetcher): Promise<{
|
|
54
|
+
url: string;
|
|
55
|
+
metadata: OAuthServerMetadata;
|
|
56
|
+
}>;
|
|
57
|
+
/**
|
|
58
|
+
* What a client config should say for this server. `hasSecret`: the app
|
|
59
|
+
* has a client secret; `pkce`: false when the caller turned PKCE off;
|
|
60
|
+
* `tokenAuth`: one the caller chose, which wins over the server's list.
|
|
61
|
+
*/
|
|
62
|
+
export declare function clientConfigFromMetadata(found: {
|
|
63
|
+
url: string;
|
|
64
|
+
metadata: OAuthServerMetadata;
|
|
65
|
+
}, opts?: {
|
|
66
|
+
hasSecret: boolean;
|
|
67
|
+
pkce?: boolean;
|
|
68
|
+
tokenAuth?: TokenAuth;
|
|
69
|
+
needsAuthorize?: boolean;
|
|
70
|
+
}): DiscoveredClientConfig;
|
|
71
|
+
/**
|
|
72
|
+
* The token_auth to use. A choice the caller made is kept. Otherwise, with a
|
|
73
|
+
* secret: the form body when the server lists it (what every secret did
|
|
74
|
+
* before), else Basic — which is also the RFC 8414 default when the server
|
|
75
|
+
* lists nothing. Without a secret: none (a public client).
|
|
76
|
+
*/
|
|
77
|
+
export declare function pickTokenAuth(supported: unknown, opts: {
|
|
78
|
+
hasSecret: boolean;
|
|
79
|
+
tokenAuth?: TokenAuth;
|
|
80
|
+
}): {
|
|
81
|
+
token_auth?: TokenAuth;
|
|
82
|
+
};
|
|
83
|
+
/** Discover and turn into a client config in one call. */
|
|
84
|
+
export declare function discoverOAuth(issuer: string, fetcher: Fetcher, opts: {
|
|
85
|
+
hasSecret: boolean;
|
|
86
|
+
pkce?: boolean;
|
|
87
|
+
tokenAuth?: TokenAuth;
|
|
88
|
+
needsAuthorize?: boolean;
|
|
89
|
+
}): Promise<DiscoveredClientConfig>;
|
|
90
|
+
export {};
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// Discovery from an issuer: give foldrun the provider's issuer URL and it
|
|
2
|
+
// reads the authorization server's own metadata (RFC 8414, OpenID Connect
|
|
3
|
+
// Discovery 1.0) instead of a person typing the authorize, token and
|
|
4
|
+
// revocation URLs by hand — where a typo costs a consent, and an endpoint
|
|
5
|
+
// on the wrong host is a mix-up attack waiting to happen.
|
|
6
|
+
//
|
|
7
|
+
// Where the metadata lives, in the order tried (RFC 8414 §3.1 and §5):
|
|
8
|
+
// https://host/.well-known/oauth-authorization-server{/path}
|
|
9
|
+
// https://host/.well-known/openid-configuration{/path}
|
|
10
|
+
// https://host{/path}/.well-known/openid-configuration (OIDC's own spelling)
|
|
11
|
+
// The document's `issuer` must equal the issuer asked for, exactly (RFC 8414
|
|
12
|
+
// §3.3) — a document that names another issuer is refused, not trusted.
|
|
13
|
+
/** Loopback is exempt: a server on this machine (tests, a local Keycloak)
|
|
14
|
+
* crosses no wire for http to leak on. Judged on the parsed URL, never on
|
|
15
|
+
* how it starts: `http://localhost:80@evil.example` is evil.example
|
|
16
|
+
* (Codex review, 1 Nov 2026). */
|
|
17
|
+
export function isSecureUrl(url) {
|
|
18
|
+
let u;
|
|
19
|
+
try {
|
|
20
|
+
u = new URL(url);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
if (u.username || u.password)
|
|
26
|
+
return false;
|
|
27
|
+
if (u.protocol === "https:")
|
|
28
|
+
return true;
|
|
29
|
+
return u.protocol === "http:" && ["127.0.0.1", "localhost", "[::1]"].includes(u.hostname);
|
|
30
|
+
}
|
|
31
|
+
/** Every URL the metadata may be at, in the order to try. */
|
|
32
|
+
export function metadataUrls(issuer) {
|
|
33
|
+
const u = new URL(issuer);
|
|
34
|
+
const pathPart = u.pathname.replace(/\/+$/, "");
|
|
35
|
+
const origin = u.origin;
|
|
36
|
+
const urls = [
|
|
37
|
+
`${origin}/.well-known/oauth-authorization-server${pathPart}`,
|
|
38
|
+
`${origin}/.well-known/openid-configuration${pathPart}`,
|
|
39
|
+
];
|
|
40
|
+
if (pathPart)
|
|
41
|
+
urls.push(`${origin}${pathPart}/.well-known/openid-configuration`);
|
|
42
|
+
return [...new Set(urls)];
|
|
43
|
+
}
|
|
44
|
+
/** Why an issuer cannot be discovered from, or null. */
|
|
45
|
+
export function issuerProblem(issuer) {
|
|
46
|
+
let u;
|
|
47
|
+
try {
|
|
48
|
+
u = new URL(issuer);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return "issuer must be a URL, like https://login.example.com";
|
|
52
|
+
}
|
|
53
|
+
if (!isSecureUrl(issuer))
|
|
54
|
+
return "issuer must be https";
|
|
55
|
+
if (u.search || u.hash)
|
|
56
|
+
return "issuer has no query or fragment (RFC 8414 §2)";
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Read the authorization server's metadata. `fetcher` is how the caller
|
|
61
|
+
* reaches the network (the platform's SSRF-guarded fetchPublic, the CLI's
|
|
62
|
+
* fetch). Throws with every place tried when none answers.
|
|
63
|
+
*/
|
|
64
|
+
export async function fetchOAuthMetadata(issuer, fetcher) {
|
|
65
|
+
const problem = issuerProblem(issuer);
|
|
66
|
+
if (problem)
|
|
67
|
+
throw new Error(problem);
|
|
68
|
+
const tried = [];
|
|
69
|
+
for (const url of metadataUrls(issuer)) {
|
|
70
|
+
let res;
|
|
71
|
+
try {
|
|
72
|
+
res = await fetcher(url, { headers: { accept: "application/json" }, redirect: "error", signal: AbortSignal.timeout(10_000) });
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
tried.push(`${url} (${err instanceof Error ? err.message : String(err)})`);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (!res.ok) {
|
|
79
|
+
await res.body?.cancel().catch(() => { });
|
|
80
|
+
tried.push(`${url} (${res.status})`);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
let doc;
|
|
84
|
+
try {
|
|
85
|
+
doc = await res.json();
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
tried.push(`${url} (not JSON)`);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (!doc || typeof doc !== "object" || Array.isArray(doc)) {
|
|
92
|
+
tried.push(`${url} (not a metadata document)`);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const metadata = doc;
|
|
96
|
+
// RFC 8414 §3.3: the issuer in the document is the one asked for, or
|
|
97
|
+
// the document is not used. Trailing-slash differences are not forgiven.
|
|
98
|
+
if (metadata.issuer !== issuer) {
|
|
99
|
+
throw new Error(`the metadata at ${url} names the issuer ${JSON.stringify(metadata.issuer ?? null)}, not ${issuer} — refused (RFC 8414 §3.3)`);
|
|
100
|
+
}
|
|
101
|
+
return { url, metadata };
|
|
102
|
+
}
|
|
103
|
+
throw new Error(`no OAuth metadata for ${issuer} — tried ${tried.join(", ")}`);
|
|
104
|
+
}
|
|
105
|
+
const ENDPOINTS = [
|
|
106
|
+
"authorization_endpoint", "token_endpoint", "revocation_endpoint",
|
|
107
|
+
"device_authorization_endpoint", "pushed_authorization_request_endpoint", "jwks_uri",
|
|
108
|
+
];
|
|
109
|
+
/**
|
|
110
|
+
* What a client config should say for this server. `hasSecret`: the app
|
|
111
|
+
* has a client secret; `pkce`: false when the caller turned PKCE off;
|
|
112
|
+
* `tokenAuth`: one the caller chose, which wins over the server's list.
|
|
113
|
+
*/
|
|
114
|
+
export function clientConfigFromMetadata(found, opts = { hasSecret: true }) {
|
|
115
|
+
const m = found.metadata;
|
|
116
|
+
for (const k of ENDPOINTS) {
|
|
117
|
+
const v = m[k];
|
|
118
|
+
if (v !== undefined && (typeof v !== "string" || !isSecureUrl(v)))
|
|
119
|
+
throw new Error(`the server's ${k} is not an https URL: ${String(v)}`);
|
|
120
|
+
}
|
|
121
|
+
if (!m.token_endpoint)
|
|
122
|
+
throw new Error(`${m.issuer} publishes no token_endpoint`);
|
|
123
|
+
if ((opts.needsAuthorize ?? true) && !m.authorization_endpoint)
|
|
124
|
+
throw new Error(`${m.issuer} publishes no authorization_endpoint — it does not do browser sign-in`);
|
|
125
|
+
// PKCE S256 is required of every connection (RFC 9700 §2.1.1). A server
|
|
126
|
+
// that lists its methods and leaves S256 out would ignore our challenge.
|
|
127
|
+
if (opts.pkce !== false && Array.isArray(m.code_challenge_methods_supported) && !m.code_challenge_methods_supported.includes("S256")) {
|
|
128
|
+
throw new Error(`${m.issuer} does not support PKCE S256 (it lists ${m.code_challenge_methods_supported.join(", ") || "none"}) — refused; turn PKCE off only if you are sure (--no-pkce)`);
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
issuer: m.issuer,
|
|
132
|
+
...(m.authorization_endpoint ? { authorize_url: m.authorization_endpoint } : {}),
|
|
133
|
+
token_url: m.token_endpoint,
|
|
134
|
+
...(m.revocation_endpoint ? { revocation_url: m.revocation_endpoint } : {}),
|
|
135
|
+
...(m.device_authorization_endpoint ? { device_url: m.device_authorization_endpoint } : {}),
|
|
136
|
+
...(m.pushed_authorization_request_endpoint ? { par_url: m.pushed_authorization_request_endpoint } : {}),
|
|
137
|
+
...(m.require_pushed_authorization_requests === true ? { par_required: true } : {}),
|
|
138
|
+
iss_parameter: m.authorization_response_iss_parameter_supported === true,
|
|
139
|
+
...pickTokenAuth(m.token_endpoint_auth_methods_supported, opts),
|
|
140
|
+
metadata_url: found.url,
|
|
141
|
+
metadata: m,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The token_auth to use. A choice the caller made is kept. Otherwise, with a
|
|
146
|
+
* secret: the form body when the server lists it (what every secret did
|
|
147
|
+
* before), else Basic — which is also the RFC 8414 default when the server
|
|
148
|
+
* lists nothing. Without a secret: none (a public client).
|
|
149
|
+
*/
|
|
150
|
+
export function pickTokenAuth(supported, opts) {
|
|
151
|
+
if (opts.tokenAuth)
|
|
152
|
+
return { token_auth: opts.tokenAuth };
|
|
153
|
+
const list = Array.isArray(supported) ? supported.filter((s) => typeof s === "string") : null;
|
|
154
|
+
// A public client sends no secret. Not refused when the list leaves out
|
|
155
|
+
// `none`: Keycloak takes public clients and lists only secret methods,
|
|
156
|
+
// so the list is not the last word on it (found by the conformance suite).
|
|
157
|
+
if (!opts.hasSecret)
|
|
158
|
+
return {};
|
|
159
|
+
if (list && list.includes("client_secret_post"))
|
|
160
|
+
return {};
|
|
161
|
+
if (!list || list.includes("client_secret_basic"))
|
|
162
|
+
return { token_auth: "client_secret_basic" };
|
|
163
|
+
throw new Error(`the server takes none of the ways foldrun signs in at its token URL (it lists ${list.join(", ") || "nothing"}; foldrun sends the secret in the form body or as HTTP Basic)`);
|
|
164
|
+
}
|
|
165
|
+
/** Discover and turn into a client config in one call. */
|
|
166
|
+
export async function discoverOAuth(issuer, fetcher, opts) {
|
|
167
|
+
return clientConfigFromMetadata(await fetchOAuthMetadata(issuer, fetcher), opts);
|
|
168
|
+
}
|
|
@@ -17,8 +17,13 @@ export declare function tokenAuthProblem(client: {
|
|
|
17
17
|
* fields (grant_type, code, refresh_token, …); the client's identity is
|
|
18
18
|
* added here, by its token_auth. Fields in `params` named client_id or
|
|
19
19
|
* client_secret are dropped — the method decides those, never an `extra`.
|
|
20
|
+
* `clientIdInBody`: client_id goes in the form whatever the method — a
|
|
21
|
+
* pushed authorization request carries it as a parameter of the request it
|
|
22
|
+
* describes even when the client authenticates with Basic (RFC 9126 §2.1).
|
|
20
23
|
*/
|
|
21
|
-
export declare function oauthTokenRequest(client: TokenClient, params: Record<string, string
|
|
24
|
+
export declare function oauthTokenRequest(client: TokenClient, params: Record<string, string>, opts?: {
|
|
25
|
+
clientIdInBody?: boolean;
|
|
26
|
+
}): {
|
|
22
27
|
headers: Record<string, string>;
|
|
23
28
|
body: string;
|
|
24
29
|
};
|
package/dist/src/oauth-token.js
CHANGED
|
@@ -51,8 +51,11 @@ const formEncode = (s) => new URLSearchParams({ x: s }).toString().slice(2);
|
|
|
51
51
|
* fields (grant_type, code, refresh_token, …); the client's identity is
|
|
52
52
|
* added here, by its token_auth. Fields in `params` named client_id or
|
|
53
53
|
* client_secret are dropped — the method decides those, never an `extra`.
|
|
54
|
+
* `clientIdInBody`: client_id goes in the form whatever the method — a
|
|
55
|
+
* pushed authorization request carries it as a parameter of the request it
|
|
56
|
+
* describes even when the client authenticates with Basic (RFC 9126 §2.1).
|
|
54
57
|
*/
|
|
55
|
-
export function oauthTokenRequest(client, params) {
|
|
58
|
+
export function oauthTokenRequest(client, params, opts = {}) {
|
|
56
59
|
const problem = tokenAuthProblem(client);
|
|
57
60
|
if (problem)
|
|
58
61
|
throw new Error(problem);
|
|
@@ -73,6 +76,8 @@ export function oauthTokenRequest(client, params) {
|
|
|
73
76
|
// not the body as well.
|
|
74
77
|
const pair = `${formEncode(client.client_id)}:${formEncode(client.client_secret)}`;
|
|
75
78
|
headers.authorization = `Basic ${Buffer.from(pair, "utf8").toString("base64")}`;
|
|
79
|
+
if (opts.clientIdInBody)
|
|
80
|
+
form.set("client_id", client.client_id);
|
|
76
81
|
}
|
|
77
82
|
else {
|
|
78
83
|
form.set("client_id", client.client_id);
|
package/dist/src/secrets.d.ts
CHANGED
|
@@ -245,6 +245,9 @@ export interface OAuth2Config {
|
|
|
245
245
|
/** Plain fields the provider returned beside the first token (Salesforce's
|
|
246
246
|
* instance_url) — kept with the recipe, never a credential. */
|
|
247
247
|
fields?: Record<string, string>;
|
|
248
|
+
/** The provider's revocation endpoint (RFC 7009): deleting or replacing
|
|
249
|
+
* this secret revokes its tokens there (revokeOAuth2). */
|
|
250
|
+
revocation_url?: string;
|
|
248
251
|
}
|
|
249
252
|
/** Tries at a token endpoint before a refresh is called failed: the first,
|
|
250
253
|
* and two more after a short wait, for a network error, a 5xx or a 429. */
|
|
@@ -271,6 +274,54 @@ export declare function tokenCacheKey(kind: "oauth2" | "sa", storedValue: string
|
|
|
271
274
|
* older token over a newer one. Returns whether a copy was replaced.
|
|
272
275
|
*/
|
|
273
276
|
export declare function replaceOAuth2RefreshToken(tenant: string, name: string, workspace: string | undefined, usedValue: string, refreshToken: string): boolean;
|
|
277
|
+
/** The oauth2 recipe stored at exactly this scope — never the account copy a
|
|
278
|
+
* workspace would fall back to: revoking is about the copy being removed. */
|
|
279
|
+
export declare function oauth2ValueAt(tenant: string, name: string, workspace?: string): string | null;
|
|
280
|
+
/**
|
|
281
|
+
* Another stored secret of this account holds a grant revoking this one could
|
|
282
|
+
* end: the same refresh token (a copy made by hand, or one rotation reached),
|
|
283
|
+
* or any grant from the same app (token URL and client id). Providers such as
|
|
284
|
+
* Google revoke every token of a user's grant to an app together, so deleting
|
|
285
|
+
* the Ads connection would take down the Search Console one made with the
|
|
286
|
+
* same app (Codex review, 1 Nov 2026). Such a grant is left alone.
|
|
287
|
+
*/
|
|
288
|
+
export declare function oauthGrantSharedElsewhere(tenant: string, name: string, workspace: string | undefined, refreshToken: string, app?: {
|
|
289
|
+
token_url: string;
|
|
290
|
+
client_id: string;
|
|
291
|
+
}): boolean;
|
|
292
|
+
/** What revoking an oauth2 secret's tokens came to. */
|
|
293
|
+
export interface OAuthRevokeResult {
|
|
294
|
+
/** Nothing was asked: no revocation endpoint; another secret holds the
|
|
295
|
+
* same grant or one from the same app; the replacement is from the same
|
|
296
|
+
* app (a reconnect: revoking would end the new grant too at providers
|
|
297
|
+
* that revoke per app); or the stored recipe could not be read. */
|
|
298
|
+
skipped?: "no-revocation-url" | "shared-grant" | "same-app" | "unreadable";
|
|
299
|
+
/** The token types the provider accepted the revocation of. */
|
|
300
|
+
revoked: ("refresh_token" | "access_token")[];
|
|
301
|
+
/** Why one failed — reported, never a reason to keep a secret. */
|
|
302
|
+
error?: string;
|
|
303
|
+
}
|
|
304
|
+
type RevokeFetcher = (url: string, init: RequestInit) => Promise<Response>;
|
|
305
|
+
/**
|
|
306
|
+
* Revoke what an oauth2 recipe holds at its provider (RFC 7009): the refresh
|
|
307
|
+
* token, then the access token this process has cached for it. The client
|
|
308
|
+
* authenticates as at the token URL (its token_auth). A 200 is success —
|
|
309
|
+
* the RFC answers 200 for a token already invalid too; anything else is
|
|
310
|
+
* reported in `error`, and the next token is still tried. `replacement`: the
|
|
311
|
+
* recipe taking this one's place; one from the same app (token URL and client
|
|
312
|
+
* id) means nothing is revoked, since a provider that revokes per app would
|
|
313
|
+
* end the new grant with the old.
|
|
314
|
+
*/
|
|
315
|
+
export declare function revokeOAuth2Value(value: string, opts?: {
|
|
316
|
+
tenant?: string;
|
|
317
|
+
name?: string;
|
|
318
|
+
workspace?: string;
|
|
319
|
+
fetcher?: RevokeFetcher;
|
|
320
|
+
replacement?: {
|
|
321
|
+
token_url: string;
|
|
322
|
+
client_id: string;
|
|
323
|
+
};
|
|
324
|
+
}): Promise<OAuthRevokeResult>;
|
|
274
325
|
/**
|
|
275
326
|
* Swap every oauth2 recipe in an env map for a live access token. The one
|
|
276
327
|
* async moment in secret resolution, kept out of the sync paths: callers
|