@hypequery/agent 0.0.0-canary-20261114145248
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 +201 -0
- package/README.md +59 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/mint/base64url.d.ts +3 -0
- package/dist/mint/base64url.d.ts.map +1 -0
- package/dist/mint/base64url.js +7 -0
- package/dist/mint/claims.d.ts +37 -0
- package/dist/mint/claims.d.ts.map +1 -0
- package/dist/mint/claims.js +124 -0
- package/dist/mint/endpoint.d.ts +2 -0
- package/dist/mint/endpoint.d.ts.map +1 -0
- package/dist/mint/endpoint.js +52 -0
- package/dist/mint/errors.d.ts +12 -0
- package/dist/mint/errors.d.ts.map +1 -0
- package/dist/mint/errors.js +13 -0
- package/dist/mint/index.d.ts +6 -0
- package/dist/mint/index.d.ts.map +1 -0
- package/dist/mint/index.js +3 -0
- package/dist/mint/key.d.ts +36 -0
- package/dist/mint/key.d.ts.map +1 -0
- package/dist/mint/key.js +55 -0
- package/dist/mint/mint.d.ts +58 -0
- package/dist/mint/mint.d.ts.map +1 -0
- package/dist/mint/mint.js +91 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are advised to review the applicable
|
|
150
|
+
documentation for the specific language governing permissions and
|
|
151
|
+
limitations under each license.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024 hypequery
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# @hypequery/agent
|
|
2
|
+
|
|
3
|
+
Put a hypequery Cloud project's tenant-safe analytics in your own agent.
|
|
4
|
+
|
|
5
|
+
`mintAgentToken()` signs an end-user token in your backend with a project's
|
|
6
|
+
managed signing key. It makes no network call, so you can mint one per
|
|
7
|
+
session or per request. The tenant comes from the signed token, never from the
|
|
8
|
+
model, so a prompt can't reach another customer's data.
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @hypequery/agent
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { mintAgentToken } from '@hypequery/agent';
|
|
16
|
+
|
|
17
|
+
// In your backend, when a session starts.
|
|
18
|
+
const { token, mcpUrl, expiresAt } = await mintAgentToken({
|
|
19
|
+
signingKey: process.env.HYPEQUERY_AGENT_KEY!,
|
|
20
|
+
tenant: session.orgId,
|
|
21
|
+
subject: session.userId,
|
|
22
|
+
permissionSet: 'customers',
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Any MCP client: send the token as a bearer token to mcpUrl.
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Options
|
|
29
|
+
|
|
30
|
+
| Option | |
|
|
31
|
+
| --- | --- |
|
|
32
|
+
| `signingKey` | The private key JSON Cloud showed when you created the key (Settings → Customer access → Signing keys for in-app agents). Keep it in `HYPEQUERY_AGENT_KEY`. |
|
|
33
|
+
| `tenant` | The end user's customer. Every query is scoped to it. |
|
|
34
|
+
| `subject` | The end user, as your app identifies them. |
|
|
35
|
+
| `permissionSet` | The permission set the end user acts with, by name. Required. |
|
|
36
|
+
| `constraints` | Optional filters every query carries: `{ filter, operator: 'eq', value }` or `{ filter, operator: 'in', value: [...] }`, at most 8. |
|
|
37
|
+
| `expiresInSeconds` | Lifetime, 1 to 900. Defaults to 900 (15 minutes, Cloud's ceiling). |
|
|
38
|
+
| `project` | Optional. The project's protocol name (for example `acme:logistics`). Defaults to the key's `hq_project`. |
|
|
39
|
+
| `endpoint` | Optional. The project's customer endpoint, `https://<endpoint>.hypequery.app`, or its `/mcp` URL. Defaults to the key's `hq_endpoint`. https only, except on localhost. |
|
|
40
|
+
|
|
41
|
+
An explicit `project` or `endpoint` wins over the key's own. If neither gives
|
|
42
|
+
one, `mintAgentToken()` throws `AgentTokenError` naming it.
|
|
43
|
+
|
|
44
|
+
It returns `{ token, mcpUrl, expiresAt }`. Mint a new token before
|
|
45
|
+
`expiresAt`. An option Cloud would refuse throws `AgentTokenError`, whose
|
|
46
|
+
`field` names it.
|
|
47
|
+
|
|
48
|
+
The token is an EdDSA JWT whose `kid` names your key, with audience
|
|
49
|
+
`hypequery-agent:<project>`. Python's `hypequery.agent.mint_agent_token()`
|
|
50
|
+
mints the same token for the same input.
|
|
51
|
+
|
|
52
|
+
Runs on Node.js 20 or later and on runtimes with Web Crypto Ed25519 (Deno,
|
|
53
|
+
Bun, Cloudflare Workers, Vercel Edge).
|
|
54
|
+
|
|
55
|
+
Docs: https://hypequery.com/docs/cloud/in-app-agents
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
|
|
59
|
+
Apache-2.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './mint/index.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64url.d.ts","sourceRoot":"","sources":["../../src/mint/base64url.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAIzD"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Unpadded base64url (RFC 7515 §2), without Buffer so it runs on edge runtimes. */
|
|
2
|
+
export function base64UrlEncode(bytes) {
|
|
3
|
+
let binary = '';
|
|
4
|
+
for (const byte of bytes)
|
|
5
|
+
binary += String.fromCharCode(byte);
|
|
6
|
+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
7
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The claim rules Cloud applies to a managed-key token (AG-18a, the same as
|
|
3
|
+
* for a customer JWT). A mint that breaks one throws instead of producing a
|
|
4
|
+
* token the tenant endpoint refuses. `python/hypequery/src/hypequery/agent`
|
|
5
|
+
* applies the same rules, pinned by `specs/agent-token/fixtures/mint-v1`.
|
|
6
|
+
*/
|
|
7
|
+
/** The longest lifetime (`exp` − `iat`) Cloud accepts: 15 minutes. */
|
|
8
|
+
export declare const AGENT_TOKEN_MAX_LIFETIME_SECONDS: number;
|
|
9
|
+
export declare const MAX_AGENT_CONSTRAINTS = 8;
|
|
10
|
+
export declare const MAX_AGENT_CONSTRAINT_VALUES = 100;
|
|
11
|
+
export type AgentConstraintScalar = string | number | boolean;
|
|
12
|
+
/**
|
|
13
|
+
* A filter every query the end user makes carries. It names one of the
|
|
14
|
+
* dataset's declared filters; a dataset without it refuses the query rather
|
|
15
|
+
* than skip the constraint.
|
|
16
|
+
*/
|
|
17
|
+
export type AgentConstraint = {
|
|
18
|
+
readonly filter: string;
|
|
19
|
+
readonly operator: 'eq';
|
|
20
|
+
readonly value: AgentConstraintScalar;
|
|
21
|
+
} | {
|
|
22
|
+
readonly filter: string;
|
|
23
|
+
readonly operator: 'in';
|
|
24
|
+
readonly value: readonly AgentConstraintScalar[];
|
|
25
|
+
};
|
|
26
|
+
export declare function checkProject(value: unknown): string;
|
|
27
|
+
export declare function checkSubject(value: unknown): string;
|
|
28
|
+
export declare function checkTenant(value: unknown): string;
|
|
29
|
+
export declare function checkPermissionSet(value: unknown): string;
|
|
30
|
+
export declare function checkLifetime(value: unknown): number;
|
|
31
|
+
/**
|
|
32
|
+
* Copies the constraints into the claim's exact shape (`filter`, `operator`,
|
|
33
|
+
* `value`, in that order), refusing anything Cloud would refuse: a token
|
|
34
|
+
* whose constraints don't parse is refused outright.
|
|
35
|
+
*/
|
|
36
|
+
export declare function checkConstraints(value: unknown): AgentConstraint[];
|
|
37
|
+
//# sourceMappingURL=claims.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claims.d.ts","sourceRoot":"","sources":["../../src/mint/claims.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AAEH,sEAAsE;AACtE,eAAO,MAAM,gCAAgC,QAAU,CAAC;AACxD,eAAO,MAAM,qBAAqB,IAAI,CAAC;AACvC,eAAO,MAAM,2BAA2B,MAAM,CAAC;AAU/C,MAAM,MAAM,qBAAqB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAE9D;;;;GAIG;AACH,MAAM,MAAM,eAAe,GACvB;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,qBAAqB,CAAA;CAAE,GAC3F;IACE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,SAAS,qBAAqB,EAAE,CAAC;CAClD,CAAC;AAEN,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAQnD;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAQnD;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAQlD;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAQzD;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAapD;AA4BD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,eAAe,EAAE,CAuDlE"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { AgentTokenError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* The claim rules Cloud applies to a managed-key token (AG-18a, the same as
|
|
4
|
+
* for a customer JWT). A mint that breaks one throws instead of producing a
|
|
5
|
+
* token the tenant endpoint refuses. `python/hypequery/src/hypequery/agent`
|
|
6
|
+
* applies the same rules, pinned by `specs/agent-token/fixtures/mint-v1`.
|
|
7
|
+
*/
|
|
8
|
+
/** The longest lifetime (`exp` − `iat`) Cloud accepts: 15 minutes. */
|
|
9
|
+
export const AGENT_TOKEN_MAX_LIFETIME_SECONDS = 15 * 60;
|
|
10
|
+
export const MAX_AGENT_CONSTRAINTS = 8;
|
|
11
|
+
export const MAX_AGENT_CONSTRAINT_VALUES = 100;
|
|
12
|
+
/** A protocol target token: the project's protocol name, at most 128 bytes. */
|
|
13
|
+
const PROJECT_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
|
|
14
|
+
const SUBJECT_PATTERN = /^[\x21-\x7e]{1,128}$/;
|
|
15
|
+
const TENANT_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
16
|
+
const PERMISSION_SET_PATTERN = /^[a-z][a-z0-9-]{0,63}$/;
|
|
17
|
+
const FILTER_PATTERN = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/;
|
|
18
|
+
const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
|
|
19
|
+
export function checkProject(value) {
|
|
20
|
+
if (typeof value !== 'string' || !PROJECT_PATTERN.test(value)) {
|
|
21
|
+
throw new AgentTokenError('project', 'project must be the project\'s protocol name as Cloud shows it (for example "acme:logistics").');
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
export function checkSubject(value) {
|
|
26
|
+
if (typeof value !== 'string' || !SUBJECT_PATTERN.test(value)) {
|
|
27
|
+
throw new AgentTokenError('subject', 'subject must be 1 to 128 printable ASCII characters, without spaces.');
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
export function checkTenant(value) {
|
|
32
|
+
if (typeof value !== 'string' || !TENANT_PATTERN.test(value)) {
|
|
33
|
+
throw new AgentTokenError('tenant', 'tenant must be 1 to 128 characters of letters, digits, ".", "_", ":" or "-".');
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
export function checkPermissionSet(value) {
|
|
38
|
+
if (typeof value !== 'string' || !PERMISSION_SET_PATTERN.test(value)) {
|
|
39
|
+
throw new AgentTokenError('permissionSet', 'permissionSet must name one of the project\'s permission sets (lowercase letters, digits and "-").');
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
export function checkLifetime(value) {
|
|
44
|
+
if (typeof value !== 'number' ||
|
|
45
|
+
!Number.isInteger(value) ||
|
|
46
|
+
value < 1 ||
|
|
47
|
+
value > AGENT_TOKEN_MAX_LIFETIME_SECONDS) {
|
|
48
|
+
throw new AgentTokenError('expiresInSeconds', `expiresInSeconds must be a whole number of seconds from 1 to ${AGENT_TOKEN_MAX_LIFETIME_SECONDS}.`);
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
function isPlainObject(value) {
|
|
53
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
54
|
+
return false;
|
|
55
|
+
const prototype = Object.getPrototypeOf(value);
|
|
56
|
+
return prototype === Object.prototype || prototype === null;
|
|
57
|
+
}
|
|
58
|
+
function scalar(value, where) {
|
|
59
|
+
if (typeof value === 'boolean')
|
|
60
|
+
return value;
|
|
61
|
+
if (typeof value === 'number') {
|
|
62
|
+
if (Number.isSafeInteger(value))
|
|
63
|
+
return value;
|
|
64
|
+
throw new AgentTokenError('constraints', `${where} must be a safe integer, a string or a boolean.`);
|
|
65
|
+
}
|
|
66
|
+
if (typeof value === 'string' &&
|
|
67
|
+
value.length >= 1 &&
|
|
68
|
+
value.length <= 256 &&
|
|
69
|
+
!LONE_SURROGATE.test(value)) {
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
throw new AgentTokenError('constraints', `${where} must be a string of 1 to 256 characters, a safe integer or a boolean.`);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Copies the constraints into the claim's exact shape (`filter`, `operator`,
|
|
76
|
+
* `value`, in that order), refusing anything Cloud would refuse: a token
|
|
77
|
+
* whose constraints don't parse is refused outright.
|
|
78
|
+
*/
|
|
79
|
+
export function checkConstraints(value) {
|
|
80
|
+
if (!Array.isArray(value)) {
|
|
81
|
+
throw new AgentTokenError('constraints', 'constraints must be an array.');
|
|
82
|
+
}
|
|
83
|
+
if (value.length > MAX_AGENT_CONSTRAINTS) {
|
|
84
|
+
throw new AgentTokenError('constraints', `A token can carry at most ${MAX_AGENT_CONSTRAINTS} constraints.`);
|
|
85
|
+
}
|
|
86
|
+
const filters = new Set();
|
|
87
|
+
// Array.from visits holes too, so a sparse array is refused, not serialized as null.
|
|
88
|
+
return Array.from(value, (entry, index) => {
|
|
89
|
+
const where = `constraints[${index}]`;
|
|
90
|
+
if (!isPlainObject(entry)) {
|
|
91
|
+
throw new AgentTokenError('constraints', `${where} must be an object.`);
|
|
92
|
+
}
|
|
93
|
+
for (const key of Object.keys(entry)) {
|
|
94
|
+
if (key !== 'filter' && key !== 'operator' && key !== 'value') {
|
|
95
|
+
throw new AgentTokenError('constraints', `${where} has an unknown member "${key}".`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const { filter, operator } = entry;
|
|
99
|
+
if (typeof filter !== 'string' || !FILTER_PATTERN.test(filter)) {
|
|
100
|
+
throw new AgentTokenError('constraints', `${where}.filter must name a dataset filter.`);
|
|
101
|
+
}
|
|
102
|
+
if (filters.has(filter)) {
|
|
103
|
+
throw new AgentTokenError('constraints', `Each filter may be constrained once ("${filter}").`);
|
|
104
|
+
}
|
|
105
|
+
filters.add(filter);
|
|
106
|
+
if (operator === 'eq') {
|
|
107
|
+
return { filter, operator, value: scalar(entry.value, `${where}.value`) };
|
|
108
|
+
}
|
|
109
|
+
if (operator === 'in') {
|
|
110
|
+
const values = entry.value;
|
|
111
|
+
if (!Array.isArray(values) ||
|
|
112
|
+
values.length < 1 ||
|
|
113
|
+
values.length > MAX_AGENT_CONSTRAINT_VALUES) {
|
|
114
|
+
throw new AgentTokenError('constraints', `${where}.value must be a list of 1 to ${MAX_AGENT_CONSTRAINT_VALUES} values.`);
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
filter,
|
|
118
|
+
operator,
|
|
119
|
+
value: Array.from(values, (item, position) => scalar(item, `${where}.value[${position}]`)),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
throw new AgentTokenError('constraints', `${where}.operator must be "eq" or "in".`);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"endpoint.d.ts","sourceRoot":"","sources":["../../src/mint/endpoint.ts"],"names":[],"mappings":"AA8BA,wBAAgB,aAAa,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,CA4BvD"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { AgentTokenError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* The project's tenant MCP URL, from its customer endpoint: the origin
|
|
4
|
+
* (`https://<endpoint>.hypequery.app`) or the MCP URL itself, given as an
|
|
5
|
+
* option or read from the key's `hq_endpoint`. It is never looked up:
|
|
6
|
+
* minting makes no network call.
|
|
7
|
+
*
|
|
8
|
+
* Parsed by pattern rather than `URL`, so Python's `mint_agent_token()`
|
|
9
|
+
* resolves every input to the same URL or refuses the same inputs.
|
|
10
|
+
*/
|
|
11
|
+
const ENDPOINT_PATTERN = /^(https?):\/\/([A-Za-z0-9.-]+|\[::1\])(?::([0-9]{1,5}))?(\/|\/mcp)?$/;
|
|
12
|
+
const LABEL_PATTERN = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
13
|
+
const DEFAULT_PORTS = { https: '443', http: '80' };
|
|
14
|
+
function refuse(message) {
|
|
15
|
+
throw new AgentTokenError('endpoint', message);
|
|
16
|
+
}
|
|
17
|
+
/** Plain HTTP only reaches a gateway on this machine. */
|
|
18
|
+
function isLoopback(host) {
|
|
19
|
+
return (host === 'localhost' ||
|
|
20
|
+
host.endsWith('.localhost') ||
|
|
21
|
+
host === '127.0.0.1' ||
|
|
22
|
+
host === '[::1]');
|
|
23
|
+
}
|
|
24
|
+
export function resolveMcpUrl(endpoint) {
|
|
25
|
+
if (typeof endpoint !== 'string') {
|
|
26
|
+
refuse('endpoint must be the project\'s customer endpoint URL from Cloud.');
|
|
27
|
+
}
|
|
28
|
+
const match = ENDPOINT_PATTERN.exec(endpoint);
|
|
29
|
+
if (!match) {
|
|
30
|
+
refuse('endpoint must be the project\'s customer endpoint, for example ' +
|
|
31
|
+
'"https://acme-logistics-1f2e3d.hypequery.app", or its "/mcp" URL.');
|
|
32
|
+
}
|
|
33
|
+
const scheme = match[1];
|
|
34
|
+
const host = match[2].toLowerCase();
|
|
35
|
+
const port = match[3];
|
|
36
|
+
const validHost = host === '[::1]' ||
|
|
37
|
+
(host.length <= 253 && host.split('.').every((label) => LABEL_PATTERN.test(label)));
|
|
38
|
+
if (!validHost)
|
|
39
|
+
refuse('endpoint has an invalid host name.');
|
|
40
|
+
if (scheme === 'http' && !isLoopback(host)) {
|
|
41
|
+
refuse('endpoint must use https (plain http is allowed for localhost only).');
|
|
42
|
+
}
|
|
43
|
+
let authority = host;
|
|
44
|
+
if (port !== undefined) {
|
|
45
|
+
const number = Number(port);
|
|
46
|
+
if (number < 1 || number > 65_535)
|
|
47
|
+
refuse('endpoint has an invalid port.');
|
|
48
|
+
if (String(number) !== DEFAULT_PORTS[scheme])
|
|
49
|
+
authority = `${host}:${number}`;
|
|
50
|
+
}
|
|
51
|
+
return `${scheme}://${authority}/mcp`;
|
|
52
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** The option a refused mint names. */
|
|
2
|
+
export type AgentTokenField = 'signingKey' | 'project' | 'tenant' | 'subject' | 'permissionSet' | 'constraints' | 'expiresInSeconds' | 'endpoint' | 'now';
|
|
3
|
+
/**
|
|
4
|
+
* Thrown when `mintAgentToken()` is given an option the tenant endpoint would
|
|
5
|
+
* refuse. Nothing is minted: a token Cloud refuses fails later and less
|
|
6
|
+
* clearly. The message never contains key material.
|
|
7
|
+
*/
|
|
8
|
+
export declare class AgentTokenError extends Error {
|
|
9
|
+
readonly field: AgentTokenField;
|
|
10
|
+
constructor(field: AgentTokenField, message: string);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/mint/errors.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,MAAM,MAAM,eAAe,GACvB,YAAY,GACZ,SAAS,GACT,QAAQ,GACR,SAAS,GACT,eAAe,GACf,aAAa,GACb,kBAAkB,GAClB,UAAU,GACV,KAAK,CAAC;AAEV;;;;GAIG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IACxC,QAAQ,CAAC,KAAK,EAAE,eAAe,CAAC;gBAEpB,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM;CAKpD"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when `mintAgentToken()` is given an option the tenant endpoint would
|
|
3
|
+
* refuse. Nothing is minted: a token Cloud refuses fails later and less
|
|
4
|
+
* clearly. The message never contains key material.
|
|
5
|
+
*/
|
|
6
|
+
export class AgentTokenError extends Error {
|
|
7
|
+
field;
|
|
8
|
+
constructor(field, message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'AgentTokenError';
|
|
11
|
+
this.field = field;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { mintAgentToken, agentTokenAudience } from './mint.js';
|
|
2
|
+
export type { MintAgentTokenOptions, AgentToken } from './mint.js';
|
|
3
|
+
export { AGENT_TOKEN_MAX_LIFETIME_SECONDS, type AgentConstraint, type AgentConstraintScalar, } from './claims.js';
|
|
4
|
+
export { AgentTokenError, type AgentTokenField } from './errors.js';
|
|
5
|
+
export type { AgentSigningJwk } from './key.js';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mint/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC/D,YAAY,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EACL,gCAAgC,EAChC,KAAK,eAAe,EACpB,KAAK,qBAAqB,GAC3B,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,eAAe,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AACpE,YAAY,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A managed signing key's private half, as Cloud shows it once (AG-18a):
|
|
3
|
+
* an Ed25519 JWK with its key id.
|
|
4
|
+
*/
|
|
5
|
+
export interface AgentSigningJwk {
|
|
6
|
+
readonly kty: 'OKP';
|
|
7
|
+
readonly crv: 'Ed25519';
|
|
8
|
+
readonly x: string;
|
|
9
|
+
readonly d: string;
|
|
10
|
+
readonly kid: string;
|
|
11
|
+
readonly alg?: 'EdDSA';
|
|
12
|
+
readonly use?: 'sig';
|
|
13
|
+
/** The project's protocol name, which Cloud adds so minting needs no `project`. */
|
|
14
|
+
readonly hq_project?: string;
|
|
15
|
+
/** The project's customer endpoint, which Cloud adds so minting needs no `endpoint`. */
|
|
16
|
+
readonly hq_endpoint?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface ParsedSigningKey {
|
|
19
|
+
readonly kid: string;
|
|
20
|
+
readonly jwk: {
|
|
21
|
+
kty: 'OKP';
|
|
22
|
+
crv: 'Ed25519';
|
|
23
|
+
x: string;
|
|
24
|
+
d: string;
|
|
25
|
+
};
|
|
26
|
+
/** `hq_project` and `hq_endpoint` as found, checked only when used. */
|
|
27
|
+
readonly project: unknown;
|
|
28
|
+
readonly endpoint: unknown;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Reads the signing key from the JSON Cloud shows (or the object it parses
|
|
32
|
+
* to). Members it doesn't use are ignored. Only the shape is checked here;
|
|
33
|
+
* `mint.ts` checks that `x` is `d`'s public key, since Cloud verifies with `x`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function parseSigningKey(input: unknown): ParsedSigningKey;
|
|
36
|
+
//# sourceMappingURL=key.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key.d.ts","sourceRoot":"","sources":["../../src/mint/key.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,GAAG,EAAE,KAAK,CAAC;IACpB,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC;IACrB,mFAAmF;IACnF,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,wFAAwF;IACxF,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE;QAAE,GAAG,EAAE,KAAK,CAAC;QAAC,GAAG,EAAE,SAAS,CAAC;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACnE,uEAAuE;IACvE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;CAC5B;AAeD;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,gBAAgB,CAkChE"}
|
package/dist/mint/key.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { AgentTokenError } from './errors.js';
|
|
2
|
+
/** Cloud's key ids: `hqsk_` and 16 random bytes, base64url. */
|
|
3
|
+
const KID_PATTERN = /^hqsk_[A-Za-z0-9_-]{22}$/;
|
|
4
|
+
/**
|
|
5
|
+
* An Ed25519 key, public or private, is 32 bytes: 43 base64url characters,
|
|
6
|
+
* the last carrying four bits. Only the canonical encoding is taken, so both
|
|
7
|
+
* languages read the same bytes whatever their decoders do with the spare bits.
|
|
8
|
+
*/
|
|
9
|
+
const KEY_BYTES_PATTERN = /^[A-Za-z0-9_-]{42}[AEIMQUYcgkosw048]$/;
|
|
10
|
+
function refuse(message) {
|
|
11
|
+
throw new AgentTokenError('signingKey', message);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Reads the signing key from the JSON Cloud shows (or the object it parses
|
|
15
|
+
* to). Members it doesn't use are ignored. Only the shape is checked here;
|
|
16
|
+
* `mint.ts` checks that `x` is `d`'s public key, since Cloud verifies with `x`.
|
|
17
|
+
*/
|
|
18
|
+
export function parseSigningKey(input) {
|
|
19
|
+
let value = input;
|
|
20
|
+
if (typeof input === 'string') {
|
|
21
|
+
try {
|
|
22
|
+
// No trimming beyond JSON's own whitespace: Python's would differ.
|
|
23
|
+
value = JSON.parse(input);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
refuse('The signing key is not valid JSON. Use the private key exactly as Cloud showed it.');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
30
|
+
refuse('The signing key must be the private key JSON Cloud showed when it was created.');
|
|
31
|
+
}
|
|
32
|
+
const jwk = value;
|
|
33
|
+
if (jwk.kty !== 'OKP' || jwk.crv !== 'Ed25519') {
|
|
34
|
+
refuse('The signing key must be an Ed25519 key (kty "OKP", crv "Ed25519").');
|
|
35
|
+
}
|
|
36
|
+
if (jwk.alg !== undefined && jwk.alg !== 'EdDSA')
|
|
37
|
+
refuse('The signing key\'s alg must be "EdDSA".');
|
|
38
|
+
if (jwk.use !== undefined && jwk.use !== 'sig')
|
|
39
|
+
refuse('The signing key\'s use must be "sig".');
|
|
40
|
+
if (typeof jwk.kid !== 'string' || !KID_PATTERN.test(jwk.kid)) {
|
|
41
|
+
refuse('The signing key has no hypequery key id (kid "hqsk_…").');
|
|
42
|
+
}
|
|
43
|
+
if (typeof jwk.d !== 'string' || !KEY_BYTES_PATTERN.test(jwk.d)) {
|
|
44
|
+
refuse('The signing key has no private key (d). Use the private key, not the public one.');
|
|
45
|
+
}
|
|
46
|
+
if (typeof jwk.x !== 'string' || !KEY_BYTES_PATTERN.test(jwk.x)) {
|
|
47
|
+
refuse('The signing key has no public key (x).');
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
kid: jwk.kid,
|
|
51
|
+
jwk: { kty: 'OKP', crv: 'Ed25519', x: jwk.x, d: jwk.d },
|
|
52
|
+
project: jwk.hq_project,
|
|
53
|
+
endpoint: jwk.hq_endpoint,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type AgentConstraint } from './claims.js';
|
|
2
|
+
import { type AgentSigningJwk } from './key.js';
|
|
3
|
+
export interface MintAgentTokenOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The managed signing key's private key, as the JSON Cloud showed once
|
|
6
|
+
* (keep it in `HYPEQUERY_AGENT_KEY`), or that JSON parsed.
|
|
7
|
+
*/
|
|
8
|
+
readonly signingKey: string | AgentSigningJwk;
|
|
9
|
+
/**
|
|
10
|
+
* The project's protocol name (for example "acme:logistics"). Defaults to
|
|
11
|
+
* the key's `hq_project`; when both are given, this wins.
|
|
12
|
+
*/
|
|
13
|
+
readonly project?: string;
|
|
14
|
+
/**
|
|
15
|
+
* The project's customer endpoint: its origin
|
|
16
|
+
* (`https://<endpoint>.hypequery.app`) or its `/mcp` URL. `mcpUrl` is
|
|
17
|
+
* `<origin>/mcp`. Defaults to the key's `hq_endpoint`; when both are
|
|
18
|
+
* given, this wins.
|
|
19
|
+
*/
|
|
20
|
+
readonly endpoint?: string;
|
|
21
|
+
/** The end user's customer: every query is scoped to this tenant. */
|
|
22
|
+
readonly tenant: string;
|
|
23
|
+
/** The end user, as your app identifies them. */
|
|
24
|
+
readonly subject: string;
|
|
25
|
+
/** The permission set the end user acts with, by name. */
|
|
26
|
+
readonly permissionSet: string;
|
|
27
|
+
/** Filters every query carries, on top of the permission set. */
|
|
28
|
+
readonly constraints?: readonly AgentConstraint[];
|
|
29
|
+
/** Lifetime in seconds, at most 900 (15 minutes, Cloud's ceiling). Defaults to 900. */
|
|
30
|
+
readonly expiresInSeconds?: number;
|
|
31
|
+
/** The issue time; defaults to now. */
|
|
32
|
+
readonly now?: Date;
|
|
33
|
+
}
|
|
34
|
+
export interface AgentToken {
|
|
35
|
+
/** A compact JWS: send it as `Authorization: Bearer <token>`. */
|
|
36
|
+
readonly token: string;
|
|
37
|
+
/** The project's tenant MCP endpoint. */
|
|
38
|
+
readonly mcpUrl: string;
|
|
39
|
+
/** When the token stops working; mint a new one before then. */
|
|
40
|
+
readonly expiresAt: Date;
|
|
41
|
+
}
|
|
42
|
+
/** Cloud's audience for a project's managed-key tokens (AG-18a). */
|
|
43
|
+
export declare function agentTokenAudience(project: string): string;
|
|
44
|
+
/**
|
|
45
|
+
* Mints an end-user token for a project's tenant MCP endpoint, signed locally
|
|
46
|
+
* with a managed signing key (AG-18a, decision 0019 §15). No network call:
|
|
47
|
+
* call it per session or per request.
|
|
48
|
+
*
|
|
49
|
+
* The token is an EdDSA JWT whose `kid` names the key, with audience
|
|
50
|
+
* `hypequery-agent:<project>`, claims `sub`, `tenant`, `permission_set`,
|
|
51
|
+
* optional `constraints`, `iat` and `exp`, and a lifetime of at most 15
|
|
52
|
+
* minutes. For the same options and time, it is byte-for-byte the token
|
|
53
|
+
* Python's `mint_agent_token()` mints.
|
|
54
|
+
*
|
|
55
|
+
* @throws AgentTokenError when an option is one Cloud would refuse.
|
|
56
|
+
*/
|
|
57
|
+
export declare function mintAgentToken(options: MintAgentTokenOptions): Promise<AgentToken>;
|
|
58
|
+
//# sourceMappingURL=mint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mint.d.ts","sourceRoot":"","sources":["../../src/mint/mint.ts"],"names":[],"mappings":"AACA,OAAO,EAQL,KAAK,eAAe,EACrB,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAmB,KAAK,eAAe,EAAE,MAAM,UAAU,CAAC;AAEjE,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,eAAe,CAAC;IAC9C;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;;OAKG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,qEAAqE;IACrE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,iDAAiD;IACjD,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,0DAA0D;IAC1D,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,iEAAiE;IACjE,QAAQ,CAAC,WAAW,CAAC,EAAE,SAAS,eAAe,EAAE,CAAC;IAClD,uFAAuF;IACvF,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IACnC,uCAAuC;IACvC,QAAQ,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC;CACrB;AAED,MAAM,WAAW,UAAU;IACzB,iEAAiE;IACjE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,yCAAyC;IACzC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,gEAAgE;IAChE,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC;CAC1B;AAED,oEAAoE;AACpE,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAE1D;AAgBD;;;;;;;;;;;;GAYG;AACH,wBAAsB,cAAc,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,UAAU,CAAC,CA0ExF"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { base64UrlEncode } from './base64url.js';
|
|
2
|
+
import { AGENT_TOKEN_MAX_LIFETIME_SECONDS, checkConstraints, checkLifetime, checkPermissionSet, checkProject, checkSubject, checkTenant, } from './claims.js';
|
|
3
|
+
import { resolveMcpUrl } from './endpoint.js';
|
|
4
|
+
import { AgentTokenError } from './errors.js';
|
|
5
|
+
import { parseSigningKey } from './key.js';
|
|
6
|
+
/** Cloud's audience for a project's managed-key tokens (AG-18a). */
|
|
7
|
+
export function agentTokenAudience(project) {
|
|
8
|
+
return `hypequery-agent:${checkProject(project)}`;
|
|
9
|
+
}
|
|
10
|
+
const encoder = new TextEncoder();
|
|
11
|
+
function encodeJson(value) {
|
|
12
|
+
return base64UrlEncode(encoder.encode(JSON.stringify(value)));
|
|
13
|
+
}
|
|
14
|
+
function subtleCrypto() {
|
|
15
|
+
const subtle = globalThis.crypto?.subtle;
|
|
16
|
+
if (!subtle) {
|
|
17
|
+
throw new Error('mintAgentToken needs Web Crypto (globalThis.crypto.subtle): Node 20 or later.');
|
|
18
|
+
}
|
|
19
|
+
return subtle;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Mints an end-user token for a project's tenant MCP endpoint, signed locally
|
|
23
|
+
* with a managed signing key (AG-18a, decision 0019 §15). No network call:
|
|
24
|
+
* call it per session or per request.
|
|
25
|
+
*
|
|
26
|
+
* The token is an EdDSA JWT whose `kid` names the key, with audience
|
|
27
|
+
* `hypequery-agent:<project>`, claims `sub`, `tenant`, `permission_set`,
|
|
28
|
+
* optional `constraints`, `iat` and `exp`, and a lifetime of at most 15
|
|
29
|
+
* minutes. For the same options and time, it is byte-for-byte the token
|
|
30
|
+
* Python's `mint_agent_token()` mints.
|
|
31
|
+
*
|
|
32
|
+
* @throws AgentTokenError when an option is one Cloud would refuse.
|
|
33
|
+
*/
|
|
34
|
+
export async function mintAgentToken(options) {
|
|
35
|
+
const key = parseSigningKey(options.signingKey);
|
|
36
|
+
// An explicit option wins over the key's own member.
|
|
37
|
+
const project = options.project ?? key.project;
|
|
38
|
+
if (project === undefined) {
|
|
39
|
+
throw new AgentTokenError('project', 'No project: pass project, or use a signing key whose JSON has hq_project.');
|
|
40
|
+
}
|
|
41
|
+
const endpoint = options.endpoint ?? key.endpoint;
|
|
42
|
+
if (endpoint === undefined) {
|
|
43
|
+
throw new AgentTokenError('endpoint', 'No endpoint: pass endpoint, or use a signing key whose JSON has hq_endpoint.');
|
|
44
|
+
}
|
|
45
|
+
const audience = `hypequery-agent:${checkProject(project)}`;
|
|
46
|
+
const mcpUrl = resolveMcpUrl(endpoint);
|
|
47
|
+
const subject = checkSubject(options.subject);
|
|
48
|
+
const tenant = checkTenant(options.tenant);
|
|
49
|
+
const permissionSet = checkPermissionSet(options.permissionSet);
|
|
50
|
+
const constraints = options.constraints === undefined ? [] : checkConstraints(options.constraints);
|
|
51
|
+
const lifetime = checkLifetime(options.expiresInSeconds ?? AGENT_TOKEN_MAX_LIFETIME_SECONDS);
|
|
52
|
+
const now = options.now ?? new Date();
|
|
53
|
+
if (!(now instanceof Date) || Number.isNaN(now.getTime())) {
|
|
54
|
+
throw new AgentTokenError('now', 'now must be a valid Date.');
|
|
55
|
+
}
|
|
56
|
+
const issuedAt = Math.floor(now.getTime() / 1_000);
|
|
57
|
+
const expiresAt = issuedAt + lifetime;
|
|
58
|
+
// Fixed member order, so both languages serialize the same bytes.
|
|
59
|
+
const header = { alg: 'EdDSA', kid: key.kid, typ: 'JWT' };
|
|
60
|
+
const payload = {
|
|
61
|
+
aud: audience,
|
|
62
|
+
sub: subject,
|
|
63
|
+
tenant,
|
|
64
|
+
permission_set: permissionSet,
|
|
65
|
+
...(constraints.length > 0 ? { constraints } : {}),
|
|
66
|
+
iat: issuedAt,
|
|
67
|
+
exp: expiresAt,
|
|
68
|
+
};
|
|
69
|
+
const signingInput = `${encodeJson(header)}.${encodeJson(payload)}`;
|
|
70
|
+
const subtle = subtleCrypto();
|
|
71
|
+
let privateKey;
|
|
72
|
+
let publicKey;
|
|
73
|
+
try {
|
|
74
|
+
privateKey = await subtle.importKey('jwk', key.jwk, { name: 'Ed25519' }, false, ['sign']);
|
|
75
|
+
publicKey = await subtle.importKey('jwk', { kty: 'OKP', crv: 'Ed25519', x: key.jwk.x }, { name: 'Ed25519' }, false, ['verify']);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
throw new AgentTokenError('signingKey', 'The signing key is not a valid Ed25519 key.');
|
|
79
|
+
}
|
|
80
|
+
const data = encoder.encode(signingInput);
|
|
81
|
+
const signature = new Uint8Array(await subtle.sign({ name: 'Ed25519' }, privateKey, data));
|
|
82
|
+
// Cloud verifies with `x`: a key whose `x` isn't `d`'s would mint tokens it refuses.
|
|
83
|
+
if (!(await subtle.verify({ name: 'Ed25519' }, publicKey, signature, data))) {
|
|
84
|
+
throw new AgentTokenError('signingKey', 'The signing key\'s public key (x) does not match its private key (d).');
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
token: `${signingInput}.${base64UrlEncode(signature)}`,
|
|
88
|
+
mcpUrl,
|
|
89
|
+
expiresAt: new Date(expiresAt * 1_000),
|
|
90
|
+
};
|
|
91
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hypequery/agent",
|
|
3
|
+
"version": "0.0.0-canary-20261114145248",
|
|
4
|
+
"description": "Put a hypequery Cloud project's tenant-safe analytics in your own agent: mint end-user tokens locally",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"hypequery",
|
|
7
|
+
"agent",
|
|
8
|
+
"mcp",
|
|
9
|
+
"jwt",
|
|
10
|
+
"multi-tenant",
|
|
11
|
+
"analytics"
|
|
12
|
+
],
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=20"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^26.6.6",
|
|
34
|
+
"jose": "^6.2.15",
|
|
35
|
+
"typescript": "^6.0.3",
|
|
36
|
+
"vitest": "^5.1.0"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/hypequery/hypequery.git",
|
|
41
|
+
"directory": "packages/agent"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://hypequery.com",
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/hypequery/hypequery/issues"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsc --project tsconfig.json",
|
|
52
|
+
"dev": "tsc --project tsconfig.json --watch",
|
|
53
|
+
"lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\" \"type-tests/**/*.ts\"",
|
|
54
|
+
"test": "npm run test:types && npm run test:unit",
|
|
55
|
+
"test:unit": "vitest run",
|
|
56
|
+
"test:types": "tsc --project tsconfig.type-tests.json",
|
|
57
|
+
"types": "tsc --project tsconfig.json --noEmit"
|
|
58
|
+
}
|
|
59
|
+
}
|