@kubohiroya/turbowarp-cloudflare-worker 0.2.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 +373 -0
- package/README.ja.md +105 -0
- package/README.md +51 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +126 -0
- package/dist/cli.js.map +1 -0
- package/dist/extension-manifest.json +149 -0
- package/dist/generator.d.ts +2 -0
- package/dist/generator.js +776 -0
- package/dist/generator.js.map +1 -0
- package/dist/ir.d.ts +50 -0
- package/dist/ir.js +168 -0
- package/dist/ir.js.map +1 -0
- package/dist/jwt.d.ts +23 -0
- package/dist/jwt.js +81 -0
- package/dist/jwt.js.map +1 -0
- package/dist/turbowarp-cloudflare-worker.js +1218 -0
- package/docs/architecture.ja.md +97 -0
- package/docs/architecture.md +7 -0
- package/docs/auth-blocks.ja.md +154 -0
- package/docs/index.html +31 -0
- package/package.json +80 -0
- package/schemas/extension-manifest.schema.json +49 -0
- package/src/block-definitions.json +115 -0
- package/src/cli.ts +146 -0
- package/src/config.ts +12 -0
- package/src/extension-manifest.ts +159 -0
- package/src/extension.ts +197 -0
- package/src/generator.ts +828 -0
- package/src/globals.d.ts +25 -0
- package/src/index.ts +8 -0
- package/src/ir.ts +246 -0
- package/src/jwt.ts +144 -0
package/src/globals.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
interface TurboWarpExtension {
|
|
2
|
+
getInfo(): Record<string, unknown>;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
interface ScratchTranslate {
|
|
6
|
+
(text: string): string;
|
|
7
|
+
(message: {default: string; description?: string}, placeholders?: Record<string, string | number>): string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface ScratchApi {
|
|
11
|
+
extensions: {
|
|
12
|
+
unsandboxed: boolean;
|
|
13
|
+
register(extension: TurboWarpExtension): void;
|
|
14
|
+
};
|
|
15
|
+
BlockType: Record<'COMMAND' | 'REPORTER' | 'BOOLEAN' | 'HAT', string>;
|
|
16
|
+
ArgumentType: Record<'STRING' | 'NUMBER' | 'BOOLEAN', string>;
|
|
17
|
+
Cast: {
|
|
18
|
+
toString(value: unknown): string;
|
|
19
|
+
toNumber(value: unknown): number;
|
|
20
|
+
toBoolean(value: unknown): boolean;
|
|
21
|
+
};
|
|
22
|
+
translate: ScratchTranslate;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
declare const Scratch: ScratchApi;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import {extensionConfig} from './config.js';
|
|
2
|
+
import {CloudflareExtension} from './extension.js';
|
|
3
|
+
|
|
4
|
+
if (extensionConfig.unsandboxed && !Scratch.extensions.unsandboxed) {
|
|
5
|
+
throw new Error(`${extensionConfig.name} must run unsandboxed.`);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
Scratch.extensions.register(new CloudflareExtension());
|
package/src/ir.ts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
2
|
+
|
|
3
|
+
export type AuthProvider = 'google' | 'microsoft' | 'github';
|
|
4
|
+
|
|
5
|
+
export type StorageKind = 'd1' | 'kv' | 'r2' | 'durable-object';
|
|
6
|
+
|
|
7
|
+
export type AuthSessionMode = 'signed-cookie' | 'bearer-jwt' | 'cloudflare-access';
|
|
8
|
+
|
|
9
|
+
export type UnauthenticatedAction = 'redirect-login' | 'json-401' | 'forbid-403';
|
|
10
|
+
|
|
11
|
+
export type ProtectedRouteAuth = 'session' | 'bearer-jwt';
|
|
12
|
+
|
|
13
|
+
export interface GeneratedFile {
|
|
14
|
+
path: string;
|
|
15
|
+
contents: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface AuthBindingIr {
|
|
19
|
+
mode: 'external-oauth-oidc';
|
|
20
|
+
providers: AuthProvider[];
|
|
21
|
+
session?: AuthSessionMode;
|
|
22
|
+
loginPath?: string;
|
|
23
|
+
callbackPath?: string;
|
|
24
|
+
logoutPath?: string;
|
|
25
|
+
mePath?: string;
|
|
26
|
+
protectedRoutes?: ProtectedRouteIr[];
|
|
27
|
+
jwtIssuerEnv?: string;
|
|
28
|
+
jwtAudienceEnv?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface ProtectedRouteIr {
|
|
32
|
+
method: HttpMethod | '*';
|
|
33
|
+
path: string;
|
|
34
|
+
onUnauthenticated: UnauthenticatedAction;
|
|
35
|
+
auth?: ProtectedRouteAuth;
|
|
36
|
+
requiredRole?: string;
|
|
37
|
+
requiredScopes?: string[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface StorageBindingIr {
|
|
41
|
+
kind: StorageKind;
|
|
42
|
+
binding: string;
|
|
43
|
+
name: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface HttpHandlerIr {
|
|
47
|
+
method: HttpMethod;
|
|
48
|
+
path: string;
|
|
49
|
+
responseText: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface CloudflareWorkerIr {
|
|
53
|
+
name: string;
|
|
54
|
+
handlers: HttpHandlerIr[];
|
|
55
|
+
compatibilityDate?: string;
|
|
56
|
+
auth?: AuthBindingIr;
|
|
57
|
+
storage?: StorageBindingIr[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function normalizeHttpMethod(value: string): HttpMethod {
|
|
61
|
+
const normalized = value.trim().toUpperCase();
|
|
62
|
+
if (isHttpMethod(normalized)) return normalized;
|
|
63
|
+
throw new TypeError(`Unsupported HTTP method: ${value}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function normalizeRoutePath(value: string): string {
|
|
67
|
+
const trimmed = value.trim();
|
|
68
|
+
if (!trimmed.startsWith('/')) return `/${trimmed}`;
|
|
69
|
+
return trimmed || '/';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function parseWorkerIr(value: unknown): CloudflareWorkerIr {
|
|
73
|
+
if (!isRecord(value)) {
|
|
74
|
+
throw new TypeError('Cloudflare Worker IR must be an object.');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const name = requireString(value.name, 'IR name');
|
|
78
|
+
const handlers = Array.isArray(value.handlers)
|
|
79
|
+
? value.handlers.map((handler, index) => parseHandlerIr(handler, index))
|
|
80
|
+
: [];
|
|
81
|
+
|
|
82
|
+
const compatibilityDate =
|
|
83
|
+
value.compatibilityDate === undefined
|
|
84
|
+
? undefined
|
|
85
|
+
: requireString(value.compatibilityDate, 'IR compatibilityDate');
|
|
86
|
+
|
|
87
|
+
const auth = value.auth === undefined ? undefined : parseAuthBindingIr(value.auth);
|
|
88
|
+
const storage = value.storage === undefined ? undefined : parseStorageBindings(value.storage);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
name,
|
|
92
|
+
handlers,
|
|
93
|
+
...(compatibilityDate === undefined ? {} : {compatibilityDate}),
|
|
94
|
+
...(auth === undefined ? {} : {auth}),
|
|
95
|
+
...(storage === undefined ? {} : {storage})
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function parseHandlerIr(value: unknown, index: number): HttpHandlerIr {
|
|
100
|
+
if (!isRecord(value)) {
|
|
101
|
+
throw new TypeError(`Handler at index ${index} must be an object.`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
method: normalizeHttpMethod(requireString(value.method, `Handler ${index} method`)),
|
|
106
|
+
path: normalizeRoutePath(requireString(value.path, `Handler ${index} path`)),
|
|
107
|
+
responseText: requireString(value.responseText, `Handler ${index} responseText`)
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function parseAuthBindingIr(value: unknown): AuthBindingIr {
|
|
112
|
+
if (!isRecord(value)) {
|
|
113
|
+
throw new TypeError('IR auth must be an object.');
|
|
114
|
+
}
|
|
115
|
+
const mode = requireString(value.mode, 'IR auth mode');
|
|
116
|
+
if (mode !== 'external-oauth-oidc') {
|
|
117
|
+
throw new TypeError(`Unsupported auth mode: ${mode}`);
|
|
118
|
+
}
|
|
119
|
+
const providers = Array.isArray(value.providers)
|
|
120
|
+
? value.providers.map((provider) => requireAuthProvider(provider))
|
|
121
|
+
: [];
|
|
122
|
+
return {
|
|
123
|
+
mode,
|
|
124
|
+
providers,
|
|
125
|
+
...(value.session === undefined ? {} : {session: requireAuthSessionMode(value.session)}),
|
|
126
|
+
...(value.loginPath === undefined
|
|
127
|
+
? {}
|
|
128
|
+
: {loginPath: normalizeRoutePath(requireString(value.loginPath, 'IR auth loginPath'))}),
|
|
129
|
+
...(value.callbackPath === undefined
|
|
130
|
+
? {}
|
|
131
|
+
: {callbackPath: normalizeRoutePath(requireString(value.callbackPath, 'IR auth callbackPath'))}),
|
|
132
|
+
...(value.logoutPath === undefined
|
|
133
|
+
? {}
|
|
134
|
+
: {logoutPath: normalizeRoutePath(requireString(value.logoutPath, 'IR auth logoutPath'))}),
|
|
135
|
+
...(value.mePath === undefined
|
|
136
|
+
? {}
|
|
137
|
+
: {mePath: normalizeRoutePath(requireString(value.mePath, 'IR auth mePath'))}),
|
|
138
|
+
...(value.protectedRoutes === undefined
|
|
139
|
+
? {}
|
|
140
|
+
: {protectedRoutes: parseProtectedRoutes(value.protectedRoutes)}),
|
|
141
|
+
...(value.jwtIssuerEnv === undefined
|
|
142
|
+
? {}
|
|
143
|
+
: {jwtIssuerEnv: requireString(value.jwtIssuerEnv, 'IR auth jwtIssuerEnv')}),
|
|
144
|
+
...(value.jwtAudienceEnv === undefined
|
|
145
|
+
? {}
|
|
146
|
+
: {jwtAudienceEnv: requireString(value.jwtAudienceEnv, 'IR auth jwtAudienceEnv')})
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function parseProtectedRoutes(value: unknown): ProtectedRouteIr[] {
|
|
151
|
+
if (!Array.isArray(value)) {
|
|
152
|
+
throw new TypeError('IR auth protectedRoutes must be an array.');
|
|
153
|
+
}
|
|
154
|
+
return value.map((route, index) => {
|
|
155
|
+
if (!isRecord(route)) {
|
|
156
|
+
throw new TypeError(`Protected route at index ${index} must be an object.`);
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
method:
|
|
160
|
+
route.method === '*'
|
|
161
|
+
? '*'
|
|
162
|
+
: normalizeHttpMethod(requireString(route.method, `Protected route ${index} method`)),
|
|
163
|
+
path: normalizeRoutePath(requireString(route.path, `Protected route ${index} path`)),
|
|
164
|
+
onUnauthenticated: requireUnauthenticatedAction(
|
|
165
|
+
route.onUnauthenticated,
|
|
166
|
+
`Protected route ${index} onUnauthenticated`
|
|
167
|
+
),
|
|
168
|
+
...(route.auth === undefined
|
|
169
|
+
? {}
|
|
170
|
+
: {auth: requireProtectedRouteAuth(route.auth, `Protected route ${index} auth`)}),
|
|
171
|
+
...(route.requiredRole === undefined
|
|
172
|
+
? {}
|
|
173
|
+
: {requiredRole: requireString(route.requiredRole, `Protected route ${index} requiredRole`)}),
|
|
174
|
+
...(route.requiredScopes === undefined
|
|
175
|
+
? {}
|
|
176
|
+
: {requiredScopes: parseStringArray(route.requiredScopes, `Protected route ${index} requiredScopes`)})
|
|
177
|
+
};
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function parseStorageBindings(value: unknown): StorageBindingIr[] {
|
|
182
|
+
if (!Array.isArray(value)) {
|
|
183
|
+
throw new TypeError('IR storage must be an array.');
|
|
184
|
+
}
|
|
185
|
+
return value.map((binding, index) => {
|
|
186
|
+
if (!isRecord(binding)) {
|
|
187
|
+
throw new TypeError(`Storage binding at index ${index} must be an object.`);
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
kind: requireStorageKind(binding.kind),
|
|
191
|
+
binding: requireString(binding.binding, `Storage binding ${index} binding`),
|
|
192
|
+
name: requireString(binding.name, `Storage binding ${index} name`)
|
|
193
|
+
};
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function isHttpMethod(value: string): value is HttpMethod {
|
|
198
|
+
return ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'].includes(value);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function requireAuthProvider(value: unknown): AuthProvider {
|
|
202
|
+
if (value === 'google' || value === 'microsoft' || value === 'github') return value;
|
|
203
|
+
throw new TypeError(`Unsupported auth provider: ${String(value)}`);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function requireAuthSessionMode(value: unknown): AuthSessionMode {
|
|
207
|
+
if (value === 'signed-cookie' || value === 'bearer-jwt' || value === 'cloudflare-access') {
|
|
208
|
+
return value;
|
|
209
|
+
}
|
|
210
|
+
throw new TypeError(`Unsupported auth session mode: ${String(value)}`);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function requireUnauthenticatedAction(value: unknown, label: string): UnauthenticatedAction {
|
|
214
|
+
if (value === 'redirect-login' || value === 'json-401' || value === 'forbid-403') return value;
|
|
215
|
+
throw new TypeError(`${label} must be redirect-login, json-401, or forbid-403.`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function requireProtectedRouteAuth(value: unknown, label: string): ProtectedRouteAuth {
|
|
219
|
+
if (value === 'session' || value === 'bearer-jwt') return value;
|
|
220
|
+
throw new TypeError(`${label} must be session or bearer-jwt.`);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function parseStringArray(value: unknown, label: string): string[] {
|
|
224
|
+
if (!Array.isArray(value)) {
|
|
225
|
+
throw new TypeError(`${label} must be an array.`);
|
|
226
|
+
}
|
|
227
|
+
return value.map((item, index) => requireString(item, `${label}[${index}]`));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function requireStorageKind(value: unknown): StorageKind {
|
|
231
|
+
if (value === 'd1' || value === 'kv' || value === 'r2' || value === 'durable-object') {
|
|
232
|
+
return value;
|
|
233
|
+
}
|
|
234
|
+
throw new TypeError(`Unsupported storage kind: ${String(value)}`);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function requireString(value: unknown, label: string): string {
|
|
238
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
239
|
+
throw new TypeError(`${label} must be a non-empty string.`);
|
|
240
|
+
}
|
|
241
|
+
return value;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
245
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
246
|
+
}
|
package/src/jwt.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {mkdir, readFile, writeFile} from 'node:fs/promises';
|
|
2
|
+
import {join} from 'node:path';
|
|
3
|
+
import {webcrypto} from 'node:crypto';
|
|
4
|
+
|
|
5
|
+
export interface JwtKeygenOptions {
|
|
6
|
+
outDir: string;
|
|
7
|
+
kid?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface JwtIssueOptions {
|
|
11
|
+
keyPath: string;
|
|
12
|
+
subject: string;
|
|
13
|
+
role: string;
|
|
14
|
+
scopes: string[];
|
|
15
|
+
workerName?: string;
|
|
16
|
+
issuer?: string;
|
|
17
|
+
audience?: string;
|
|
18
|
+
ttlSeconds?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface JwtKeygenResult {
|
|
22
|
+
kid: string;
|
|
23
|
+
privateKeyPath: string;
|
|
24
|
+
publicJwkPath: string;
|
|
25
|
+
jwksPath: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type Es256PrivateJwk = JsonWebKey & {
|
|
29
|
+
kid: string;
|
|
30
|
+
alg: 'ES256';
|
|
31
|
+
crv: 'P-256';
|
|
32
|
+
d: string;
|
|
33
|
+
kty: 'EC';
|
|
34
|
+
x: string;
|
|
35
|
+
y: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
type JwtJwk = JsonWebKey & {
|
|
39
|
+
kid: string;
|
|
40
|
+
alg: 'ES256';
|
|
41
|
+
use: 'sig';
|
|
42
|
+
key_ops: string[];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const DEFAULT_TTL_SECONDS = 60 * 60;
|
|
46
|
+
// Keep the pre-rename value stable so existing default-audience tokens remain valid.
|
|
47
|
+
const LEGACY_DEFAULT_AUDIENCE = 'turbowarp-http-server-cloudflare';
|
|
48
|
+
|
|
49
|
+
export async function generateEs256JwtKey(options: JwtKeygenOptions): Promise<JwtKeygenResult> {
|
|
50
|
+
const kid = options.kid ?? randomId();
|
|
51
|
+
const pair = await webcrypto.subtle.generateKey(
|
|
52
|
+
{name: 'ECDSA', namedCurve: 'P-256'},
|
|
53
|
+
true,
|
|
54
|
+
['sign', 'verify']
|
|
55
|
+
);
|
|
56
|
+
const privateJwk = await webcrypto.subtle.exportKey('jwk', pair.privateKey);
|
|
57
|
+
const publicJwk = await webcrypto.subtle.exportKey('jwk', pair.publicKey);
|
|
58
|
+
const normalizedPrivate = withKeyMetadata(privateJwk, kid, true);
|
|
59
|
+
const normalizedPublic = withKeyMetadata(publicJwk, kid, false);
|
|
60
|
+
|
|
61
|
+
await mkdir(options.outDir, {recursive: true});
|
|
62
|
+
const privateKeyPath = join(options.outDir, 'private-key.jwk');
|
|
63
|
+
const publicJwkPath = join(options.outDir, 'public-jwk.json');
|
|
64
|
+
const jwksPath = join(options.outDir, 'jwks.json');
|
|
65
|
+
|
|
66
|
+
await writeJson(privateKeyPath, normalizedPrivate);
|
|
67
|
+
await writeJson(publicJwkPath, normalizedPublic);
|
|
68
|
+
await writeJson(jwksPath, {keys: [normalizedPublic]});
|
|
69
|
+
|
|
70
|
+
return {kid, privateKeyPath, publicJwkPath, jwksPath};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function issueEs256Jwt(options: JwtIssueOptions): Promise<string> {
|
|
74
|
+
const privateJwk = JSON.parse(await readFile(options.keyPath, 'utf8')) as Es256PrivateJwk;
|
|
75
|
+
const now = Math.floor(Date.now() / 1000);
|
|
76
|
+
const audience = options.audience ?? options.workerName ?? LEGACY_DEFAULT_AUDIENCE;
|
|
77
|
+
const issuer = options.issuer ?? `https://${audience}.workers.dev`;
|
|
78
|
+
const ttlSeconds = options.ttlSeconds ?? DEFAULT_TTL_SECONDS;
|
|
79
|
+
const header = {
|
|
80
|
+
alg: 'ES256',
|
|
81
|
+
typ: 'JWT',
|
|
82
|
+
kid: privateJwk.kid
|
|
83
|
+
};
|
|
84
|
+
const payload = {
|
|
85
|
+
iss: issuer,
|
|
86
|
+
aud: audience,
|
|
87
|
+
sub: options.subject,
|
|
88
|
+
role: options.role,
|
|
89
|
+
scope: options.scopes.join(' '),
|
|
90
|
+
iat: now,
|
|
91
|
+
exp: now + ttlSeconds
|
|
92
|
+
};
|
|
93
|
+
const signingInput = `${base64UrlEncode(JSON.stringify(header))}.${base64UrlEncode(JSON.stringify(payload))}`;
|
|
94
|
+
const key = await webcrypto.subtle.importKey(
|
|
95
|
+
'jwk',
|
|
96
|
+
privateJwk,
|
|
97
|
+
{name: 'ECDSA', namedCurve: 'P-256'},
|
|
98
|
+
false,
|
|
99
|
+
['sign']
|
|
100
|
+
);
|
|
101
|
+
const signature = await webcrypto.subtle.sign(
|
|
102
|
+
{name: 'ECDSA', hash: 'SHA-256'},
|
|
103
|
+
key,
|
|
104
|
+
new TextEncoder().encode(signingInput)
|
|
105
|
+
);
|
|
106
|
+
return `${signingInput}.${base64UrlEncode(new Uint8Array(signature))}`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function parseTtl(value: string | undefined): number {
|
|
110
|
+
if (!value) return DEFAULT_TTL_SECONDS;
|
|
111
|
+
const match = /^(\d+)([smhd])?$/.exec(value.trim());
|
|
112
|
+
if (!match) {
|
|
113
|
+
throw new TypeError('TTL must look like 900s, 15m, 1h, or 7d.');
|
|
114
|
+
}
|
|
115
|
+
const amount = Number(match[1]);
|
|
116
|
+
const unit = match[2] ?? 's';
|
|
117
|
+
const multiplier = unit === 's' ? 1 : unit === 'm' ? 60 : unit === 'h' ? 3600 : 86400;
|
|
118
|
+
return amount * multiplier;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function withKeyMetadata(key: JsonWebKey, kid: string, privateKey: boolean): JwtJwk {
|
|
122
|
+
return {
|
|
123
|
+
...key,
|
|
124
|
+
kid,
|
|
125
|
+
alg: 'ES256',
|
|
126
|
+
use: 'sig',
|
|
127
|
+
key_ops: privateKey ? ['sign'] : ['verify']
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function writeJson(path: string, value: unknown): Promise<void> {
|
|
132
|
+
await writeFile(path, `${JSON.stringify(value, null, 2)}\n`, {encoding: 'utf8', mode: 0o600});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function randomId(): string {
|
|
136
|
+
const bytes = new Uint8Array(16);
|
|
137
|
+
webcrypto.getRandomValues(bytes);
|
|
138
|
+
return base64UrlEncode(bytes);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function base64UrlEncode(value: string | Uint8Array): string {
|
|
142
|
+
const bytes = typeof value === 'string' ? new TextEncoder().encode(value) : value;
|
|
143
|
+
return Buffer.from(bytes).toString('base64url');
|
|
144
|
+
}
|