@introspection-sdk/introspection-node 0.5.5 → 0.5.7
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/.turbo/turbo-build.log +1 -1
- package/dist/auth.d.ts +127 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +112 -0
- package/dist/auth.js.map +1 -0
- package/dist/client.d.ts +30 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +35 -0
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/auth.ts +236 -0
- package/src/client.ts +38 -0
- package/src/index.ts +14 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
|
|
2
|
-
> @introspection-sdk/introspection-node@0.5.
|
|
2
|
+
> @introspection-sdk/introspection-node@0.5.7 build /home/runner/work/introspection-js-sdk/introspection-js-sdk/packages/introspection-node
|
|
3
3
|
> tsc
|
|
4
4
|
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CP `POST /v1/oauth/token` response. No refresh token is issued for the
|
|
3
|
+
* machine grants — re-mint when it expires.
|
|
4
|
+
*/
|
|
5
|
+
export interface OAuthToken {
|
|
6
|
+
/** Project-scoped RS256 access token (`Authorization: Bearer …`). */
|
|
7
|
+
access_token: string;
|
|
8
|
+
/** Always `"Bearer"`. */
|
|
9
|
+
token_type: string;
|
|
10
|
+
/** Token lifetime in seconds. */
|
|
11
|
+
expires_in: number;
|
|
12
|
+
/** The granted (scope-capped) scope, when the CP returns one. */
|
|
13
|
+
scope: string | null;
|
|
14
|
+
/**
|
|
15
|
+
* Data Plane API base URL for the token's project, resolved by the CP the
|
|
16
|
+
* same way it is for the CLI login. `null` when the deployment can't be
|
|
17
|
+
* resolved; the caller then needs an explicit DP URL. Hand this to the
|
|
18
|
+
* browser SDK as `dpUrl` so the SPA connects without separate DP config.
|
|
19
|
+
*/
|
|
20
|
+
dp_url: string | null;
|
|
21
|
+
}
|
|
22
|
+
export interface ServiceAccountTokenParams {
|
|
23
|
+
/** Confidential Application client id (`intro_app_…`). */
|
|
24
|
+
clientId: string;
|
|
25
|
+
/** Confidential Application client secret (`intro_sk_…`). */
|
|
26
|
+
clientSecret: string;
|
|
27
|
+
/**
|
|
28
|
+
* Project the token is scoped to. Required by the CP `client_credentials`
|
|
29
|
+
* grant — the minted token is project-scoped and the project must belong to
|
|
30
|
+
* the Application's organization.
|
|
31
|
+
*/
|
|
32
|
+
projectId: string;
|
|
33
|
+
/**
|
|
34
|
+
* Optional space-separated scope. Capped server-side to the Application's
|
|
35
|
+
* `allowed_scopes`; omit to receive the Application's default scope.
|
|
36
|
+
*/
|
|
37
|
+
scope?: string;
|
|
38
|
+
/**
|
|
39
|
+
* CP API base URL. Defaults to `INTROSPECTION_BASE_API_URL` or
|
|
40
|
+
* `https://api.introspection.dev`.
|
|
41
|
+
*/
|
|
42
|
+
baseApiUrl?: string;
|
|
43
|
+
/** Custom `fetch` (for tests or non-standard runtimes). */
|
|
44
|
+
fetch?: typeof fetch;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Back-compat alias: the `client_credentials` response is the same shape as
|
|
48
|
+
* every other `/v1/oauth/token` response.
|
|
49
|
+
*/
|
|
50
|
+
export type ServiceAccountToken = OAuthToken;
|
|
51
|
+
/**
|
|
52
|
+
* Mint a project-scoped CP access token from confidential service-account
|
|
53
|
+
* credentials. See {@link ServiceAccountTokenParams}.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const { access_token, dp_url } = await serviceAccountToken({
|
|
58
|
+
* clientId: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_ID!,
|
|
59
|
+
* clientSecret: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_SECRET!,
|
|
60
|
+
* projectId: process.env.INTRO_PROJECT_ID!,
|
|
61
|
+
* });
|
|
62
|
+
* const client = new IntrospectionClient({ token: access_token });
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function serviceAccountToken(params: ServiceAccountTokenParams): Promise<OAuthToken>;
|
|
66
|
+
export interface AuthorizationCodeParams {
|
|
67
|
+
/** The authorization code returned to the redirect URI. */
|
|
68
|
+
code: string;
|
|
69
|
+
/** Public SPA Application `client_id` (PKCE — no secret). */
|
|
70
|
+
clientId: string;
|
|
71
|
+
/** The redirect URI the code was issued for (must match the authorize call). */
|
|
72
|
+
redirectUri: string;
|
|
73
|
+
/** The PKCE `code_verifier` paired with the authorize-step challenge. */
|
|
74
|
+
codeVerifier: string;
|
|
75
|
+
/**
|
|
76
|
+
* CP API base URL. Defaults to `INTROSPECTION_BASE_API_URL` or
|
|
77
|
+
* `https://api.introspection.dev`.
|
|
78
|
+
*/
|
|
79
|
+
baseApiUrl?: string;
|
|
80
|
+
/** Custom `fetch` (for tests or non-standard runtimes). */
|
|
81
|
+
fetch?: typeof fetch;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* RFC 6749 / PKCE `authorization_code` exchange against CP
|
|
85
|
+
* `POST /v1/oauth/token`. Run it in your backend so the browser hosted-login
|
|
86
|
+
* flow does not hand-roll the token POST.
|
|
87
|
+
*/
|
|
88
|
+
export declare function authorizationCodeToken(params: AuthorizationCodeParams): Promise<OAuthToken>;
|
|
89
|
+
export interface TokenExchangeParams {
|
|
90
|
+
/** The end user's subject token (e.g. a partner-IdP `id_token`). */
|
|
91
|
+
subjectToken: string;
|
|
92
|
+
/** The federated Application's `client_id` (public client — no secret). */
|
|
93
|
+
clientId: string;
|
|
94
|
+
/** Project the minted DP token is scoped to. */
|
|
95
|
+
projectId: string;
|
|
96
|
+
/**
|
|
97
|
+
* The subject token's type URI. Defaults to
|
|
98
|
+
* `urn:ietf:params:oauth:token-type:id_token`.
|
|
99
|
+
*/
|
|
100
|
+
subjectTokenType?: string;
|
|
101
|
+
/** Optional space-separated scope, capped server-side. */
|
|
102
|
+
scope?: string;
|
|
103
|
+
/**
|
|
104
|
+
* CP API base URL. Defaults to `INTROSPECTION_BASE_API_URL` or
|
|
105
|
+
* `https://api.introspection.dev`.
|
|
106
|
+
*/
|
|
107
|
+
baseApiUrl?: string;
|
|
108
|
+
/** Custom `fetch` (for tests or non-standard runtimes). */
|
|
109
|
+
fetch?: typeof fetch;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* RFC 8693 token-exchange against CP `POST /v1/oauth/token`: trade an end
|
|
113
|
+
* user's partner-IdP token for a project-scoped DP access token for a
|
|
114
|
+
* `member_type=customer` member. Intended to run server-side in a broker (the
|
|
115
|
+
* subject token shouldn't be re-handled in the browser longer than needed).
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const { access_token, dp_url } = await tokenExchange({
|
|
120
|
+
* subjectToken: idTokenFromPartnerIdp,
|
|
121
|
+
* clientId: process.env.FEDERATED_CLIENT_ID!,
|
|
122
|
+
* projectId: process.env.INTRO_PROJECT_ID!,
|
|
123
|
+
* });
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export declare function tokenExchange(params: TokenExchangeParams): Promise<OAuthToken>;
|
|
127
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAyDA;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,qEAAqE;IACrE,YAAY,EAAE,MAAM,CAAC;IACrB,yBAAyB;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,iCAAiC;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,iEAAiE;IACjE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB;;;;;OAKG;IACH,MAA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H,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,2DAA2D;IAC3D,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,aAAa,CACjC,MAAM,EAAE,mBAAmB,GAC1B,OAAO,CAAC,UAAU,CAAC,CAcrB"}
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Service-account (machine) authentication for the Control Plane.
|
|
3
|
+
*
|
|
4
|
+
* Mints a short-lived, project-scoped Introspection access token via the
|
|
5
|
+
* OAuth 2.0 `client_credentials` grant against CP `POST /v1/oauth/token`.
|
|
6
|
+
* This is the headless / CI counterpart to a long-lived API key: the
|
|
7
|
+
* confidential Application's `client_id` + `client_secret` stay server-side,
|
|
8
|
+
* and you re-mint when the token expires (no refresh token is issued).
|
|
9
|
+
*
|
|
10
|
+
* The minted `access_token` is an ordinary CP bearer token, so it drops
|
|
11
|
+
* straight into the existing client surface — pass it to
|
|
12
|
+
* {@link IntrospectionClient}, or use
|
|
13
|
+
* {@link IntrospectionClient.fromServiceAccount} to mint and construct in one
|
|
14
|
+
* call, and the usual `client.runtimes("name").run()` flow works unchanged.
|
|
15
|
+
*/
|
|
16
|
+
import { stripTrailingSlash, toApiError } from "@introspection-sdk/http";
|
|
17
|
+
const DEFAULT_BASE_API_URL = "https://api.introspection.dev";
|
|
18
|
+
const GRANT_TYPE_CLIENT_CREDENTIALS = "client_credentials";
|
|
19
|
+
const GRANT_TYPE_TOKEN_EXCHANGE = "urn:ietf:params:oauth:grant-type:token-exchange";
|
|
20
|
+
const GRANT_TYPE_AUTHORIZATION_CODE = "authorization_code";
|
|
21
|
+
const SUBJECT_TOKEN_TYPE_ID_TOKEN = "urn:ietf:params:oauth:token-type:id_token";
|
|
22
|
+
function resolveBaseApiUrl(baseApiUrl) {
|
|
23
|
+
return stripTrailingSlash(baseApiUrl ??
|
|
24
|
+
process.env.INTROSPECTION_BASE_API_URL ??
|
|
25
|
+
DEFAULT_BASE_API_URL);
|
|
26
|
+
}
|
|
27
|
+
function resolveFetch(custom) {
|
|
28
|
+
const fetchImpl = custom ?? globalThis.fetch;
|
|
29
|
+
if (!fetchImpl) {
|
|
30
|
+
throw new Error("global fetch is unavailable; pass `fetch` or run on Node 18+");
|
|
31
|
+
}
|
|
32
|
+
return fetchImpl;
|
|
33
|
+
}
|
|
34
|
+
async function postTokenForm(baseApiUrl, form, fetchImpl) {
|
|
35
|
+
const res = await fetchImpl(`${baseApiUrl}/v1/oauth/token`, {
|
|
36
|
+
method: "POST",
|
|
37
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
38
|
+
body: form.toString(),
|
|
39
|
+
cache: "no-store",
|
|
40
|
+
});
|
|
41
|
+
if (!res.ok)
|
|
42
|
+
throw await toApiError(res);
|
|
43
|
+
return (await res.json());
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Mint a project-scoped CP access token from confidential service-account
|
|
47
|
+
* credentials. See {@link ServiceAccountTokenParams}.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const { access_token, dp_url } = await serviceAccountToken({
|
|
52
|
+
* clientId: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_ID!,
|
|
53
|
+
* clientSecret: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_SECRET!,
|
|
54
|
+
* projectId: process.env.INTRO_PROJECT_ID!,
|
|
55
|
+
* });
|
|
56
|
+
* const client = new IntrospectionClient({ token: access_token });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export async function serviceAccountToken(params) {
|
|
60
|
+
const form = new URLSearchParams({
|
|
61
|
+
grant_type: GRANT_TYPE_CLIENT_CREDENTIALS,
|
|
62
|
+
client_id: params.clientId,
|
|
63
|
+
client_secret: params.clientSecret,
|
|
64
|
+
project_id: params.projectId,
|
|
65
|
+
});
|
|
66
|
+
if (params.scope)
|
|
67
|
+
form.set("scope", params.scope);
|
|
68
|
+
return postTokenForm(resolveBaseApiUrl(params.baseApiUrl), form, resolveFetch(params.fetch));
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* RFC 6749 / PKCE `authorization_code` exchange against CP
|
|
72
|
+
* `POST /v1/oauth/token`. Run it in your backend so the browser hosted-login
|
|
73
|
+
* flow does not hand-roll the token POST.
|
|
74
|
+
*/
|
|
75
|
+
export async function authorizationCodeToken(params) {
|
|
76
|
+
const form = new URLSearchParams({
|
|
77
|
+
grant_type: GRANT_TYPE_AUTHORIZATION_CODE,
|
|
78
|
+
code: params.code,
|
|
79
|
+
client_id: params.clientId,
|
|
80
|
+
redirect_uri: params.redirectUri,
|
|
81
|
+
code_verifier: params.codeVerifier,
|
|
82
|
+
});
|
|
83
|
+
return postTokenForm(resolveBaseApiUrl(params.baseApiUrl), form, resolveFetch(params.fetch));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* RFC 8693 token-exchange against CP `POST /v1/oauth/token`: trade an end
|
|
87
|
+
* user's partner-IdP token for a project-scoped DP access token for a
|
|
88
|
+
* `member_type=customer` member. Intended to run server-side in a broker (the
|
|
89
|
+
* subject token shouldn't be re-handled in the browser longer than needed).
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const { access_token, dp_url } = await tokenExchange({
|
|
94
|
+
* subjectToken: idTokenFromPartnerIdp,
|
|
95
|
+
* clientId: process.env.FEDERATED_CLIENT_ID!,
|
|
96
|
+
* projectId: process.env.INTRO_PROJECT_ID!,
|
|
97
|
+
* });
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export async function tokenExchange(params) {
|
|
101
|
+
const form = new URLSearchParams({
|
|
102
|
+
grant_type: GRANT_TYPE_TOKEN_EXCHANGE,
|
|
103
|
+
subject_token: params.subjectToken,
|
|
104
|
+
subject_token_type: params.subjectTokenType ?? SUBJECT_TOKEN_TYPE_ID_TOKEN,
|
|
105
|
+
client_id: params.clientId,
|
|
106
|
+
project_id: params.projectId,
|
|
107
|
+
});
|
|
108
|
+
if (params.scope)
|
|
109
|
+
form.set("scope", params.scope);
|
|
110
|
+
return postTokenForm(resolveBaseApiUrl(params.baseApiUrl), form, resolveFetch(params.fetch));
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=auth.js.map
|
package/dist/auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,kBAAkB,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAEzE,MAAM,oBAAoB,GAAG,+BAA+B,CAAC;AAC7D,MAAM,6BAA6B,GAAG,oBAAoB,CAAC;AAC3D,MAAM,yBAAyB,GAC7B,iDAAiD,CAAC;AACpD,MAAM,6BAA6B,GAAG,oBAAoB,CAAC;AAC3D,MAAM,2BAA2B,GAAG,2CAA2C,CAAC;AAEhF,SAAS,iBAAiB,CAAC,UAAmB;IAC5C,OAAO,kBAAkB,CACvB,UAAU;QACR,OAAO,CAAC,GAAG,CAAC,0BAA0B;QACtC,oBAAoB,CACvB,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CAAC,MAAqB;IACzC,MAAM,SAAS,GAAG,MAAM,IAAI,UAAU,CAAC,KAAK,CAAC;IAC7C,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CACb,8DAA8D,CAC/D,CAAC;IACJ,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,KAAK,UAAU,aAAa,CAC1B,UAAkB,EAClB,IAAqB,EACrB,SAAuB;IAEvB,MAAM,GAAG,GAAG,MAAM,SAAS,CAAC,GAAG,UAAU,iBAAiB,EAAE;QAC1D,MAAM,EAAE,MAAM;QACd,OAAO,EAAE,EAAE,cAAc,EAAE,mCAAmC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;;;;;;GAcG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,MAA2B;IAE3B,MAAM,IAAI,GAAG,IAAI,eAAe,CAAC;QAC/B,UAAU,EAAE,yBAAyB;QACrC,aAAa,EAAE,MAAM,CAAC,YAAY;QAClC,kBAAkB,EAAE,MAAM,CAAC,gBAAgB,IAAI,2BAA2B;QAC1E,SAAS,EAAE,MAAM,CAAC,QAAQ;QAC1B,UAAU,EAAE,MAAM,CAAC,SAAS;KAC7B,CAAC,CAAC;IACH,IAAI,MAAM,CAAC,KAAK;QAAE,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;IAClD,OAAO,aAAa,CAClB,iBAAiB,CAAC,MAAM,CAAC,UAAU,CAAC,EACpC,IAAI,EACJ,YAAY,CAAC,MAAM,CAAC,KAAK,CAAC,CAC3B,CAAC;AACJ,CAAC"}
|
package/dist/client.d.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import type { AdvancedOptions } from "@introspection-sdk/types";
|
|
15
15
|
import type { IntrospectionClientOptions } from "./types.js";
|
|
16
|
+
import { type ServiceAccountTokenParams } from "./auth.js";
|
|
16
17
|
import { HttpClient } from "./http.js";
|
|
17
18
|
import { type RuntimeHandleFactory, type RuntimesApi } from "./resources/runtimes.js";
|
|
18
19
|
import { type ExperimentHandleFactory, type ExperimentsApi } from "./resources/experiments.js";
|
|
@@ -60,6 +61,35 @@ export declare class IntrospectionClient {
|
|
|
60
61
|
*/
|
|
61
62
|
readonly recipes: RecipesApi;
|
|
62
63
|
constructor(options?: IntrospectionClientOptions);
|
|
64
|
+
/**
|
|
65
|
+
* Authenticate as a confidential service account and return a ready
|
|
66
|
+
* client.
|
|
67
|
+
*
|
|
68
|
+
* Mints a short-lived, project-scoped CP access token via the
|
|
69
|
+
* `client_credentials` grant (see {@link serviceAccountToken}) and wires
|
|
70
|
+
* it in as the bearer token, so the runtime flow works exactly as it does
|
|
71
|
+
* with an API key:
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const client = await IntrospectionClient.fromServiceAccount({
|
|
76
|
+
* clientId: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_ID!,
|
|
77
|
+
* clientSecret: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_SECRET!,
|
|
78
|
+
* projectId: process.env.INTRO_PROJECT_ID!,
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* // Resolved fresh from the runtime name on every call.
|
|
82
|
+
* const runner = await client.runtimes("customer-agent").run({
|
|
83
|
+
* identity: { user_id: "u_demo" },
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* The token is not auto-refreshed: it lives for `expires_in` seconds, so
|
|
88
|
+
* re-mint (call this again) for long-lived processes once it lapses.
|
|
89
|
+
*/
|
|
90
|
+
static fromServiceAccount(params: ServiceAccountTokenParams & {
|
|
91
|
+
serviceName?: string;
|
|
92
|
+
}): Promise<IntrospectionClient>;
|
|
63
93
|
/** Close the underlying HTTP client. */
|
|
64
94
|
shutdown(): Promise<void>;
|
|
65
95
|
}
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAEhE,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAEL,KAAK,oBAAoB,EACzB,KAAK,WAAW,EACjB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,cAAc,EACpB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAiB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;;;;OAKG;IACH,QAAQ,CAAC,QAAQ,EAAE,WAAW,GAAG,oBAAoB,CAAC;IAEtD;;OAEG;IACH,QAAQ,CAAC,WAAW,EAAE,cAAc,GAAG,uBAAuB,CAAC;IAE/D;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC;gBAEjB,OAAO,GAAE,0BAA+B;IAgCpD;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;WACU,kBAAkB,CAC7B,MAAM,EAAE,yBAAyB,GAAG;QAAE,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,GAC3D,OAAO,CAAC,mBAAmB,CAAC;IAS/B,wCAAwC;IAClC,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAKhC"}
|
package/dist/client.js
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
* `@introspection-sdk/introspection-node/otel`.
|
|
13
13
|
*/
|
|
14
14
|
import { logger as sdkLogger } from "./utils.js";
|
|
15
|
+
import { serviceAccountToken } from "./auth.js";
|
|
15
16
|
import { HttpClient } from "./http.js";
|
|
16
17
|
import { attachRuntimes, } from "./resources/runtimes.js";
|
|
17
18
|
import { attachExperiments, } from "./resources/experiments.js";
|
|
@@ -82,6 +83,40 @@ export class IntrospectionClient {
|
|
|
82
83
|
this.recipes = attachRecipes(this.cpHttp);
|
|
83
84
|
sdkLogger.info(`IntrospectionClient initialized: api=${baseApiUrl}`);
|
|
84
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Authenticate as a confidential service account and return a ready
|
|
88
|
+
* client.
|
|
89
|
+
*
|
|
90
|
+
* Mints a short-lived, project-scoped CP access token via the
|
|
91
|
+
* `client_credentials` grant (see {@link serviceAccountToken}) and wires
|
|
92
|
+
* it in as the bearer token, so the runtime flow works exactly as it does
|
|
93
|
+
* with an API key:
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const client = await IntrospectionClient.fromServiceAccount({
|
|
98
|
+
* clientId: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_ID!,
|
|
99
|
+
* clientSecret: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_SECRET!,
|
|
100
|
+
* projectId: process.env.INTRO_PROJECT_ID!,
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* // Resolved fresh from the runtime name on every call.
|
|
104
|
+
* const runner = await client.runtimes("customer-agent").run({
|
|
105
|
+
* identity: { user_id: "u_demo" },
|
|
106
|
+
* });
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* The token is not auto-refreshed: it lives for `expires_in` seconds, so
|
|
110
|
+
* re-mint (call this again) for long-lived processes once it lapses.
|
|
111
|
+
*/
|
|
112
|
+
static async fromServiceAccount(params) {
|
|
113
|
+
const { access_token } = await serviceAccountToken(params);
|
|
114
|
+
return new IntrospectionClient({
|
|
115
|
+
token: access_token,
|
|
116
|
+
serviceName: params.serviceName,
|
|
117
|
+
advanced: { baseApiUrl: params.baseApiUrl, fetch: params.fetch },
|
|
118
|
+
});
|
|
119
|
+
}
|
|
85
120
|
/** Close the underlying HTTP client. */
|
|
86
121
|
async shutdown() {
|
|
87
122
|
// HttpClient has no persistent connections to close, but reserved
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAAE,MAAM,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAEjD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EACL,cAAc,GAGf,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,iBAAiB,GAGlB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,aAAa,EAAmB,MAAM,wBAAwB,CAAC;AAExE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,mBAAmB;IAC9B,2E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U;YAClB,KAAK;YACL,iBAAiB,EAAE,QAAQ,CAAC,iBAAiB;YAC7C,KAAK,EAAE,QAAQ,CAAC,KAAK;SACtB,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,GAAG,cAAc,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QAClD,IAAI,CAAC,WAAW,GAAG,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QACxD,IAAI,CAAC,OAAO,GAAG,aAAa,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAE1C,SAAS,CAAC,IAAI,CAAC,wCAAwC,UAAU,EAAE,CAAC,CAAC;IACvE,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,MAAM,CAAC,KAAK,CAAC,kBAAkB,CAC7B,MAA4D;QAE5D,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,mBAAmB,CAAC,MAAM,CAAC,CAAC;QAC3D,OAAO,IAAI,mBAAmB,CAAC;YAC7B,KAAK,EAAE,YAAY;YACnB,WAAW,EAAE,MAAM,CAAC,WAAW;YAC/B,QAAQ,EAAE,EAAE,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE;SACjE,CAAC,CAAC;IACL,CAAC;IAED,wCAAwC;IACxC,KAAK,CAAC,QAAQ;QACZ,kEAAkE;QAClE,gDAAgD;QAChD,SAAS,CAAC,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC3D,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
* from `@introspection-sdk/introspection-node/otel`.
|
|
10
10
|
*/
|
|
11
11
|
export { IntrospectionClient } from "./client.js";
|
|
12
|
+
export { serviceAccountToken, tokenExchange, authorizationCodeToken, } from "./auth.js";
|
|
13
|
+
export type { ServiceAccountTokenParams, ServiceAccountToken, TokenExchangeParams, AuthorizationCodeParams, OAuthToken, } from "./auth.js";
|
|
12
14
|
export type { AdvancedOptions, IntrospectionClientOptions, FeedbackOptions, UserTraits, } from "./types.js";
|
|
13
15
|
export { ConversationItemsApi, ConversationsApi, FileVersionsApi, FilesApi, RunHandle, SharesApi, TaskRunsApi, TasksApi, } from "@introspection-sdk/http";
|
|
14
16
|
export type { FileUploadBody, StartParams } from "@introspection-sdk/http";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAGlD,YAAY,EACV,eAAe,EACf,0BAA0B,EAC1B,eAAe,EACf,UAAU,GACX,MAAM,YAAY,CAAC;AAIpB,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,eAAe,EACf,QAAQ,EACR,SAAS,EACT,SAAS,EACT,WAAW,EACX,QAAQ,GACT,MAAM,yBAAyB,CAAC;AACjC,YAAY,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,YAAY,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAG1C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EACL,WAAW,EACX,aAAa,EACb,cAAc,EACd,MAAM,GACP,MAAM,yBAAyB,CAAC;AACjC,YAAY,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AACpE,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,4BAA4B,CAAC;AACpC,YAAY,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAC1E,OAA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;AAClC,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,sBAAsB,EACtB,kBAAkB,EAClB,aAAa,EACb,aAAa,EACb,eAAe,EACf,cAAc,EACd,uBAAuB,EACvB,WAAW,EACX,YAAY,EACZ,oBAAoB,GACrB,MAAM,0BAA0B,CAAC;AAGlC,YAAY,EACV,eAAe,EACf,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,QAAQ,EACR,mBAAmB,EACnB,oBAAoB,EACpB,cAAc,EACd,YAAY,EACZ,cAAc,EACd,WAAW,GACZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAGxD,OAAO,EACL,+BAA+B,EAC/B,gCAAgC,EAChC,8BAA8B,EAC9B,qCAAqC,GACtC,MAAM,wBAAwB,CAAC;AAChC,YAAY,EACV,iBAAiB,EACjB,kBAAkB,EAClB,YAAY,EACZ,aAAa,EACb,QAAQ,IAAI,cAAc,GAC3B,MAAM,wBAAwB,CAAC;AAGhC,OAAO,EACL,oCAAoC,EACpC,uCAAuC,EACvC,uCAAuC,EACvC,mCAAmC,GACpC,MAAM,wBAAwB,CAAC;AAChC,YAAY,EACV,eAAe,EACf,aAAa,EACb,yBAAyB,EACzB,UAAU,EACV,UAAU,GACX,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
*/
|
|
11
11
|
// REST client.
|
|
12
12
|
export { IntrospectionClient } from "./client.js";
|
|
13
|
+
// Machine / federated / hosted-login OAuth token helpers.
|
|
14
|
+
export { serviceAccountToken, tokenExchange, authorizationCodeToken, } from "./auth.js";
|
|
13
15
|
// Runner-bound REST namespaces (runner.tasks, runner.files,
|
|
14
16
|
// runner.conversations) + HTTP/SSE.
|
|
15
17
|
export { ConversationItemsApi, ConversationsApi, FileVersionsApi, FilesApi, RunHandle, SharesApi, TaskRunsApi, TasksApi, } from "@introspection-sdk/http";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,eAAe;AACf,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,eAAe;AACf,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD,0DAA0D;AAC1D,OAAO,EACL,mBAAmB,EACnB,aAAa,EACb,sBAAsB,GACvB,MAAM,WAAW,CAAC;AAiBnB,4DAA4D;AAC5D,oCAAoC;AACpC,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,eAAe,EACf,QAAQ,EACR,SAAS,EACT,SAAS,EACT,WAAW,EACX,QAAQ,GACT,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEvC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,yBAAyB;AACzB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EACL,WAAW,EACX,aAAa,EACb,cAAc,EACd,MAAM,GACP,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAgEnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,sBAAsB,EACtB,kBAAkB,EAClB,aAAa,EACb,aAAa,EACb,eAAe,EACf,cAAc,EACd,uBAAuB,EACvB,WAAW,EACX,YAAY,EACZ,oBAAoB,GACrB,MAAM,0BAA0B,CAAC;AAiBlC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD,kEAAkE;AAClE,OAAO,EACL,+BAA+B,EAC/B,gCAAgC,EAChC,8BAA8B,EAC9B,qCAAqC,GACtC,MAAM,wBAAwB,CAAC;AAShC,2DAA2D;AAC3D,OAAO,EACL,oCAAoC,EACpC,uCAAuC,EACvC,uCAAuC,EACvC,mCAAmC,GACpC,MAAM,wBAAwB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@introspection-sdk/introspection-node",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.7",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "Introspection observability SDK for Node.js",
|
|
6
6
|
"engines": {
|
|
@@ -139,10 +139,10 @@
|
|
|
139
139
|
"@opentelemetry/api": "^1.9.1",
|
|
140
140
|
"https-proxy-agent": "^7.0.6",
|
|
141
141
|
"proxy-from-env": "^2.1.0",
|
|
142
|
-
"@introspection-sdk/types": "0.5.
|
|
143
|
-
"@introspection-sdk/http": "0.5.
|
|
144
|
-
"@introspection-sdk/introspection-
|
|
145
|
-
"@introspection-sdk/introspection-
|
|
142
|
+
"@introspection-sdk/types": "0.5.7",
|
|
143
|
+
"@introspection-sdk/http": "0.5.7",
|
|
144
|
+
"@introspection-sdk/introspection-proxy": "0.5.7",
|
|
145
|
+
"@introspection-sdk/introspection-pi": "0.5.7"
|
|
146
146
|
},
|
|
147
147
|
"scripts": {
|
|
148
148
|
"lint": "eslint . --max-warnings 0",
|
package/src/auth.ts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Service-account (machine) authentication for the Control Plane.
|
|
3
|
+
*
|
|
4
|
+
* Mints a short-lived, project-scoped Introspection access token via the
|
|
5
|
+
* OAuth 2.0 `client_credentials` grant against CP `POST /v1/oauth/token`.
|
|
6
|
+
* This is the headless / CI counterpart to a long-lived API key: the
|
|
7
|
+
* confidential Application's `client_id` + `client_secret` stay server-side,
|
|
8
|
+
* and you re-mint when the token expires (no refresh token is issued).
|
|
9
|
+
*
|
|
10
|
+
* The minted `access_token` is an ordinary CP bearer token, so it drops
|
|
11
|
+
* straight into the existing client surface — pass it to
|
|
12
|
+
* {@link IntrospectionClient}, or use
|
|
13
|
+
* {@link IntrospectionClient.fromServiceAccount} to mint and construct in one
|
|
14
|
+
* call, and the usual `client.runtimes("name").run()` flow works unchanged.
|
|
15
|
+
*/
|
|
16
|
+
import { stripTrailingSlash, toApiError } from "@introspection-sdk/http";
|
|
17
|
+
|
|
18
|
+
const DEFAULT_BASE_API_URL = "https://api.introspection.dev";
|
|
19
|
+
const GRANT_TYPE_CLIENT_CREDENTIALS = "client_credentials";
|
|
20
|
+
const GRANT_TYPE_TOKEN_EXCHANGE =
|
|
21
|
+
"urn:ietf:params:oauth:grant-type:token-exchange";
|
|
22
|
+
const GRANT_TYPE_AUTHORIZATION_CODE = "authorization_code";
|
|
23
|
+
const SUBJECT_TOKEN_TYPE_ID_TOKEN = "urn:ietf:params:oauth:token-type:id_token";
|
|
24
|
+
|
|
25
|
+
function resolveBaseApiUrl(baseApiUrl?: string): string {
|
|
26
|
+
return stripTrailingSlash(
|
|
27
|
+
baseApiUrl ??
|
|
28
|
+
process.env.INTROSPECTION_BASE_API_URL ??
|
|
29
|
+
DEFAULT_BASE_API_URL,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function resolveFetch(custom?: typeof fetch): typeof fetch {
|
|
34
|
+
const fetchImpl = custom ?? globalThis.fetch;
|
|
35
|
+
if (!fetchImpl) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
"global fetch is unavailable; pass `fetch` or run on Node 18+",
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return fetchImpl;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function postTokenForm(
|
|
44
|
+
baseApiUrl: string,
|
|
45
|
+
form: URLSearchParams,
|
|
46
|
+
fetchImpl: typeof fetch,
|
|
47
|
+
): Promise<OAuthToken> {
|
|
48
|
+
const res = await fetchImpl(`${baseApiUrl}/v1/oauth/token`, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
51
|
+
body: form.toString(),
|
|
52
|
+
cache: "no-store",
|
|
53
|
+
});
|
|
54
|
+
if (!res.ok) throw await toApiError(res);
|
|
55
|
+
return (await res.json()) as OAuthToken;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* CP `POST /v1/oauth/token` response. No refresh token is issued for the
|
|
60
|
+
* machine grants — re-mint when it expires.
|
|
61
|
+
*/
|
|
62
|
+
export interface OAuthToken {
|
|
63
|
+
/** Project-scoped RS256 access token (`Authorization: Bearer …`). */
|
|
64
|
+
access_token: string;
|
|
65
|
+
/** Always `"Bearer"`. */
|
|
66
|
+
token_type: string;
|
|
67
|
+
/** Token lifetime in seconds. */
|
|
68
|
+
expires_in: number;
|
|
69
|
+
/** The granted (scope-capped) scope, when the CP returns one. */
|
|
70
|
+
scope: string | null;
|
|
71
|
+
/**
|
|
72
|
+
* Data Plane API base URL for the token's project, resolved by the CP the
|
|
73
|
+
* same way it is for the CLI login. `null` when the deployment can't be
|
|
74
|
+
* resolved; the caller then needs an explicit DP URL. Hand this to the
|
|
75
|
+
* browser SDK as `dpUrl` so the SPA connects without separate DP config.
|
|
76
|
+
*/
|
|
77
|
+
dp_url: string | null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface ServiceAccountTokenParams {
|
|
81
|
+
/** Confidential Application client id (`intro_app_…`). */
|
|
82
|
+
clientId: string;
|
|
83
|
+
/** Confidential Application client secret (`intro_sk_…`). */
|
|
84
|
+
clientSecret: string;
|
|
85
|
+
/**
|
|
86
|
+
* Project the token is scoped to. Required by the CP `client_credentials`
|
|
87
|
+
* grant — the minted token is project-scoped and the project must belong to
|
|
88
|
+
* the Application's organization.
|
|
89
|
+
*/
|
|
90
|
+
projectId: string;
|
|
91
|
+
/**
|
|
92
|
+
* Optional space-separated scope. Capped server-side to the Application's
|
|
93
|
+
* `allowed_scopes`; omit to receive the Application's default scope.
|
|
94
|
+
*/
|
|
95
|
+
scope?: string;
|
|
96
|
+
/**
|
|
97
|
+
* CP API base URL. Defaults to `INTROSPECTION_BASE_API_URL` or
|
|
98
|
+
* `https://api.introspection.dev`.
|
|
99
|
+
*/
|
|
100
|
+
baseApiUrl?: string;
|
|
101
|
+
/** Custom `fetch` (for tests or non-standard runtimes). */
|
|
102
|
+
fetch?: typeof fetch;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Back-compat alias: the `client_credentials` response is the same shape as
|
|
107
|
+
* every other `/v1/oauth/token` response.
|
|
108
|
+
*/
|
|
109
|
+
export type ServiceAccountToken = OAuthToken;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Mint a project-scoped CP access token from confidential service-account
|
|
113
|
+
* credentials. See {@link ServiceAccountTokenParams}.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* const { access_token, dp_url } = await serviceAccountToken({
|
|
118
|
+
* clientId: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_ID!,
|
|
119
|
+
* clientSecret: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_SECRET!,
|
|
120
|
+
* projectId: process.env.INTRO_PROJECT_ID!,
|
|
121
|
+
* });
|
|
122
|
+
* const client = new IntrospectionClient({ token: access_token });
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export async function serviceAccountToken(
|
|
126
|
+
params: ServiceAccountTokenParams,
|
|
127
|
+
): Promise<OAuthToken> {
|
|
128
|
+
const form = new URLSearchParams({
|
|
129
|
+
grant_type: GRANT_TYPE_CLIENT_CREDENTIALS,
|
|
130
|
+
client_id: params.clientId,
|
|
131
|
+
client_secret: params.clientSecret,
|
|
132
|
+
project_id: params.projectId,
|
|
133
|
+
});
|
|
134
|
+
if (params.scope) form.set("scope", params.scope);
|
|
135
|
+
return postTokenForm(
|
|
136
|
+
resolveBaseApiUrl(params.baseApiUrl),
|
|
137
|
+
form,
|
|
138
|
+
resolveFetch(params.fetch),
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface AuthorizationCodeParams {
|
|
143
|
+
/** The authorization code returned to the redirect URI. */
|
|
144
|
+
code: string;
|
|
145
|
+
/** Public SPA Application `client_id` (PKCE — no secret). */
|
|
146
|
+
clientId: string;
|
|
147
|
+
/** The redirect URI the code was issued for (must match the authorize call). */
|
|
148
|
+
redirectUri: string;
|
|
149
|
+
/** The PKCE `code_verifier` paired with the authorize-step challenge. */
|
|
150
|
+
codeVerifier: string;
|
|
151
|
+
/**
|
|
152
|
+
* CP API base URL. Defaults to `INTROSPECTION_BASE_API_URL` or
|
|
153
|
+
* `https://api.introspection.dev`.
|
|
154
|
+
*/
|
|
155
|
+
baseApiUrl?: string;
|
|
156
|
+
/** Custom `fetch` (for tests or non-standard runtimes). */
|
|
157
|
+
fetch?: typeof fetch;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* RFC 6749 / PKCE `authorization_code` exchange against CP
|
|
162
|
+
* `POST /v1/oauth/token`. Run it in your backend so the browser hosted-login
|
|
163
|
+
* flow does not hand-roll the token POST.
|
|
164
|
+
*/
|
|
165
|
+
export async function authorizationCodeToken(
|
|
166
|
+
params: AuthorizationCodeParams,
|
|
167
|
+
): Promise<OAuthToken> {
|
|
168
|
+
const form = new URLSearchParams({
|
|
169
|
+
grant_type: GRANT_TYPE_AUTHORIZATION_CODE,
|
|
170
|
+
code: params.code,
|
|
171
|
+
client_id: params.clientId,
|
|
172
|
+
redirect_uri: params.redirectUri,
|
|
173
|
+
code_verifier: params.codeVerifier,
|
|
174
|
+
});
|
|
175
|
+
return postTokenForm(
|
|
176
|
+
resolveBaseApiUrl(params.baseApiUrl),
|
|
177
|
+
form,
|
|
178
|
+
resolveFetch(params.fetch),
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface TokenExchangeParams {
|
|
183
|
+
/** The end user's subject token (e.g. a partner-IdP `id_token`). */
|
|
184
|
+
subjectToken: string;
|
|
185
|
+
/** The federated Application's `client_id` (public client — no secret). */
|
|
186
|
+
clientId: string;
|
|
187
|
+
/** Project the minted DP token is scoped to. */
|
|
188
|
+
projectId: string;
|
|
189
|
+
/**
|
|
190
|
+
* The subject token's type URI. Defaults to
|
|
191
|
+
* `urn:ietf:params:oauth:token-type:id_token`.
|
|
192
|
+
*/
|
|
193
|
+
subjectTokenType?: string;
|
|
194
|
+
/** Optional space-separated scope, capped server-side. */
|
|
195
|
+
scope?: string;
|
|
196
|
+
/**
|
|
197
|
+
* CP API base URL. Defaults to `INTROSPECTION_BASE_API_URL` or
|
|
198
|
+
* `https://api.introspection.dev`.
|
|
199
|
+
*/
|
|
200
|
+
baseApiUrl?: string;
|
|
201
|
+
/** Custom `fetch` (for tests or non-standard runtimes). */
|
|
202
|
+
fetch?: typeof fetch;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* RFC 8693 token-exchange against CP `POST /v1/oauth/token`: trade an end
|
|
207
|
+
* user's partner-IdP token for a project-scoped DP access token for a
|
|
208
|
+
* `member_type=customer` member. Intended to run server-side in a broker (the
|
|
209
|
+
* subject token shouldn't be re-handled in the browser longer than needed).
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* const { access_token, dp_url } = await tokenExchange({
|
|
214
|
+
* subjectToken: idTokenFromPartnerIdp,
|
|
215
|
+
* clientId: process.env.FEDERATED_CLIENT_ID!,
|
|
216
|
+
* projectId: process.env.INTRO_PROJECT_ID!,
|
|
217
|
+
* });
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
export async function tokenExchange(
|
|
221
|
+
params: TokenExchangeParams,
|
|
222
|
+
): Promise<OAuthToken> {
|
|
223
|
+
const form = new URLSearchParams({
|
|
224
|
+
grant_type: GRANT_TYPE_TOKEN_EXCHANGE,
|
|
225
|
+
subject_token: params.subjectToken,
|
|
226
|
+
subject_token_type: params.subjectTokenType ?? SUBJECT_TOKEN_TYPE_ID_TOKEN,
|
|
227
|
+
client_id: params.clientId,
|
|
228
|
+
project_id: params.projectId,
|
|
229
|
+
});
|
|
230
|
+
if (params.scope) form.set("scope", params.scope);
|
|
231
|
+
return postTokenForm(
|
|
232
|
+
resolveBaseApiUrl(params.baseApiUrl),
|
|
233
|
+
form,
|
|
234
|
+
resolveFetch(params.fetch),
|
|
235
|
+
);
|
|
236
|
+
}
|
package/src/client.ts
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
import type { AdvancedOptions } from "@introspection-sdk/types";
|
|
16
16
|
import { logger as sdkLogger } from "./utils.js";
|
|
17
17
|
import type { IntrospectionClientOptions } from "./types.js";
|
|
18
|
+
import { serviceAccountToken, type ServiceAccountTokenParams } from "./auth.js";
|
|
18
19
|
import { HttpClient } from "./http.js";
|
|
19
20
|
import {
|
|
20
21
|
attachRuntimes,
|
|
@@ -106,6 +107,43 @@ export class IntrospectionClient {
|
|
|
106
107
|
sdkLogger.info(`IntrospectionClient initialized: api=${baseApiUrl}`);
|
|
107
108
|
}
|
|
108
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Authenticate as a confidential service account and return a ready
|
|
112
|
+
* client.
|
|
113
|
+
*
|
|
114
|
+
* Mints a short-lived, project-scoped CP access token via the
|
|
115
|
+
* `client_credentials` grant (see {@link serviceAccountToken}) and wires
|
|
116
|
+
* it in as the bearer token, so the runtime flow works exactly as it does
|
|
117
|
+
* with an API key:
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const client = await IntrospectionClient.fromServiceAccount({
|
|
122
|
+
* clientId: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_ID!,
|
|
123
|
+
* clientSecret: process.env.INTROSPECTION_SERVICE_ACCOUNT_CLIENT_SECRET!,
|
|
124
|
+
* projectId: process.env.INTRO_PROJECT_ID!,
|
|
125
|
+
* });
|
|
126
|
+
*
|
|
127
|
+
* // Resolved fresh from the runtime name on every call.
|
|
128
|
+
* const runner = await client.runtimes("customer-agent").run({
|
|
129
|
+
* identity: { user_id: "u_demo" },
|
|
130
|
+
* });
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* The token is not auto-refreshed: it lives for `expires_in` seconds, so
|
|
134
|
+
* re-mint (call this again) for long-lived processes once it lapses.
|
|
135
|
+
*/
|
|
136
|
+
static async fromServiceAccount(
|
|
137
|
+
params: ServiceAccountTokenParams & { serviceName?: string },
|
|
138
|
+
): Promise<IntrospectionClient> {
|
|
139
|
+
const { access_token } = await serviceAccountToken(params);
|
|
140
|
+
return new IntrospectionClient({
|
|
141
|
+
token: access_token,
|
|
142
|
+
serviceName: params.serviceName,
|
|
143
|
+
advanced: { baseApiUrl: params.baseApiUrl, fetch: params.fetch },
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
109
147
|
/** Close the underlying HTTP client. */
|
|
110
148
|
async shutdown(): Promise<void> {
|
|
111
149
|
// HttpClient has no persistent connections to close, but reserved
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,20 @@
|
|
|
12
12
|
// REST client.
|
|
13
13
|
export { IntrospectionClient } from "./client.js";
|
|
14
14
|
|
|
15
|
+
// Machine / federated / hosted-login OAuth token helpers.
|
|
16
|
+
export {
|
|
17
|
+
serviceAccountToken,
|
|
18
|
+
tokenExchange,
|
|
19
|
+
authorizationCodeToken,
|
|
20
|
+
} from "./auth.js";
|
|
21
|
+
export type {
|
|
22
|
+
ServiceAccountTokenParams,
|
|
23
|
+
ServiceAccountToken,
|
|
24
|
+
TokenExchangeParams,
|
|
25
|
+
AuthorizationCodeParams,
|
|
26
|
+
OAuthToken,
|
|
27
|
+
} from "./auth.js";
|
|
28
|
+
|
|
15
29
|
// Configuration / event types shared with the OTel surface.
|
|
16
30
|
export type {
|
|
17
31
|
AdvancedOptions,
|