oidc-remote 0.0.0-stage → 0.1.0-rc2
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/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/cli.d.ts +16 -0
- package/dist/cli.js +135 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/proxy.d.ts +10 -0
- package/dist/proxy.js +78 -0
- package/dist/stdio.d.ts +13 -0
- package/dist/stdio.js +35 -0
- package/dist/token.d.ts +40 -0
- package/dist/token.js +110 -0
- package/package.json +52 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rafael Gaspar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# oidc-remote
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Attach a live OAuth2 access token on the hop to a remote HTTPS server.
|
|
4
|
+
|
|
5
|
+
`oidc-remote` is a **local** process (or library) that gets and refreshes an access token, then injects it as `Authorization: Bearer`. It does not implement an OpenID Provider, and it is not a full OIDC relying party.
|
|
6
|
+
|
|
7
|
+
The token request is `client_credentials` with a JWT `client_assertion` (for example a projected Kubernetes ServiceAccount token). Any token endpoint that accepts that grant works.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install oidc-remote
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Node.js 22+.
|
|
16
|
+
|
|
17
|
+
## Library
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { TokenClient } from "oidc-remote";
|
|
21
|
+
|
|
22
|
+
const tokens = new TokenClient({
|
|
23
|
+
tokenUrl: "https://idp.example/token",
|
|
24
|
+
clientId: "cluster-mcp.example.test",
|
|
25
|
+
assertionFile: "/var/run/secrets/tokens/oidc",
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const accessToken = await tokens.getAccessToken();
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`getAccessToken()` reads the assertion file on each exchange (the projected token is rotated in place), POSTs `grant_type=client_credentials` with `client_assertion`, and caches the access token until 80% of `expires_in`. A failed exchange throws `TokenExchangeError` — it does not mint a fallback token.
|
|
32
|
+
|
|
33
|
+
Default scope is `openid profile entitlements`. Default assertion path is `/var/run/secrets/tokens/oidc`.
|
|
34
|
+
|
|
35
|
+
Callers that already speak HTTPS should use `TokenClient` directly and keep the real hostname on the request so TLS SNI and `:authority` match the gateway.
|
|
36
|
+
|
|
37
|
+
## CLI
|
|
38
|
+
|
|
39
|
+
### `stdio`
|
|
40
|
+
|
|
41
|
+
For MCP clients that spawn a command (Cursor `mcp.json`, Claude `mcpServers`):
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
"cluster": {
|
|
45
|
+
"command": "oidc-remote",
|
|
46
|
+
"args": [
|
|
47
|
+
"stdio",
|
|
48
|
+
"--gateway", "cluster-mcp.example.test",
|
|
49
|
+
"--token-url", "https://idp.example/token"
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Stdio to the parent, Streamable HTTP to `https://<gateway>/mcp` (MCP SDK transports, same hop as mcp-remote). `authProvider.token()` runs before every HTTP request so the Bearer can refresh. SNI and Host are the gateway hostname.
|
|
55
|
+
|
|
56
|
+
### `proxy`
|
|
57
|
+
|
|
58
|
+
For HTTP clients that freeze a URL or API key at construct time:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
oidc-remote proxy \
|
|
62
|
+
--gateway ai.example.test \
|
|
63
|
+
--token-url https://idp.example/token \
|
|
64
|
+
--listen 127.0.0.1:8080
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Point the app at `http://127.0.0.1:8080` (plaintext loopback). The proxy injects Bearer and forwards to `https://<gateway>` with that hostname as SNI/Host.
|
|
68
|
+
|
|
69
|
+
### Flags
|
|
70
|
+
|
|
71
|
+
| Flag | Env | Default |
|
|
72
|
+
|---|---|---|
|
|
73
|
+
| `--gateway` | | *(required)* hostname; also the OAuth `client_id` unless `--client-id` is set |
|
|
74
|
+
| `--token-url` | `OIDC_REMOTE_TOKEN_URL` | *(required)* |
|
|
75
|
+
| `--assertion-file` | `OIDC_REMOTE_ASSERTION_FILE` | `/var/run/secrets/tokens/oidc` |
|
|
76
|
+
| `--scope` | `OIDC_REMOTE_SCOPE` | `openid profile entitlements` |
|
|
77
|
+
| `--client-id` | | `--gateway` |
|
|
78
|
+
| `--url` | | `https://<gateway>/mcp` (`stdio`) |
|
|
79
|
+
| `--mcp-path` | | `/mcp` |
|
|
80
|
+
| `--upstream` | | `https://<gateway>` (`proxy`) |
|
|
81
|
+
| `--listen` | | `127.0.0.1:8080` (`proxy`) |
|
|
82
|
+
|
|
83
|
+
## What this is not
|
|
84
|
+
|
|
85
|
+
- Not an OIDC stack. No authorization-code browser flow, no DCR, no PKCE.
|
|
86
|
+
- Assertion JWTs never go to the gateway. They are only exchanged at the token URL.
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
MIT
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
export type Command = "stdio" | "proxy";
|
|
3
|
+
export type CliOptions = {
|
|
4
|
+
command: Command;
|
|
5
|
+
gateway: string;
|
|
6
|
+
clientId: string;
|
|
7
|
+
tokenUrl: string;
|
|
8
|
+
assertionFile: string;
|
|
9
|
+
scope: string;
|
|
10
|
+
url: string;
|
|
11
|
+
upstream: string;
|
|
12
|
+
listenHost: string;
|
|
13
|
+
listenPort: number;
|
|
14
|
+
};
|
|
15
|
+
export declare function parseArgs(argv: string[], env?: NodeJS.ProcessEnv): CliOptions;
|
|
16
|
+
export declare function main(argv?: string[], env?: NodeJS.ProcessEnv): Promise<void>;
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { pathToFileURL } from "node:url";
|
|
3
|
+
import { createProxyServer, listen } from "./proxy.js";
|
|
4
|
+
import { runStdio } from "./stdio.js";
|
|
5
|
+
import { DEFAULT_ASSERTION_FILE, DEFAULT_SCOPE, TokenClient } from "./token.js";
|
|
6
|
+
function nextValue(argv, i, flag) {
|
|
7
|
+
const value = argv[i];
|
|
8
|
+
if (value === undefined || value.length === 0)
|
|
9
|
+
throw new Error(`${flag} requires a value`);
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
function envValue(env, name) {
|
|
13
|
+
const value = env[name];
|
|
14
|
+
return value && value.length > 0 ? value : undefined;
|
|
15
|
+
}
|
|
16
|
+
function parseListen(value) {
|
|
17
|
+
const idx = value.lastIndexOf(":");
|
|
18
|
+
if (idx <= 0)
|
|
19
|
+
throw new Error(`--listen requires host:port, got ${value}`);
|
|
20
|
+
const host = value.slice(0, idx);
|
|
21
|
+
const port = Number(value.slice(idx + 1));
|
|
22
|
+
if (!Number.isInteger(port) || port < 0)
|
|
23
|
+
throw new Error(`--listen has an invalid port: ${value}`);
|
|
24
|
+
return { host, port };
|
|
25
|
+
}
|
|
26
|
+
export function parseArgs(argv, env = process.env) {
|
|
27
|
+
const command = argv[0];
|
|
28
|
+
if (command !== "stdio" && command !== "proxy") {
|
|
29
|
+
throw new Error("usage: oidc-remote <stdio|proxy> --gateway <hostname> --token-url <url>");
|
|
30
|
+
}
|
|
31
|
+
let gateway = "";
|
|
32
|
+
let clientId = "";
|
|
33
|
+
let tokenUrl = envValue(env, "OIDC_REMOTE_TOKEN_URL") ?? "";
|
|
34
|
+
let assertionFile = envValue(env, "OIDC_REMOTE_ASSERTION_FILE") ?? DEFAULT_ASSERTION_FILE;
|
|
35
|
+
let scope = envValue(env, "OIDC_REMOTE_SCOPE") ?? DEFAULT_SCOPE;
|
|
36
|
+
let url = "";
|
|
37
|
+
let mcpPath = "/mcp";
|
|
38
|
+
let upstream = "";
|
|
39
|
+
let listenHost = "127.0.0.1";
|
|
40
|
+
let listenPort = 8080;
|
|
41
|
+
const rest = argv.slice(1);
|
|
42
|
+
for (let i = 0; i < rest.length; i++) {
|
|
43
|
+
const arg = rest[i] ?? "";
|
|
44
|
+
if (arg === "--gateway")
|
|
45
|
+
gateway = nextValue(rest, ++i, "--gateway");
|
|
46
|
+
else if (arg.startsWith("--gateway="))
|
|
47
|
+
gateway = arg.slice("--gateway=".length);
|
|
48
|
+
else if (arg === "--client-id")
|
|
49
|
+
clientId = nextValue(rest, ++i, "--client-id");
|
|
50
|
+
else if (arg.startsWith("--client-id="))
|
|
51
|
+
clientId = arg.slice("--client-id=".length);
|
|
52
|
+
else if (arg === "--token-url")
|
|
53
|
+
tokenUrl = nextValue(rest, ++i, "--token-url");
|
|
54
|
+
else if (arg.startsWith("--token-url="))
|
|
55
|
+
tokenUrl = arg.slice("--token-url=".length);
|
|
56
|
+
else if (arg === "--assertion-file")
|
|
57
|
+
assertionFile = nextValue(rest, ++i, "--assertion-file");
|
|
58
|
+
else if (arg.startsWith("--assertion-file="))
|
|
59
|
+
assertionFile = arg.slice("--assertion-file=".length);
|
|
60
|
+
else if (arg === "--scope")
|
|
61
|
+
scope = nextValue(rest, ++i, "--scope");
|
|
62
|
+
else if (arg.startsWith("--scope="))
|
|
63
|
+
scope = arg.slice("--scope=".length);
|
|
64
|
+
else if (arg === "--url")
|
|
65
|
+
url = nextValue(rest, ++i, "--url");
|
|
66
|
+
else if (arg.startsWith("--url="))
|
|
67
|
+
url = arg.slice("--url=".length);
|
|
68
|
+
else if (arg === "--mcp-path")
|
|
69
|
+
mcpPath = nextValue(rest, ++i, "--mcp-path");
|
|
70
|
+
else if (arg.startsWith("--mcp-path="))
|
|
71
|
+
mcpPath = arg.slice("--mcp-path=".length);
|
|
72
|
+
else if (arg === "--upstream")
|
|
73
|
+
upstream = nextValue(rest, ++i, "--upstream");
|
|
74
|
+
else if (arg.startsWith("--upstream="))
|
|
75
|
+
upstream = arg.slice("--upstream=".length);
|
|
76
|
+
else if (arg === "--listen") {
|
|
77
|
+
const parsed = parseListen(nextValue(rest, ++i, "--listen"));
|
|
78
|
+
listenHost = parsed.host;
|
|
79
|
+
listenPort = parsed.port;
|
|
80
|
+
}
|
|
81
|
+
else if (arg.startsWith("--listen=")) {
|
|
82
|
+
const parsed = parseListen(arg.slice("--listen=".length));
|
|
83
|
+
listenHost = parsed.host;
|
|
84
|
+
listenPort = parsed.port;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (!gateway)
|
|
88
|
+
throw new Error("--gateway is required");
|
|
89
|
+
if (!tokenUrl)
|
|
90
|
+
throw new Error("--token-url or OIDC_REMOTE_TOKEN_URL is required");
|
|
91
|
+
if (!mcpPath.startsWith("/"))
|
|
92
|
+
mcpPath = `/${mcpPath}`;
|
|
93
|
+
const id = clientId || gateway;
|
|
94
|
+
return {
|
|
95
|
+
command,
|
|
96
|
+
gateway,
|
|
97
|
+
clientId: id,
|
|
98
|
+
tokenUrl,
|
|
99
|
+
assertionFile,
|
|
100
|
+
scope,
|
|
101
|
+
url: url || `https://${gateway}${mcpPath}`,
|
|
102
|
+
upstream: upstream || `https://${gateway}`,
|
|
103
|
+
listenHost,
|
|
104
|
+
listenPort,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export async function main(argv = process.argv.slice(2), env = process.env) {
|
|
108
|
+
const options = parseArgs(argv, env);
|
|
109
|
+
const tokens = new TokenClient({
|
|
110
|
+
tokenUrl: options.tokenUrl,
|
|
111
|
+
clientId: options.clientId,
|
|
112
|
+
assertionFile: options.assertionFile,
|
|
113
|
+
scope: options.scope,
|
|
114
|
+
});
|
|
115
|
+
if (options.command === "stdio") {
|
|
116
|
+
await runStdio({
|
|
117
|
+
url: options.url,
|
|
118
|
+
getAccessToken: () => tokens.getAccessToken(),
|
|
119
|
+
});
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const server = createProxyServer({
|
|
123
|
+
upstream: options.upstream,
|
|
124
|
+
getAccessToken: () => tokens.getAccessToken(),
|
|
125
|
+
});
|
|
126
|
+
const addr = await listen(server, options.listenHost, options.listenPort);
|
|
127
|
+
console.error(`oidc-remote proxy listening on http://${addr.host}:${addr.port} -> ${options.upstream}`);
|
|
128
|
+
}
|
|
129
|
+
const entry = process.argv[1] ? pathToFileURL(process.argv[1]).href : undefined;
|
|
130
|
+
if (import.meta.url === entry) {
|
|
131
|
+
main().catch((err) => {
|
|
132
|
+
console.error(err instanceof Error ? err.message : err);
|
|
133
|
+
process.exit(1);
|
|
134
|
+
});
|
|
135
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { createTokenClient, DEFAULT_ASSERTION_FILE, DEFAULT_SCOPE, JWT_BEARER_ASSERTION_TYPE, TokenClient, TokenExchangeError, type TokenClientOptions, } from "./token.js";
|
|
2
|
+
export { createProxyServer, listen, type ProxyOptions } from "./proxy.js";
|
|
3
|
+
export { runStdio, type StdioOptions } from "./stdio.js";
|
package/dist/index.js
ADDED
package/dist/proxy.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import http from "node:http";
|
|
2
|
+
export type ProxyOptions = {
|
|
3
|
+
upstream: string;
|
|
4
|
+
getAccessToken: () => Promise<string>;
|
|
5
|
+
};
|
|
6
|
+
export declare function createProxyServer(options: ProxyOptions): http.Server;
|
|
7
|
+
export declare function listen(server: http.Server, host: string, port: number): Promise<{
|
|
8
|
+
host: string;
|
|
9
|
+
port: number;
|
|
10
|
+
}>;
|
package/dist/proxy.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import http from "node:http";
|
|
2
|
+
import https from "node:https";
|
|
3
|
+
import { URL } from "node:url";
|
|
4
|
+
const HOP_BY_HOP = new Set([
|
|
5
|
+
"connection",
|
|
6
|
+
"keep-alive",
|
|
7
|
+
"proxy-authenticate",
|
|
8
|
+
"proxy-authorization",
|
|
9
|
+
"te",
|
|
10
|
+
"trailers",
|
|
11
|
+
"transfer-encoding",
|
|
12
|
+
"upgrade",
|
|
13
|
+
"host",
|
|
14
|
+
"authorization",
|
|
15
|
+
]);
|
|
16
|
+
export function createProxyServer(options) {
|
|
17
|
+
const upstream = new URL(options.upstream);
|
|
18
|
+
const isHttps = upstream.protocol === "https:";
|
|
19
|
+
const port = upstream.port ? Number(upstream.port) : isHttps ? 443 : 80;
|
|
20
|
+
return http.createServer((req, res) => {
|
|
21
|
+
void (async () => {
|
|
22
|
+
try {
|
|
23
|
+
const token = await options.getAccessToken();
|
|
24
|
+
const headers = {};
|
|
25
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
26
|
+
if (HOP_BY_HOP.has(key.toLowerCase()))
|
|
27
|
+
continue;
|
|
28
|
+
headers[key] = value;
|
|
29
|
+
}
|
|
30
|
+
headers.host = upstream.host;
|
|
31
|
+
headers.authorization = `Bearer ${token}`;
|
|
32
|
+
const requestFn = isHttps ? https.request : http.request;
|
|
33
|
+
const upstreamReq = requestFn({
|
|
34
|
+
protocol: upstream.protocol,
|
|
35
|
+
hostname: upstream.hostname,
|
|
36
|
+
port,
|
|
37
|
+
method: req.method,
|
|
38
|
+
path: req.url,
|
|
39
|
+
headers,
|
|
40
|
+
servername: isHttps ? upstream.hostname : undefined,
|
|
41
|
+
}, (upstreamRes) => {
|
|
42
|
+
const outHeaders = {};
|
|
43
|
+
for (const [key, value] of Object.entries(upstreamRes.headers)) {
|
|
44
|
+
if (key.toLowerCase() === "transfer-encoding")
|
|
45
|
+
continue;
|
|
46
|
+
outHeaders[key] = value;
|
|
47
|
+
}
|
|
48
|
+
res.writeHead(upstreamRes.statusCode ?? 502, outHeaders);
|
|
49
|
+
upstreamRes.pipe(res);
|
|
50
|
+
});
|
|
51
|
+
upstreamReq.on("error", (err) => {
|
|
52
|
+
if (!res.headersSent)
|
|
53
|
+
res.writeHead(502);
|
|
54
|
+
res.end(err instanceof Error ? err.message : "upstream error");
|
|
55
|
+
});
|
|
56
|
+
req.pipe(upstreamReq);
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
if (!res.headersSent)
|
|
60
|
+
res.writeHead(502);
|
|
61
|
+
res.end(err instanceof Error ? err.message : "proxy error");
|
|
62
|
+
}
|
|
63
|
+
})();
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
export function listen(server, host, port) {
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
server.once("error", reject);
|
|
69
|
+
server.listen(port, host, () => {
|
|
70
|
+
const addr = server.address();
|
|
71
|
+
if (!addr || typeof addr === "string") {
|
|
72
|
+
reject(new Error("failed to bind"));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
resolve({ host: addr.address, port: addr.port });
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
package/dist/stdio.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Readable, Writable } from "node:stream";
|
|
2
|
+
export type StdioOptions = {
|
|
3
|
+
url: string;
|
|
4
|
+
getAccessToken: () => Promise<string>;
|
|
5
|
+
stdin?: Readable;
|
|
6
|
+
stdout?: Writable;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Same hop as mcp-remote: MCP SDK stdio transport on the parent, Streamable
|
|
10
|
+
* HTTP to the gateway. `authProvider.token()` runs before every HTTP request
|
|
11
|
+
* so the Bearer can refresh.
|
|
12
|
+
*/
|
|
13
|
+
export declare function runStdio(options: StdioOptions): Promise<void>;
|
package/dist/stdio.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/client";
|
|
2
|
+
import { StdioServerTransport } from "@modelcontextprotocol/server/stdio";
|
|
3
|
+
/**
|
|
4
|
+
* Same hop as mcp-remote: MCP SDK stdio transport on the parent, Streamable
|
|
5
|
+
* HTTP to the gateway. `authProvider.token()` runs before every HTTP request
|
|
6
|
+
* so the Bearer can refresh.
|
|
7
|
+
*/
|
|
8
|
+
export async function runStdio(options) {
|
|
9
|
+
const remote = new StreamableHTTPClientTransport(new URL(options.url), {
|
|
10
|
+
authProvider: { token: options.getAccessToken },
|
|
11
|
+
onInsufficientScope: "throw",
|
|
12
|
+
});
|
|
13
|
+
const local = new StdioServerTransport(options.stdin, options.stdout);
|
|
14
|
+
const report = (err) => {
|
|
15
|
+
console.error(err instanceof Error ? err.message : err);
|
|
16
|
+
};
|
|
17
|
+
local.onmessage = (message) => {
|
|
18
|
+
void remote.send(message).catch(report);
|
|
19
|
+
};
|
|
20
|
+
remote.onmessage = (message) => {
|
|
21
|
+
void local.send(message).catch(report);
|
|
22
|
+
};
|
|
23
|
+
local.onerror = report;
|
|
24
|
+
remote.onerror = report;
|
|
25
|
+
await remote.start();
|
|
26
|
+
await local.start();
|
|
27
|
+
await new Promise((resolve) => {
|
|
28
|
+
local.onclose = () => {
|
|
29
|
+
void remote.close().finally(resolve);
|
|
30
|
+
};
|
|
31
|
+
remote.onclose = () => {
|
|
32
|
+
void local.close();
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
}
|
package/dist/token.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export declare const JWT_BEARER_ASSERTION_TYPE = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer";
|
|
2
|
+
export declare const DEFAULT_SCOPE = "openid profile entitlements";
|
|
3
|
+
/** Default projected JWT used as client_assertion. */
|
|
4
|
+
export declare const DEFAULT_ASSERTION_FILE = "/var/run/secrets/tokens/oidc";
|
|
5
|
+
export type TokenClientOptions = {
|
|
6
|
+
tokenUrl: string;
|
|
7
|
+
clientId: string;
|
|
8
|
+
assertionFile?: string;
|
|
9
|
+
scope?: string;
|
|
10
|
+
fetch?: typeof fetch;
|
|
11
|
+
readFile?: (path: string) => Promise<string>;
|
|
12
|
+
now?: () => number;
|
|
13
|
+
/** Refresh when this fraction of `expires_in` has elapsed. */
|
|
14
|
+
refreshRatio?: number;
|
|
15
|
+
};
|
|
16
|
+
export declare class TokenExchangeError extends Error {
|
|
17
|
+
readonly status?: number;
|
|
18
|
+
readonly body?: string;
|
|
19
|
+
constructor(message: string, opts?: {
|
|
20
|
+
status?: number;
|
|
21
|
+
body?: string;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export declare class TokenClient {
|
|
25
|
+
private readonly tokenUrl;
|
|
26
|
+
private readonly clientId;
|
|
27
|
+
private readonly assertionFile;
|
|
28
|
+
private readonly scope;
|
|
29
|
+
private readonly fetchFn;
|
|
30
|
+
private readonly readFile;
|
|
31
|
+
private readonly now;
|
|
32
|
+
private readonly refreshRatio;
|
|
33
|
+
private cached;
|
|
34
|
+
private inflight;
|
|
35
|
+
constructor(options: TokenClientOptions);
|
|
36
|
+
invalidate(): void;
|
|
37
|
+
getAccessToken(): Promise<string>;
|
|
38
|
+
private exchange;
|
|
39
|
+
}
|
|
40
|
+
export declare function createTokenClient(options: TokenClientOptions): TokenClient;
|
package/dist/token.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
export const JWT_BEARER_ASSERTION_TYPE = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer";
|
|
2
|
+
export const DEFAULT_SCOPE = "openid profile entitlements";
|
|
3
|
+
/** Default projected JWT used as client_assertion. */
|
|
4
|
+
export const DEFAULT_ASSERTION_FILE = "/var/run/secrets/tokens/oidc";
|
|
5
|
+
export class TokenExchangeError extends Error {
|
|
6
|
+
status;
|
|
7
|
+
body;
|
|
8
|
+
constructor(message, opts) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "TokenExchangeError";
|
|
11
|
+
this.status = opts?.status;
|
|
12
|
+
this.body = opts?.body;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export class TokenClient {
|
|
16
|
+
tokenUrl;
|
|
17
|
+
clientId;
|
|
18
|
+
assertionFile;
|
|
19
|
+
scope;
|
|
20
|
+
fetchFn;
|
|
21
|
+
readFile;
|
|
22
|
+
now;
|
|
23
|
+
refreshRatio;
|
|
24
|
+
cached = null;
|
|
25
|
+
inflight = null;
|
|
26
|
+
constructor(options) {
|
|
27
|
+
if (!options.tokenUrl)
|
|
28
|
+
throw new Error("tokenUrl is required");
|
|
29
|
+
if (!options.clientId)
|
|
30
|
+
throw new Error("clientId is required");
|
|
31
|
+
this.tokenUrl = options.tokenUrl;
|
|
32
|
+
this.clientId = options.clientId;
|
|
33
|
+
this.assertionFile = options.assertionFile ?? DEFAULT_ASSERTION_FILE;
|
|
34
|
+
this.scope = options.scope ?? DEFAULT_SCOPE;
|
|
35
|
+
this.fetchFn = options.fetch ?? fetch;
|
|
36
|
+
this.readFile =
|
|
37
|
+
options.readFile ??
|
|
38
|
+
(async (path) => {
|
|
39
|
+
const { readFile } = await import("node:fs/promises");
|
|
40
|
+
return readFile(path, "utf8");
|
|
41
|
+
});
|
|
42
|
+
this.now = options.now ?? (() => Date.now() / 1000);
|
|
43
|
+
this.refreshRatio = options.refreshRatio ?? 0.8;
|
|
44
|
+
}
|
|
45
|
+
invalidate() {
|
|
46
|
+
this.cached = null;
|
|
47
|
+
}
|
|
48
|
+
async getAccessToken() {
|
|
49
|
+
const now = this.now();
|
|
50
|
+
if (this.cached && now < this.cached.refreshAt)
|
|
51
|
+
return this.cached.token;
|
|
52
|
+
if (this.inflight)
|
|
53
|
+
return this.inflight;
|
|
54
|
+
this.inflight = this.exchange().finally(() => {
|
|
55
|
+
this.inflight = null;
|
|
56
|
+
});
|
|
57
|
+
return this.inflight;
|
|
58
|
+
}
|
|
59
|
+
async exchange() {
|
|
60
|
+
let assertion;
|
|
61
|
+
try {
|
|
62
|
+
assertion = (await this.readFile(this.assertionFile)).trim();
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
const why = err instanceof Error ? err.message : String(err);
|
|
66
|
+
throw new TokenExchangeError(`failed to read assertion file ${this.assertionFile}: ${why}`);
|
|
67
|
+
}
|
|
68
|
+
if (!assertion) {
|
|
69
|
+
throw new TokenExchangeError(`assertion file ${this.assertionFile} is empty`);
|
|
70
|
+
}
|
|
71
|
+
const body = new URLSearchParams({
|
|
72
|
+
grant_type: "client_credentials",
|
|
73
|
+
client_id: this.clientId,
|
|
74
|
+
client_assertion_type: JWT_BEARER_ASSERTION_TYPE,
|
|
75
|
+
client_assertion: assertion,
|
|
76
|
+
scope: this.scope,
|
|
77
|
+
});
|
|
78
|
+
const res = await this.fetchFn(this.tokenUrl, {
|
|
79
|
+
method: "POST",
|
|
80
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
81
|
+
body,
|
|
82
|
+
});
|
|
83
|
+
const text = await res.text();
|
|
84
|
+
if (!res.ok) {
|
|
85
|
+
throw new TokenExchangeError(`token endpoint HTTP ${res.status}`, {
|
|
86
|
+
status: res.status,
|
|
87
|
+
body: text.slice(0, 400),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
let parsed;
|
|
91
|
+
try {
|
|
92
|
+
parsed = JSON.parse(text);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
throw new TokenExchangeError("token endpoint returned non-JSON", { body: text.slice(0, 400) });
|
|
96
|
+
}
|
|
97
|
+
if (typeof parsed.access_token !== "string" || !parsed.access_token) {
|
|
98
|
+
throw new TokenExchangeError("token endpoint omitted access_token", { body: text.slice(0, 400) });
|
|
99
|
+
}
|
|
100
|
+
const expiresIn = typeof parsed.expires_in === "number" && parsed.expires_in > 0 ? parsed.expires_in : 3600;
|
|
101
|
+
this.cached = {
|
|
102
|
+
token: parsed.access_token,
|
|
103
|
+
refreshAt: this.now() + expiresIn * this.refreshRatio,
|
|
104
|
+
};
|
|
105
|
+
return parsed.access_token;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export function createTokenClient(options) {
|
|
109
|
+
return new TokenClient(options);
|
|
110
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oidc-remote",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-rc2",
|
|
4
|
+
"description": "Attach a live OAuth2 access token on the hop to a remote HTTPS server (library, stdio, proxy).",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/rafaelgaspar/oidc-remote"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://github.com/rafaelgaspar/oidc-remote",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/rafaelgaspar/oidc-remote/issues"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"bin": {
|
|
16
|
+
"oidc-remote": "dist/cli.js"
|
|
17
|
+
},
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=22"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@modelcontextprotocol/client": "^2.3.1",
|
|
34
|
+
"@modelcontextprotocol/server": "^2.3.1"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc -p tsconfig.build.json",
|
|
38
|
+
"typecheck": "tsc --noEmit",
|
|
39
|
+
"test": "tsx --test 'src/**/*.test.ts'"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"oauth2",
|
|
43
|
+
"oidc",
|
|
44
|
+
"mcp",
|
|
45
|
+
"authentik",
|
|
46
|
+
"kubernetes",
|
|
47
|
+
"client-assertion"
|
|
48
|
+
],
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "24.19.2",
|
|
51
|
+
"tsx": "4.23.15",
|
|
52
|
+
"typescript": "7.0.2"
|
|
53
|
+
}
|
|
54
|
+
}
|