@quickpipeaiab/tokens 0.0.0-stage → 0.6.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/LICENSE +21 -0
- package/README.md +207 -2
- package/dist/assertion.d.ts +4 -0
- package/dist/assertion.d.ts.map +1 -0
- package/dist/assertion.js +15 -0
- package/dist/assertion.js.map +1 -0
- package/dist/body.d.ts +3 -0
- package/dist/body.d.ts.map +1 -0
- package/dist/body.js +32 -0
- package/dist/body.js.map +1 -0
- package/dist/claims.d.ts +9 -0
- package/dist/claims.d.ts.map +1 -0
- package/dist/claims.js +251 -0
- package/dist/claims.js.map +1 -0
- package/dist/endpoint.d.ts +10 -0
- package/dist/endpoint.d.ts.map +1 -0
- package/dist/endpoint.js +25 -0
- package/dist/endpoint.js.map +1 -0
- package/dist/exchange.d.ts +66 -0
- package/dist/exchange.d.ts.map +1 -0
- package/dist/exchange.js +135 -0
- package/dist/exchange.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/key.d.ts +8 -0
- package/dist/key.d.ts.map +1 -0
- package/dist/key.js +30 -0
- package/dist/key.js.map +1 -0
- package/dist/keys.d.ts +16 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +106 -0
- package/dist/keys.js.map +1 -0
- package/dist/live.d.ts +30 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +123 -0
- package/dist/live.js.map +1 -0
- package/dist/refusal.d.ts +9 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +27 -0
- package/dist/refusal.js.map +1 -0
- package/dist/roles.d.ts +5 -0
- package/dist/roles.d.ts.map +1 -0
- package/dist/roles.js +24 -0
- package/dist/roles.js.map +1 -0
- package/dist/source.d.ts +15 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +92 -0
- package/dist/source.js.map +1 -0
- package/dist/token.d.ts +58 -0
- package/dist/token.d.ts.map +1 -0
- package/dist/token.js +10 -0
- package/dist/token.js.map +1 -0
- package/dist/url.d.ts +2 -0
- package/dist/url.d.ts.map +1 -0
- package/dist/url.js +16 -0
- package/dist/url.js.map +1 -0
- package/dist/verifier.d.ts +18 -0
- package/dist/verifier.d.ts.map +1 -0
- package/dist/verifier.js +90 -0
- package/dist/verifier.js.map +1 -0
- package/package.json +29 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 QuickPipe AI AB
|
|
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,208 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @quickpipeaiab/tokens
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The local check of the access tokens of `auth`, and the source of machine tokens for a service in TypeScript. `auth` records 0003, 0006 and 0011 give the rules.
|
|
4
|
+
|
|
5
|
+
The package is MIT. It does not import code of `auth`. It is the only location that knows the claim names of `Zitadel`.
|
|
6
|
+
|
|
7
|
+
## Check a token
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Refusal, verifier } from '@quickpipeaiab/tokens';
|
|
11
|
+
|
|
12
|
+
const tokens = verifier({
|
|
13
|
+
issuer: process.env.AUTH_ISSUER,
|
|
14
|
+
audience: process.env.AUTH_AUDIENCE,
|
|
15
|
+
jwksUrl: process.env.AUTH_JWKS_URL,
|
|
16
|
+
staffOrganization: process.env.AUTH_STAFF_ORGANIZATION,
|
|
17
|
+
staffProject: process.env.AUTH_STAFF_PROJECT,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const token = await tokens.verify(jwt, { workspace: c.req.param('workspace') ?? null });
|
|
21
|
+
switch (token.kind) {
|
|
22
|
+
case 'machine':
|
|
23
|
+
requireScope(token.scopes, 'holds:write');
|
|
24
|
+
break;
|
|
25
|
+
case 'user':
|
|
26
|
+
if (token.staff) requireStaffPermission(token.subject);
|
|
27
|
+
else audit(token.subject, token.workspace);
|
|
28
|
+
break;
|
|
29
|
+
case 'exchanged':
|
|
30
|
+
case 'agent-key':
|
|
31
|
+
audit(token.subject, token.workspace);
|
|
32
|
+
break;
|
|
33
|
+
default: {
|
|
34
|
+
const unknown: never = token;
|
|
35
|
+
return unknown;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`staffProject` is the id of the project `quickpipe`. It is optional. With no `staffProject`, each user token has no `accessRoles`.
|
|
41
|
+
|
|
42
|
+
`audience` is the project id of the target service. `staffOrganization` is the id of the organization `quickpipe`. `jwksUrl` must use `https`. Only `localhost` and `127.0.0.1` can use `http`. The same rule applies to the `issuer` of `source`. `verify` accepts only `RS256` and a clock tolerance of 15 seconds. It gives one token of the closed union `Token`, with the field `kind`:
|
|
43
|
+
|
|
44
|
+
| `kind` | Fields |
|
|
45
|
+
| ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
46
|
+
| `user` | `staff: false`, `workspace` and `accessRoles`: the keys of the claim `urn:zitadel:iam:org:project:{staffProject}:roles` that have the workspace in their value. Or `staff: true` with no `workspace`. `staff: true` means that the home organization of the user is the staff organization. A staff token also has `accessRoles`: the keys of the claim `urn:zitadel:iam:org:project:{staffProject}:roles` that have the staff organization in their value. `accessRoles` is empty when the claim is not there. `verify` refuses this claim when it is not an object. `verify` refuses a different user token with no workspace. It also refuses a user token with a workspace when the home organization is the staff organization, or when the token has no home organization. |
|
|
47
|
+
| `machine` | `client`: the name of the machine client. `scopes`: the project roles of the audience project. `verify` refuses a machine token with no client, an empty client or a client that is not text. |
|
|
48
|
+
| `exchanged` | `workspace`, `actor` with its chain, and `exchange` with the field `kind`: `user-call`, `run`, `subscription`, `impersonation` or `flow`. A `run` and a `subscription` have `kaiRunId`. A `run` also has an optional `scheduleId`. Each actor has `subject`. An actor that is a machine client also has `client`: `desk`, `kai-agent` or `kai-agent-worker`. `scopes`: the machine scopes of the claim `scope`. A token of the kind `subscription` has them. |
|
|
49
|
+
| `agent-key` | `workspace` and `keyId` |
|
|
50
|
+
|
|
51
|
+
`verify` accepts only these chains of actors, as `auth` records 0007 and 0008 tell. The first actor is the claim `act`. A staff user has no `client`.
|
|
52
|
+
|
|
53
|
+
| Exchange kind | Chain of actors |
|
|
54
|
+
| -------------------------- | --------------------------------------------------------------------- |
|
|
55
|
+
| `user-call` | `desk`, or `desk` then a staff user |
|
|
56
|
+
| `run` with no `scheduleId` | `kai-agent` then `desk`, or `kai-agent` then `desk` then a staff user |
|
|
57
|
+
| `run` with a `scheduleId` | `kai-agent-worker` |
|
|
58
|
+
| `subscription` | `kai-agent` |
|
|
59
|
+
| `impersonation` | A staff user |
|
|
60
|
+
| `flow` | `desk` |
|
|
61
|
+
|
|
62
|
+
A route authorizes a machine token by its scope and, when the route needs one, by its client.
|
|
63
|
+
|
|
64
|
+
Each token has `subject`, `audience` and `expiresAt`. A token never has the email or the name of a user.
|
|
65
|
+
|
|
66
|
+
Staff get access to a workspace only through impersonation, with a token of kind `exchanged`, as `auth` record 0004 tells. `staff: true` does not give a permission. A staff route also checks the staff access-role of the user, as `auth` record 0008 item 7 tells.
|
|
67
|
+
|
|
68
|
+
`verify` fails with a `Refusal`. Its `reason` is one of `malformed`, `signature`, `issuer`, `audience`, `expired`, `claims`, `keys` and `workspace`. The reason `claims` tells that a claim of the token kind is missing or not correct. The reason `keys` tells that the service has no usable keys.
|
|
69
|
+
|
|
70
|
+
`refusalKindOf` groups the reasons of a `Refusal`: `bad` for a token that is not valid, `keys` for unusable signature keys and `workspace` for a route of a different workspace. A service answers `keys` with `502 upstream_failed`, not `401`, and writes one log line with `KEY_SOURCE_FAILURE`.
|
|
71
|
+
|
|
72
|
+
The second argument is the route. It must have the field `workspace`.
|
|
73
|
+
|
|
74
|
+
When the route has a workspace, give its id. Then a `user`, `exchanged` or `agent-key` token must have the same workspace. A `machine` token has no workspace and passes. When the route has no workspace, give `null`. `verify` refuses a route with an empty workspace.
|
|
75
|
+
|
|
76
|
+
## JWKS cache
|
|
77
|
+
|
|
78
|
+
```mermaid
|
|
79
|
+
stateDiagram-v2
|
|
80
|
+
[*] --> Empty
|
|
81
|
+
Empty --> Fresh: refresh
|
|
82
|
+
Fresh --> Due: 10 minutes
|
|
83
|
+
Fresh --> Fresh: unknown key id, refresh
|
|
84
|
+
Due --> Fresh: refresh
|
|
85
|
+
Due --> Stale: refresh fails
|
|
86
|
+
Stale --> Fresh: refresh
|
|
87
|
+
Stale --> Closed: 24 hours from the last refresh
|
|
88
|
+
Empty --> Closed: refresh fails
|
|
89
|
+
Closed --> Fresh: refresh
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The JWKS request and the token request do not follow a redirect. Only one refresh runs at a time. A refresh starts 30 seconds or more after the last attempt. When the keys are due and know the key id of the token, the check uses them at once and refreshes in the background. The JWKS answer must be 64 KiB or less. Thus a flood of unknown key ids does not send many calls to `auth`.
|
|
93
|
+
|
|
94
|
+
## Get a machine token
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { readKey, source } from '@quickpipeaiab/tokens';
|
|
98
|
+
|
|
99
|
+
const billing = source({
|
|
100
|
+
key: readKey(process.env.AUTH_CLIENT_KEY),
|
|
101
|
+
issuer: process.env.AUTH_ISSUER,
|
|
102
|
+
audience: process.env.BILLING_PROJECT_ID,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
await fetch(url, { headers: { authorization: `Bearer ${await billing.get()}` } });
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`readKey` reads the JSON key of the machine client from Doppler. When the value is not JSON, its error message has no text of the value. `get` keeps the token and gets a new token one minute before the expiry. Only one request runs at a time. When `auth` does not answer, `get` gives the last token until its expiry. After a failed request, `get` sends no request for 30 seconds: it gives the last valid token, or it fails at once.
|
|
109
|
+
|
|
110
|
+
## Exchange a user token
|
|
111
|
+
|
|
112
|
+
`desk` exchanges the token of a user for a token of 2 minutes, bound to one workspace and one audience, as `auth` record 0007 tells.
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { ExchangeFailure, exchanger, readKey, source } from '@quickpipeaiab/tokens';
|
|
116
|
+
|
|
117
|
+
const key = readKey(process.env.AUTH_KEY);
|
|
118
|
+
const actor = source({ key, issuer: process.env.AUTH_ISSUER, audience: process.env.AUTH_AUDIENCE });
|
|
119
|
+
const exchange = exchanger({ key, issuer: process.env.AUTH_ISSUER, actor });
|
|
120
|
+
|
|
121
|
+
const bound = await exchange.userCall({ subjectToken, workspace, audience: billingProject });
|
|
122
|
+
await fetch(url, { headers: { authorization: `Bearer ${bound.value}` } });
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
`userCall` signs a client assertion with the key, and gives the machine token of `actor` as the actor token. It gives the token and its expiry. It fails with an `ExchangeFailure`. Its `error` is the refusal of RFC 8693 (`invalid_request`, `invalid_grant`, `unauthorized_client` or `invalid_target`), or `unavailable` when `auth` does not answer or gives no token. Its `detail` tells the status of the answer (`status 502`), the network cause (`network ECONNRESET`, `timeout`) or `no token in the answer`, and `reason` joins the two for a log line. The caller keeps the token in a cache until its expiry.
|
|
126
|
+
|
|
127
|
+
## Exchange a user id for an action of a Kai flow
|
|
128
|
+
|
|
129
|
+
`desk` runs an action of a Kai flow when it has no token of the user. It exchanges the user id for a token of 2 minutes, bound to one workspace and the audience `leads`, as `auth` record 0007 tells.
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { flowExchanger, readKey, source } from '@quickpipeaiab/tokens';
|
|
133
|
+
|
|
134
|
+
const key = readKey(process.env.AUTH_KEY);
|
|
135
|
+
const actor = source({ key, issuer: process.env.AUTH_ISSUER, audience: process.env.AUTH_AUDIENCE });
|
|
136
|
+
const exchange = flowExchanger({ key, issuer: process.env.AUTH_ISSUER, actor });
|
|
137
|
+
|
|
138
|
+
const bound = await exchange.flow({ user, workspace, audience: leadsProject });
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
`flow` fails with the same `ExchangeFailure` as `userCall`.
|
|
142
|
+
|
|
143
|
+
## Exchange a subscription run for a server token
|
|
144
|
+
|
|
145
|
+
`kai-agent` starts a run of a task from a webhook, and the run has no user. It exchanges its own machine token for a server token of 15 minutes. The token binds to one workspace, one run and one machine scope, as `auth` record 0007 tells.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import { readKey, source, subscriptionExchanger } from '@quickpipeaiab/tokens';
|
|
149
|
+
|
|
150
|
+
const key = readKey(process.env.AUTH_KEY);
|
|
151
|
+
const actor = source({ key, issuer: process.env.AUTH_ISSUER, audience: process.env.AUTH_AUDIENCE });
|
|
152
|
+
const exchange = subscriptionExchanger({ key, issuer: process.env.AUTH_ISSUER, actor });
|
|
153
|
+
|
|
154
|
+
const bound = await exchange.subscription({
|
|
155
|
+
subjectToken,
|
|
156
|
+
workspace,
|
|
157
|
+
audience: leadScoringProject,
|
|
158
|
+
kaiRunId: run,
|
|
159
|
+
scope: 'identity:write',
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
The subject token and the actor token are the machine token of the client. `auth` gives the token only when the client holds the requested scope at the target project.
|
|
164
|
+
|
|
165
|
+
`subscription` fails with the same `ExchangeFailure` as `userCall`.
|
|
166
|
+
## Check a sensitive permission
|
|
167
|
+
|
|
168
|
+
A sensitive permission reads the current access-role of the subject in `auth` at the time of the call, as platform record 0028 tells. A route that is not sensitive checks the claim of the token only.
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
import { compile } from '@quickpipeaiab/rbac';
|
|
172
|
+
import { read } from '@quickpipeaiab/rbac/catalog';
|
|
173
|
+
import { liveCheck, readKey } from '@quickpipeaiab/tokens';
|
|
174
|
+
|
|
175
|
+
const rules = compile(read());
|
|
176
|
+
const check = liveCheck({
|
|
177
|
+
issuer: process.env.AUTH_ISSUER,
|
|
178
|
+
project: process.env.AUTH_STAFF_PROJECT,
|
|
179
|
+
key: readKey(process.env.AUTH_KEY),
|
|
180
|
+
isAllowed: (accessRole, permission) => rules.isAllowed(accessRole, permission),
|
|
181
|
+
logger: log,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const caller = { jwt, subject: token.subject, workspace };
|
|
185
|
+
const decision = await check.allows(caller, 'grants:write');
|
|
186
|
+
if (decision.allowed) return handle(c);
|
|
187
|
+
return refuse('forbidden');
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
`allows` gives a result with `allowed`, `cause` and `cached`. A result with `allowed: false` refuses the call, as `desk` does with `Problem('forbidden')`.
|
|
191
|
+
|
|
192
|
+
| Cause | Description |
|
|
193
|
+
|-------|---------|
|
|
194
|
+
| `granted` | An access-role of the project accepts the permission. |
|
|
195
|
+
| `role-removed` | The answer has no access-role that accepts the permission. |
|
|
196
|
+
| `inactive` | The answer has `active: false`. |
|
|
197
|
+
| `auth-unavailable` | `auth` does not answer, answers with an error status, or gives no valid JSON. |
|
|
198
|
+
| `bad-answer` | The answer is not for the same subject or workspace, has no workspace claim, or has a malformed role claim. |
|
|
199
|
+
|
|
200
|
+
`allows` sends the token to the introspection of `auth`. It signs the request with the machine key. It gives `allowed: true` only when `auth` gives an active answer for the same subject and workspace. That answer must also have an access-role of the project `project` that `isAllowed` accepts. It gives `allowed: false` for any other case.
|
|
201
|
+
|
|
202
|
+
The optional `logger` has `info(fields, message)`. The function calls it once for each decision with `subject`, `workspace`, `permission`, `allowed`, `cause` and `cached`. The line has no token and no secret. `cached` is `true` when the answer came from the map. A service can add a field of its own, for example `httpRequestId` for the id of the HTTP request.
|
|
203
|
+
|
|
204
|
+
It keeps the answer for 10 seconds in a map of 1000 entries at most. The key is a JSON array of the subject, the workspace and the permission, so no field can collide with another. It does not keep the token text. A call with the same key while a request runs waits for that request. The refusal of a failure stays for the same 10 seconds, so a burst of calls does not load `auth` during its outage.
|
|
205
|
+
|
|
206
|
+
`project` is the id of the project `quickpipe`. It must not be empty: `liveCheck` throws when it is empty, so the service fails at start. Its roles claim is the source of the access-roles, in the same form as `verify` reads it. The machine key of the service needs the project role `introspect`, as `auth` record 0015 tells. `issuer` must use `https`, as for `source`.
|
|
207
|
+
|
|
208
|
+
A staff token has no workspace claim. Give the id of the organization `quickpipe` as `workspace` for a staff call. `auth` grants the staff access-roles of a staff user in that organization.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assertion.d.ts","sourceRoot":"","sources":["../src/assertion.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAI1C,eAAO,MAAM,OAAO,GAAI,QAAQ,MAAM,KAAG,MAAoC,CAAC;AAE9E,wBAAgB,SAAS,CAAC,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAUtF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { SignJWT } from 'jose';
|
|
2
|
+
const ASSERTION_SECONDS = 60;
|
|
3
|
+
export const trimmed = (issuer) => issuer.replace(/\/+$/, '');
|
|
4
|
+
export function assertion(key, issuer, now) {
|
|
5
|
+
const seconds = Math.floor(now / 1000);
|
|
6
|
+
return new SignJWT({})
|
|
7
|
+
.setProtectedHeader({ alg: 'RS256', kid: key.keyId })
|
|
8
|
+
.setIssuer(key.userId)
|
|
9
|
+
.setSubject(key.userId)
|
|
10
|
+
.setAudience(trimmed(issuer))
|
|
11
|
+
.setIssuedAt(seconds)
|
|
12
|
+
.setExpirationTime(seconds + ASSERTION_SECONDS)
|
|
13
|
+
.sign(key.privateKey);
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=assertion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assertion.js","sourceRoot":"","sources":["../src/assertion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAG/B,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,MAAc,EAAU,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAE9E,MAAM,UAAU,SAAS,CAAC,GAAc,EAAE,MAAc,EAAE,GAAW;IACnE,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC;IACvC,OAAO,IAAI,OAAO,CAAC,EAAE,CAAC;SACnB,kBAAkB,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,CAAC,KAAK,EAAE,CAAC;SACpD,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC;SACrB,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC;SACtB,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;SAC5B,WAAW,CAAC,OAAO,CAAC;SACpB,iBAAiB,CAAC,OAAO,GAAG,iBAAiB,CAAC;SAC9C,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;AAC1B,CAAC"}
|
package/dist/body.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body.d.ts","sourceRoot":"","sources":["../src/body.ts"],"names":[],"mappings":"AAEA,wBAAsB,UAAU,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,MAAM,CAAC,CAgBpE;AAMD,wBAAsB,UAAU,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC,CAOjG"}
|
package/dist/body.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const MAX_BODY_BYTES = 64 * 1024;
|
|
2
|
+
export async function readCapped(response) {
|
|
3
|
+
const reader = response.body?.getReader();
|
|
4
|
+
if (!reader)
|
|
5
|
+
return '';
|
|
6
|
+
const decoder = new TextDecoder();
|
|
7
|
+
let size = 0;
|
|
8
|
+
let text = '';
|
|
9
|
+
for (let chunk = await reader.read(); !chunk.done; chunk = await reader.read()) {
|
|
10
|
+
const bytes = chunk.value;
|
|
11
|
+
size += bytes.byteLength;
|
|
12
|
+
if (size > MAX_BODY_BYTES) {
|
|
13
|
+
await reader.cancel();
|
|
14
|
+
throw new Error('The answer is too large');
|
|
15
|
+
}
|
|
16
|
+
text += decoder.decode(bytes, { stream: true });
|
|
17
|
+
}
|
|
18
|
+
return text + decoder.decode();
|
|
19
|
+
}
|
|
20
|
+
function isObject(value) {
|
|
21
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
22
|
+
}
|
|
23
|
+
export async function readObject(response) {
|
|
24
|
+
try {
|
|
25
|
+
const value = JSON.parse(await readCapped(response));
|
|
26
|
+
return isObject(value) ? value : undefined;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=body.js.map
|
package/dist/body.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body.js","sourceRoot":"","sources":["../src/body.ts"],"names":[],"mappings":"AAAA,MAAM,cAAc,GAAG,EAAE,GAAG,IAAI,CAAC;AAEjC,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,QAAkB;IACjD,MAAM,MAAM,GAAG,QAAQ,CAAC,IAAI,EAAE,SAAS,EAAyD,CAAC;IACjG,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,CAAC;IACvB,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;IAClC,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,KAAK,IAAI,KAAK,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC;QAC/E,MAAM,KAAK,GAAe,KAAK,CAAC,KAAK,CAAC;QACtC,IAAI,IAAI,KAAK,CAAC,UAAU,CAAC;QACzB,IAAI,IAAI,GAAG,cAAc,EAAE,CAAC;YAC1B,MAAM,MAAM,CAAC,MAAM,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;QAC7C,CAAC;QACD,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,IAAI,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;AACjC,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,QAAkB;IACjD,IAAI,CAAC;QACH,MAAM,KAAK,GAAY,IAAI,CAAC,KAAK,CAAC,MAAM,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC;QAC9D,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;IAC7C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC"}
|
package/dist/claims.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { JWTPayload } from 'jose';
|
|
2
|
+
import type { Token } from './token.js';
|
|
3
|
+
export interface Context {
|
|
4
|
+
project: string;
|
|
5
|
+
staffOrganization: string;
|
|
6
|
+
staffProject: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function read(payload: JWTPayload, context: Context): Token;
|
|
9
|
+
//# sourceMappingURL=claims.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claims.d.ts","sourceRoot":"","sources":["../src/claims.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAIvC,OAAO,KAAK,EAA+C,KAAK,EAAE,MAAM,YAAY,CAAC;AAgBrF,MAAM,WAAW,OAAO;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;CACtB;AAmOD,wBAAgB,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,KAAK,CAgBjE"}
|
package/dist/claims.js
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { Refusal } from './refusal.js';
|
|
2
|
+
import { parseAccessRoles, parseRoles } from './roles.js';
|
|
3
|
+
import { CLIENTS, EXCHANGE_KINDS, KINDS } from './token.js';
|
|
4
|
+
const KIND = 'urn:quickpipe:token-kind';
|
|
5
|
+
const EXCHANGE_KIND = 'urn:quickpipe:exchange-kind';
|
|
6
|
+
const KAI_RUN_ID = 'urn:quickpipe:kai-run-id';
|
|
7
|
+
const SCHEDULE_ID = 'urn:quickpipe:schedule-id';
|
|
8
|
+
const AGENT_KEY_ID = 'urn:quickpipe:agent-key-id';
|
|
9
|
+
const SCOPE = 'scope';
|
|
10
|
+
const WORKSPACE = 'urn:zitadel:iam:org:id';
|
|
11
|
+
const HOME = 'urn:zitadel:iam:user:resourceowner:id';
|
|
12
|
+
const ACTOR = 'act';
|
|
13
|
+
const CLIENT = 'client_id';
|
|
14
|
+
const MAX_ACTOR_DEPTH = 3;
|
|
15
|
+
function refuse() {
|
|
16
|
+
throw new Refusal('claims');
|
|
17
|
+
}
|
|
18
|
+
function text(claims, name) {
|
|
19
|
+
const value = claims[name];
|
|
20
|
+
if (typeof value !== 'string' || value === '')
|
|
21
|
+
return refuse();
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function optionalText(claims, name) {
|
|
25
|
+
if (claims[name] === undefined)
|
|
26
|
+
return undefined;
|
|
27
|
+
return text(claims, name);
|
|
28
|
+
}
|
|
29
|
+
function isKind(value) {
|
|
30
|
+
return KINDS.includes(value);
|
|
31
|
+
}
|
|
32
|
+
function isExchangeKind(value) {
|
|
33
|
+
return EXCHANGE_KINDS.includes(value);
|
|
34
|
+
}
|
|
35
|
+
function kindOf(claims) {
|
|
36
|
+
const value = claims[KIND];
|
|
37
|
+
if (!isKind(value))
|
|
38
|
+
return refuse();
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
function audienceOf(payload) {
|
|
42
|
+
if (typeof payload.aud === 'string')
|
|
43
|
+
return [payload.aud];
|
|
44
|
+
return payload.aud ?? [];
|
|
45
|
+
}
|
|
46
|
+
function base(payload) {
|
|
47
|
+
if (typeof payload.exp !== 'number')
|
|
48
|
+
return refuse();
|
|
49
|
+
return {
|
|
50
|
+
subject: text(payload, 'sub'),
|
|
51
|
+
audience: audienceOf(payload),
|
|
52
|
+
expiresAt: new Date(payload.exp * 1000),
|
|
53
|
+
...(typeof payload.iat === 'number' ? { issuedAt: new Date(payload.iat * 1000) } : {}),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function isClient(value) {
|
|
57
|
+
return CLIENTS.includes(value);
|
|
58
|
+
}
|
|
59
|
+
function clientOf(claims) {
|
|
60
|
+
const value = claims[CLIENT];
|
|
61
|
+
if (value === undefined)
|
|
62
|
+
return undefined;
|
|
63
|
+
if (!isClient(value))
|
|
64
|
+
return refuse();
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
function actorOf(value, depth) {
|
|
68
|
+
if (depth > MAX_ACTOR_DEPTH || typeof value !== 'object' || value === null)
|
|
69
|
+
return refuse();
|
|
70
|
+
const claims = value;
|
|
71
|
+
const actor = { subject: text(claims, 'sub') };
|
|
72
|
+
const client = clientOf(claims);
|
|
73
|
+
if (client !== undefined)
|
|
74
|
+
actor.client = client;
|
|
75
|
+
if (claims[ACTOR] !== undefined)
|
|
76
|
+
actor.actor = actorOf(claims[ACTOR], depth + 1);
|
|
77
|
+
return actor;
|
|
78
|
+
}
|
|
79
|
+
function linksOf(actor) {
|
|
80
|
+
const link = actor.client ?? 'staff';
|
|
81
|
+
if (actor.actor === undefined)
|
|
82
|
+
return [link];
|
|
83
|
+
return [link, ...linksOf(actor.actor)];
|
|
84
|
+
}
|
|
85
|
+
function chainsOf(exchange) {
|
|
86
|
+
switch (exchange.kind) {
|
|
87
|
+
case 'user-call':
|
|
88
|
+
return [['desk'], ['desk', 'staff']];
|
|
89
|
+
case 'run':
|
|
90
|
+
if (exchange.scheduleId !== undefined)
|
|
91
|
+
return [['kai-agent-worker']];
|
|
92
|
+
return [
|
|
93
|
+
['kai-agent', 'desk'],
|
|
94
|
+
['kai-agent', 'desk', 'staff'],
|
|
95
|
+
];
|
|
96
|
+
case 'subscription':
|
|
97
|
+
return [['kai-agent']];
|
|
98
|
+
case 'impersonation':
|
|
99
|
+
return [['staff']];
|
|
100
|
+
case 'flow':
|
|
101
|
+
return [['desk']];
|
|
102
|
+
default: {
|
|
103
|
+
const unknown = exchange;
|
|
104
|
+
return unknown;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function sameChain(chain, links) {
|
|
109
|
+
return chain.length === links.length && chain.every((link, index) => link === links[index]);
|
|
110
|
+
}
|
|
111
|
+
function checkChain(actor, exchange) {
|
|
112
|
+
const links = linksOf(actor);
|
|
113
|
+
if (!chainsOf(exchange).some((chain) => sameChain(chain, links)))
|
|
114
|
+
return refuse();
|
|
115
|
+
return actor;
|
|
116
|
+
}
|
|
117
|
+
function run(claims) {
|
|
118
|
+
const kaiRunId = text(claims, KAI_RUN_ID);
|
|
119
|
+
const scheduleId = optionalText(claims, SCHEDULE_ID);
|
|
120
|
+
if (scheduleId === undefined)
|
|
121
|
+
return { kind: 'run', kaiRunId };
|
|
122
|
+
return { kind: 'run', kaiRunId, scheduleId };
|
|
123
|
+
}
|
|
124
|
+
function subscription(claims) {
|
|
125
|
+
return { kind: 'subscription', kaiRunId: text(claims, KAI_RUN_ID) };
|
|
126
|
+
}
|
|
127
|
+
function exchangeOf(claims) {
|
|
128
|
+
const kind = claims[EXCHANGE_KIND];
|
|
129
|
+
if (!isExchangeKind(kind))
|
|
130
|
+
return refuse();
|
|
131
|
+
switch (kind) {
|
|
132
|
+
case 'user-call':
|
|
133
|
+
return { kind };
|
|
134
|
+
case 'run':
|
|
135
|
+
return run(claims);
|
|
136
|
+
case 'subscription':
|
|
137
|
+
return subscription(claims);
|
|
138
|
+
case 'impersonation':
|
|
139
|
+
case 'flow':
|
|
140
|
+
return { kind };
|
|
141
|
+
default: {
|
|
142
|
+
const unknown = kind;
|
|
143
|
+
return unknown;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function accessRolesIn(claims, context, organization) {
|
|
148
|
+
const accessRoles = parseAccessRoles(claims, context.staffProject, organization);
|
|
149
|
+
if (accessRoles === undefined)
|
|
150
|
+
return refuse();
|
|
151
|
+
return accessRoles;
|
|
152
|
+
}
|
|
153
|
+
function assertNoActor(claims) {
|
|
154
|
+
if (claims[ACTOR] !== undefined || claims[EXCHANGE_KIND] !== undefined)
|
|
155
|
+
refuse();
|
|
156
|
+
}
|
|
157
|
+
function staff(payload, context) {
|
|
158
|
+
if (text(payload, HOME) !== context.staffOrganization)
|
|
159
|
+
return refuse();
|
|
160
|
+
return {
|
|
161
|
+
kind: 'user',
|
|
162
|
+
...base(payload),
|
|
163
|
+
staff: true,
|
|
164
|
+
accessRoles: accessRolesIn(payload, context, context.staffOrganization),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function clientIdOf(payload) {
|
|
168
|
+
const clientId = optionalText(payload, CLIENT);
|
|
169
|
+
return clientId === undefined ? {} : { clientId };
|
|
170
|
+
}
|
|
171
|
+
function teammate(payload, workspace, context) {
|
|
172
|
+
if (text(payload, HOME) === context.staffOrganization)
|
|
173
|
+
return refuse();
|
|
174
|
+
return {
|
|
175
|
+
kind: 'user',
|
|
176
|
+
...base(payload),
|
|
177
|
+
workspace,
|
|
178
|
+
staff: false,
|
|
179
|
+
accessRoles: accessRolesIn(payload, context, workspace),
|
|
180
|
+
...clientIdOf(payload),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
function user(payload, context) {
|
|
184
|
+
assertNoActor(payload);
|
|
185
|
+
const workspace = optionalText(payload, WORKSPACE);
|
|
186
|
+
if (workspace === undefined)
|
|
187
|
+
return staff(payload, context);
|
|
188
|
+
return teammate(payload, workspace, context);
|
|
189
|
+
}
|
|
190
|
+
function machine(payload, project) {
|
|
191
|
+
assertNoActor(payload);
|
|
192
|
+
if (payload[WORKSPACE] !== undefined)
|
|
193
|
+
refuse();
|
|
194
|
+
const scopes = parseRoles(payload, project);
|
|
195
|
+
if (scopes === undefined)
|
|
196
|
+
return refuse();
|
|
197
|
+
return {
|
|
198
|
+
kind: 'machine',
|
|
199
|
+
...base(payload),
|
|
200
|
+
client: text(payload, CLIENT),
|
|
201
|
+
scopes: Object.keys(scopes),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function scopesOf(payload) {
|
|
205
|
+
const value = payload[SCOPE];
|
|
206
|
+
const scopes = typeof value === 'string' ? value.split(' ') : value;
|
|
207
|
+
if (!Array.isArray(scopes))
|
|
208
|
+
return [];
|
|
209
|
+
return scopes.filter((scope) => typeof scope === 'string');
|
|
210
|
+
}
|
|
211
|
+
function exchanged(payload) {
|
|
212
|
+
if (audienceOf(payload).length !== 1)
|
|
213
|
+
return refuse();
|
|
214
|
+
const exchange = exchangeOf(payload);
|
|
215
|
+
const scopes = scopesOf(payload);
|
|
216
|
+
return {
|
|
217
|
+
kind: 'exchanged',
|
|
218
|
+
...base(payload),
|
|
219
|
+
workspace: text(payload, WORKSPACE),
|
|
220
|
+
actor: checkChain(actorOf(payload[ACTOR], 1), exchange),
|
|
221
|
+
exchange,
|
|
222
|
+
...(scopes.length === 0 ? {} : { scopes }),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
function agentKey(payload) {
|
|
226
|
+
assertNoActor(payload);
|
|
227
|
+
return {
|
|
228
|
+
kind: 'agent-key',
|
|
229
|
+
...base(payload),
|
|
230
|
+
workspace: text(payload, WORKSPACE),
|
|
231
|
+
keyId: text(payload, AGENT_KEY_ID),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
export function read(payload, context) {
|
|
235
|
+
const kind = kindOf(payload);
|
|
236
|
+
switch (kind) {
|
|
237
|
+
case 'user':
|
|
238
|
+
return user(payload, context);
|
|
239
|
+
case 'machine':
|
|
240
|
+
return machine(payload, context.project);
|
|
241
|
+
case 'exchanged':
|
|
242
|
+
return exchanged(payload);
|
|
243
|
+
case 'agent-key':
|
|
244
|
+
return agentKey(payload);
|
|
245
|
+
default: {
|
|
246
|
+
const unknown = kind;
|
|
247
|
+
return unknown;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
//# sourceMappingURL=claims.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claims.js","sourceRoot":"","sources":["../src/claims.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAG5D,MAAM,IAAI,GAAG,0BAA0B,CAAC;AACxC,MAAM,aAAa,GAAG,6BAA6B,CAAC;AACpD,MAAM,UAAU,GAAG,0BAA0B,CAAC;AAC9C,MAAM,WAAW,GAAG,2BAA2B,CAAC;AAChD,MAAM,YAAY,GAAG,4BAA4B,CAAC;AAClD,MAAM,KAAK,GAAG,OAAO,CAAC;AACtB,MAAM,SAAS,GAAG,wBAAwB,CAAC;AAC3C,MAAM,IAAI,GAAG,uCAAuC,CAAC;AACrD,MAAM,KAAK,GAAG,KAAK,CAAC;AACpB,MAAM,MAAM,GAAG,WAAW,CAAC;AAC3B,MAAM,eAAe,GAAG,CAAC,CAAC;AAU1B,SAAS,MAAM;IACb,MAAM,IAAI,OAAO,CAAC,QAAQ,CAAC,CAAC;AAC9B,CAAC;AAED,SAAS,IAAI,CAAC,MAAc,EAAE,IAAY;IACxC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAC3B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,EAAE;QAAE,OAAO,MAAM,EAA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WAAW;YACd,OAAO,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC3B,OAAO,CAAC,CAAC,CAAC;YACR,MAAM,OAAO,GAAU,IAAI,CAAC;YAC5B,OAAO,OAAO,CAAC;QACjB,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface Endpoint {
|
|
2
|
+
url: string;
|
|
3
|
+
}
|
|
4
|
+
export interface Target {
|
|
5
|
+
url: string;
|
|
6
|
+
headers: Record<string, string>;
|
|
7
|
+
}
|
|
8
|
+
export declare function checkEndpoint(endpoint: Endpoint | undefined): void;
|
|
9
|
+
export declare function targetOf(issuer: string, endpoint: Endpoint | undefined): Target;
|
|
10
|
+
//# sourceMappingURL=endpoint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"endpoint.d.ts","sourceRoot":"","sources":["../src/endpoint.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,QAAQ;IACvB,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,MAAM;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjC;AAMD,wBAAgB,aAAa,CAAC,QAAQ,EAAE,QAAQ,GAAG,SAAS,GAAG,IAAI,CAGlE;AAED,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,SAAS,GAAG,MAAM,CAW/E"}
|
package/dist/endpoint.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { trimmed } from './assertion.js';
|
|
2
|
+
const TOKEN_PATH = '/oauth/v2/token';
|
|
3
|
+
function hostOf(issuer) {
|
|
4
|
+
return new URL(issuer).host;
|
|
5
|
+
}
|
|
6
|
+
export function checkEndpoint(endpoint) {
|
|
7
|
+
if (endpoint === undefined)
|
|
8
|
+
return;
|
|
9
|
+
if (!URL.canParse(endpoint.url))
|
|
10
|
+
throw new Error('The endpoint must be a URL');
|
|
11
|
+
}
|
|
12
|
+
export function targetOf(issuer, endpoint) {
|
|
13
|
+
if (endpoint === undefined)
|
|
14
|
+
return { url: `${trimmed(issuer)}${TOKEN_PATH}`, headers: {} };
|
|
15
|
+
const host = hostOf(issuer);
|
|
16
|
+
return {
|
|
17
|
+
url: `${trimmed(endpoint.url)}${TOKEN_PATH}`,
|
|
18
|
+
headers: {
|
|
19
|
+
'x-zitadel-instance-host': host,
|
|
20
|
+
'x-zitadel-public-host': host,
|
|
21
|
+
'x-forwarded-proto': 'https',
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=endpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"endpoint.js","sourceRoot":"","sources":["../src/endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzC,MAAM,UAAU,GAAG,iBAAiB,CAAC;AAarC,SAAS,MAAM,CAAC,MAAc;IAC5B,OAAO,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC;AAC9B,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,QAA8B;IAC1D,IAAI,QAAQ,KAAK,SAAS;QAAE,OAAO;IACnC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;AACjF,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,MAAc,EAAE,QAA8B;IACrE,IAAI,QAAQ,KAAK,SAAS;QAAE,OAAO,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,GAAG,UAAU,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;IAC3F,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5B,OAAO;QACL,GAAG,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,UAAU,EAAE;QAC5C,OAAO,EAAE;YACP,yBAAyB,EAAE,IAAI;YAC/B,uBAAuB,EAAE,IAAI;YAC7B,mBAAmB,EAAE,OAAO;SAC7B;KACF,CAAC;AACJ,CAAC"}
|