@vercel/kms 0.0.0 → 0.1.1
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/CHANGELOG.md +13 -0
- package/LICENSE +202 -0
- package/README.md +79 -2
- package/dist/errors.d.ts +20 -0
- package/dist/errors.js +41 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +34 -0
- package/dist/request.d.ts +41 -0
- package/dist/request.js +105 -0
- package/dist/request.test.js +39 -0
- package/dist/resolve-token.d.ts +9 -0
- package/dist/resolve-token.js +36 -0
- package/dist/sign-message.d.ts +39 -0
- package/dist/sign-message.js +47 -0
- package/dist/sign-message.test.d.ts +1 -0
- package/dist/sign-message.test.js +97 -0
- package/dist/sign-token.d.ts +37 -0
- package/dist/sign-token.js +44 -0
- package/dist/sign-token.test.d.ts +1 -0
- package/dist/sign-token.test.js +82 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +28 -0
- package/docs/README.md +20 -0
- package/docs/classes/VercelKmsError.md +190 -0
- package/docs/functions/signMessage.md +27 -0
- package/docs/functions/signToken.md +26 -0
- package/docs/interfaces/FlattenedJWS.md +53 -0
- package/docs/interfaces/SignMessageOptions.md +71 -0
- package/docs/interfaces/SignTokenOptions.md +89 -0
- package/package.json +46 -11
- /package/{index.js → dist/request.test.d.ts} +0 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @vercel/kms
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- @vercel/oidc@3.8.5
|
|
8
|
+
|
|
9
|
+
## 0.1.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 08ffe7d: Initial release of `@vercel/kms`: runtime helpers `signToken` and `signMessage` for signing JWTs and raw messages via the Vercel KMS API.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2017 Vercel, Inc.
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@vercel/kms`
|
|
2
2
|
|
|
3
|
-
KMS
|
|
3
|
+
Runtime KMS signing helpers intended to be used with your Vercel Functions.
|
|
4
|
+
|
|
5
|
+
`@vercel/kms` calls the Vercel KMS signing API to sign JWTs and arbitrary
|
|
6
|
+
messages with an issuer's managed signing key. It authenticates using the
|
|
7
|
+
function's [Vercel OIDC token](https://vercel.com/docs/oidc) (fetched
|
|
8
|
+
automatically via [`@vercel/oidc`](https://www.npmjs.com/package/@vercel/oidc)).
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
### Signing a token
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { signToken } from '@vercel/kms';
|
|
16
|
+
|
|
17
|
+
// Returns a compact JWT string signed by the issuer.
|
|
18
|
+
const token = await signToken({
|
|
19
|
+
issuerId: 'issuer_abc123',
|
|
20
|
+
claims: { sub: 'user_123', scope: 'read:data' },
|
|
21
|
+
ttl: 300, // seconds; defaults to 300
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Signing a message
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { signMessage } from '@vercel/kms';
|
|
29
|
+
|
|
30
|
+
// `message` is a UTF-8 string or raw bytes (Uint8Array); it is base64-encoded
|
|
31
|
+
// internally. Returns a JOSE Flattened JWS.
|
|
32
|
+
const signature = await signMessage({
|
|
33
|
+
issuerId: 'issuer_abc123',
|
|
34
|
+
message: 'hello world',
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Providing a token explicitly
|
|
39
|
+
|
|
40
|
+
By default the OIDC token is fetched via `getVercelOidcToken()`. To supply your
|
|
41
|
+
own token, pass `token`:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
const token = await signToken({
|
|
45
|
+
issuerId: 'issuer_abc123',
|
|
46
|
+
token: myOidcToken,
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## API
|
|
51
|
+
|
|
52
|
+
### `signToken(options)`
|
|
53
|
+
|
|
54
|
+
Signs a JWT for an issuer.
|
|
55
|
+
|
|
56
|
+
**Options:**
|
|
57
|
+
|
|
58
|
+
- `issuerId: string` — The ID of the issuer.
|
|
59
|
+
- `claims?: Record<string, unknown>` — Claims to include in the token.
|
|
60
|
+
- `headers?: Record<string, unknown>` — Additional JWT headers.
|
|
61
|
+
- `ttl?: number | null` — Time-to-live in seconds (default: `300`).
|
|
62
|
+
- `token?: string` — Explicit OIDC token; skips automatic retrieval.
|
|
63
|
+
- `region?: string` — Region for the regional KMS API host, e.g. `sfo1`, producing `https://api-<region>.vercel.com/v1`. Defaults to the `VERCEL_REGION` environment variable, falling back to `https://api.vercel.com/v1`. Ignored when `baseUrl` is set.
|
|
64
|
+
- `baseUrl?: string` — Override the API base URL. Takes precedence over `region`.
|
|
65
|
+
|
|
66
|
+
Returns `Promise<string>` — the compact JWT.
|
|
67
|
+
|
|
68
|
+
### `signMessage(options)`
|
|
69
|
+
|
|
70
|
+
Signs a message for an issuer.
|
|
71
|
+
|
|
72
|
+
**Options:**
|
|
73
|
+
|
|
74
|
+
- `issuerId: string` — The ID of the issuer.
|
|
75
|
+
- `message: Uint8Array | string` — The message to sign. A `string` is treated as UTF-8 text; a `Uint8Array` is signed as raw bytes. The value is base64-encoded internally before being sent to the KMS API.
|
|
76
|
+
- `token?: string` — Explicit OIDC token; skips automatic retrieval.
|
|
77
|
+
- `region?: string` — Region for the regional KMS API host, e.g. `sfo1`, producing `https://api-<region>.vercel.com/v1`. Defaults to the `VERCEL_REGION` environment variable, falling back to `https://api.vercel.com/v1`. Ignored when `baseUrl` is set.
|
|
78
|
+
- `baseUrl?: string` — Override the API base URL. Takes precedence over `region`.
|
|
79
|
+
|
|
80
|
+
Returns `Promise<FlattenedJWS>` — the JOSE Flattened JWS.
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when a Vercel KMS request fails. Mirrors the API error envelope
|
|
3
|
+
* `{ error: { code, message, ...meta } }`: `code` and `message` come from the
|
|
4
|
+
* response body when present, and any remaining fields are exposed on
|
|
5
|
+
* {@link VercelKmsError.metadata}.
|
|
6
|
+
*/
|
|
7
|
+
export declare class VercelKmsError extends Error {
|
|
8
|
+
/** HTTP status code of the failed response. */
|
|
9
|
+
readonly status: number;
|
|
10
|
+
/** Machine-readable error code from the API (e.g. `issuer_not_found`). */
|
|
11
|
+
readonly code: string;
|
|
12
|
+
/** Any additional fields the API attached to the error envelope. */
|
|
13
|
+
readonly metadata: Record<string, unknown>;
|
|
14
|
+
constructor({ status, code, message, metadata, }: {
|
|
15
|
+
status: number;
|
|
16
|
+
code: string;
|
|
17
|
+
message: string;
|
|
18
|
+
metadata?: Record<string, unknown>;
|
|
19
|
+
});
|
|
20
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var errors_exports = {};
|
|
20
|
+
__export(errors_exports, {
|
|
21
|
+
VercelKmsError: () => VercelKmsError
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(errors_exports);
|
|
24
|
+
class VercelKmsError extends Error {
|
|
25
|
+
constructor({
|
|
26
|
+
status,
|
|
27
|
+
code,
|
|
28
|
+
message,
|
|
29
|
+
metadata = {}
|
|
30
|
+
}) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = "VercelKmsError";
|
|
33
|
+
this.status = status;
|
|
34
|
+
this.code = code;
|
|
35
|
+
this.metadata = metadata;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
39
|
+
0 && (module.exports = {
|
|
40
|
+
VercelKmsError
|
|
41
|
+
});
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var src_exports = {};
|
|
20
|
+
__export(src_exports, {
|
|
21
|
+
VercelKmsError: () => import_errors.VercelKmsError,
|
|
22
|
+
signMessage: () => import_sign_message.signMessage,
|
|
23
|
+
signToken: () => import_sign_token.signToken
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
var import_sign_token = require("./sign-token");
|
|
27
|
+
var import_sign_message = require("./sign-message");
|
|
28
|
+
var import_errors = require("./errors");
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
VercelKmsError,
|
|
32
|
+
signMessage,
|
|
33
|
+
signToken
|
|
34
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Non-regional fallback base URL for the Vercel KMS API. The sign endpoints are
|
|
3
|
+
* exposed under `/kms/issuers/:issuerId/sign/*` relative to this base.
|
|
4
|
+
*/
|
|
5
|
+
export declare const API_BASE_URL = "https://api.vercel.com/v1";
|
|
6
|
+
/**
|
|
7
|
+
* Resolves the KMS API base URL. When an explicit `baseUrl` is provided it wins
|
|
8
|
+
* (used as an escape hatch / test override). Otherwise the region — from the
|
|
9
|
+
* `region` argument, then the `VERCEL_REGION` environment variable — selects the
|
|
10
|
+
* regional host `https://api-${region}.vercel.com/v1`. When no region is
|
|
11
|
+
* available, falls back to the global {@link API_BASE_URL}.
|
|
12
|
+
*/
|
|
13
|
+
export declare function resolveBaseUrl({ region, baseUrl, }: {
|
|
14
|
+
region?: string;
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
}): string;
|
|
17
|
+
/**
|
|
18
|
+
* A JOSE Flattened JWS, as returned by the message-signing endpoint.
|
|
19
|
+
*
|
|
20
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7515#section-7.2.2
|
|
21
|
+
*/
|
|
22
|
+
export interface FlattenedJWS {
|
|
23
|
+
/** Base64url-encoded JWS payload. */
|
|
24
|
+
payload: string;
|
|
25
|
+
/** Base64url-encoded JWS signature. */
|
|
26
|
+
signature: string;
|
|
27
|
+
/** Base64url-encoded protected header. */
|
|
28
|
+
protected?: string;
|
|
29
|
+
/** Optional unprotected JWS header. */
|
|
30
|
+
header?: Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Performs an authenticated POST to a Vercel KMS sign endpoint and returns the
|
|
34
|
+
* parsed JSON body. Throws {@link VercelKmsError} on a non-2xx response.
|
|
35
|
+
*/
|
|
36
|
+
export declare function postJson<T>({ baseUrl, path, token, body, }: {
|
|
37
|
+
baseUrl: string;
|
|
38
|
+
path: string;
|
|
39
|
+
token: string;
|
|
40
|
+
body: unknown;
|
|
41
|
+
}): Promise<T>;
|
package/dist/request.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var request_exports = {};
|
|
20
|
+
__export(request_exports, {
|
|
21
|
+
API_BASE_URL: () => API_BASE_URL,
|
|
22
|
+
postJson: () => postJson,
|
|
23
|
+
resolveBaseUrl: () => resolveBaseUrl
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(request_exports);
|
|
26
|
+
var import_errors = require("./errors");
|
|
27
|
+
var import_version = require("./version");
|
|
28
|
+
const API_BASE_URL = "https://api.vercel.com/v1";
|
|
29
|
+
function resolveBaseUrl({
|
|
30
|
+
region,
|
|
31
|
+
baseUrl
|
|
32
|
+
}) {
|
|
33
|
+
if (baseUrl) {
|
|
34
|
+
return baseUrl;
|
|
35
|
+
}
|
|
36
|
+
const resolved = region ?? process.env.VERCEL_REGION;
|
|
37
|
+
return resolved ? `https://api-${resolved}.vercel.com/v1` : API_BASE_URL;
|
|
38
|
+
}
|
|
39
|
+
async function postJson({
|
|
40
|
+
baseUrl,
|
|
41
|
+
path,
|
|
42
|
+
token,
|
|
43
|
+
body
|
|
44
|
+
}) {
|
|
45
|
+
const response = await fetch(new URL(`${baseUrl}${path}`), {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers: {
|
|
48
|
+
accept: "application/json",
|
|
49
|
+
"content-type": "application/json",
|
|
50
|
+
authorization: `Bearer ${token}`,
|
|
51
|
+
"user-agent": `@vercel/kms@${import_version.version}`
|
|
52
|
+
},
|
|
53
|
+
body: JSON.stringify(body)
|
|
54
|
+
});
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
throw await createVercelKmsError(response);
|
|
57
|
+
}
|
|
58
|
+
return await response.json();
|
|
59
|
+
}
|
|
60
|
+
async function createVercelKmsError(response) {
|
|
61
|
+
const text = await response.text();
|
|
62
|
+
const parsed = parseJsonObject(text);
|
|
63
|
+
const apiError = isRecord(parsed?.error) ? parsed.error : null;
|
|
64
|
+
if (apiError) {
|
|
65
|
+
const { code, message, ...metadata } = apiError;
|
|
66
|
+
return new import_errors.VercelKmsError({
|
|
67
|
+
status: response.status,
|
|
68
|
+
code: typeof code === "string" ? code : "request_failed",
|
|
69
|
+
message: typeof message === "string" ? message : getFallbackErrorMessage(response),
|
|
70
|
+
metadata
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return new import_errors.VercelKmsError({
|
|
74
|
+
status: response.status,
|
|
75
|
+
code: "request_failed",
|
|
76
|
+
message: getFallbackErrorMessage(response),
|
|
77
|
+
metadata: {
|
|
78
|
+
...isRecord(parsed) ? { body: parsed } : {},
|
|
79
|
+
...!parsed && text ? { body: text } : {}
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
function getFallbackErrorMessage(response) {
|
|
84
|
+
return `Vercel KMS request failed with ${response.status} ${response.statusText}`;
|
|
85
|
+
}
|
|
86
|
+
function parseJsonObject(text) {
|
|
87
|
+
if (!text) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const parsed = JSON.parse(text);
|
|
92
|
+
return isRecord(parsed) ? parsed : null;
|
|
93
|
+
} catch {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function isRecord(value) {
|
|
98
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
99
|
+
}
|
|
100
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
101
|
+
0 && (module.exports = {
|
|
102
|
+
API_BASE_URL,
|
|
103
|
+
postJson,
|
|
104
|
+
resolveBaseUrl
|
|
105
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var import_vitest = require("vitest");
|
|
3
|
+
var import_request = require("./request");
|
|
4
|
+
const originalRegion = process.env.VERCEL_REGION;
|
|
5
|
+
(0, import_vitest.afterEach)(() => {
|
|
6
|
+
if (originalRegion === void 0) {
|
|
7
|
+
delete process.env.VERCEL_REGION;
|
|
8
|
+
} else {
|
|
9
|
+
process.env.VERCEL_REGION = originalRegion;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
(0, import_vitest.describe)("resolveBaseUrl", () => {
|
|
13
|
+
(0, import_vitest.test)("builds a regional host from an explicit region", () => {
|
|
14
|
+
delete process.env.VERCEL_REGION;
|
|
15
|
+
(0, import_vitest.expect)((0, import_request.resolveBaseUrl)({ region: "sfo1" })).toBe(
|
|
16
|
+
"https://api-sfo1.vercel.com/v1"
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
(0, import_vitest.test)("defaults the region to the VERCEL_REGION env var", () => {
|
|
20
|
+
process.env.VERCEL_REGION = "iad1";
|
|
21
|
+
(0, import_vitest.expect)((0, import_request.resolveBaseUrl)({})).toBe("https://api-iad1.vercel.com/v1");
|
|
22
|
+
});
|
|
23
|
+
(0, import_vitest.test)("prefers an explicit region over VERCEL_REGION", () => {
|
|
24
|
+
process.env.VERCEL_REGION = "iad1";
|
|
25
|
+
(0, import_vitest.expect)((0, import_request.resolveBaseUrl)({ region: "sfo1" })).toBe(
|
|
26
|
+
"https://api-sfo1.vercel.com/v1"
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
(0, import_vitest.test)("falls back to the global host when no region is available", () => {
|
|
30
|
+
delete process.env.VERCEL_REGION;
|
|
31
|
+
(0, import_vitest.expect)((0, import_request.resolveBaseUrl)({})).toBe("https://api.vercel.com/v1");
|
|
32
|
+
});
|
|
33
|
+
(0, import_vitest.test)("an explicit baseUrl takes precedence over region and env", () => {
|
|
34
|
+
process.env.VERCEL_REGION = "iad1";
|
|
35
|
+
(0, import_vitest.expect)(
|
|
36
|
+
(0, import_request.resolveBaseUrl)({ region: "sfo1", baseUrl: "https://example.test/v1" })
|
|
37
|
+
).toBe("https://example.test/v1");
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the OIDC token used to authenticate a sign request. When an explicit
|
|
3
|
+
* `token` is supplied it is used as-is; otherwise the function's Vercel OIDC
|
|
4
|
+
* token is fetched via `@vercel/oidc`. The KMS API validates the token's
|
|
5
|
+
* original audience, so the token is used without an audience exchange.
|
|
6
|
+
*/
|
|
7
|
+
export declare function resolveOidcToken({ token, }: {
|
|
8
|
+
token?: string;
|
|
9
|
+
}): Promise<string>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var resolve_token_exports = {};
|
|
20
|
+
__export(resolve_token_exports, {
|
|
21
|
+
resolveOidcToken: () => resolveOidcToken
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(resolve_token_exports);
|
|
24
|
+
var import_oidc = require("@vercel/oidc");
|
|
25
|
+
async function resolveOidcToken({
|
|
26
|
+
token
|
|
27
|
+
}) {
|
|
28
|
+
if (token) {
|
|
29
|
+
return token;
|
|
30
|
+
}
|
|
31
|
+
return (0, import_oidc.getVercelOidcToken)();
|
|
32
|
+
}
|
|
33
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
+
0 && (module.exports = {
|
|
35
|
+
resolveOidcToken
|
|
36
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type FlattenedJWS } from './request';
|
|
2
|
+
/**
|
|
3
|
+
* Options for {@link signMessage}.
|
|
4
|
+
*/
|
|
5
|
+
export interface SignMessageOptions {
|
|
6
|
+
/** The ID of the issuer whose signing key should sign the message. */
|
|
7
|
+
issuerId: string;
|
|
8
|
+
/**
|
|
9
|
+
* The message to sign. Provide raw bytes as a `Uint8Array`, or a `string`
|
|
10
|
+
* which is treated as UTF-8 text. The value is base64-encoded internally
|
|
11
|
+
* before being sent to the KMS API.
|
|
12
|
+
*/
|
|
13
|
+
message: Uint8Array | string;
|
|
14
|
+
/**
|
|
15
|
+
* An explicit Vercel OIDC token to authenticate with. When omitted, the
|
|
16
|
+
* function's OIDC token is fetched automatically via `@vercel/oidc`.
|
|
17
|
+
*/
|
|
18
|
+
token?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Region for the regional KMS API host, e.g. `sfo1`, producing
|
|
21
|
+
* `https://api-<region>.vercel.com/v1`. Defaults to the `VERCEL_REGION`
|
|
22
|
+
* environment variable, falling back to the global `api.vercel.com` host.
|
|
23
|
+
* Ignored when `baseUrl` is provided.
|
|
24
|
+
*/
|
|
25
|
+
region?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Override the API base URL. Takes precedence over `region`.
|
|
28
|
+
* @default "https://api-<region>.vercel.com/v1" or "https://api.vercel.com/v1"
|
|
29
|
+
*/
|
|
30
|
+
baseUrl?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Signs a message for an issuer using its managed signing key and returns the
|
|
34
|
+
* resulting JOSE Flattened JWS. The message may be provided as raw bytes
|
|
35
|
+
* (`Uint8Array`) or a UTF-8 `string`; it is base64-encoded before being sent.
|
|
36
|
+
*
|
|
37
|
+
* @throws {import('./errors').VercelKmsError} If the sign request fails.
|
|
38
|
+
*/
|
|
39
|
+
export declare function signMessage(options: SignMessageOptions): Promise<FlattenedJWS>;
|