@samebase/alchemy-convex 0.46.0 → 0.48.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/README.md +3 -174
- package/lib/index.d.ts +52 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1691 -8
- package/lib/index.js.map +1 -1
- package/lib/sealedSecret.js +78 -1
- package/lib/sealedSecret.js.map +1 -0
- package/package.json +2 -9
- package/src/Providers.ts +2 -21
- package/lib/IdentityBroker-BqA_2V3_.js +0 -430
- package/lib/IdentityBroker-BqA_2V3_.js.map +0 -1
- package/lib/IdentityBroker.d.ts +0 -162
- package/lib/IdentityBroker.d.ts.map +0 -1
- package/lib/IdentityBroker.js +0 -1
- package/lib/Providers-zL1fUs0r.d.ts +0 -58
- package/lib/Providers-zL1fUs0r.d.ts.map +0 -1
- package/lib/State-Bp7iSx8T.js +0 -1699
- package/lib/State-Bp7iSx8T.js.map +0 -1
- package/lib/cli.d.ts +0 -1
- package/lib/cli.js +0 -218
- package/lib/cli.js.map +0 -1
- package/lib/sealedSecret-B7HlvIg2.js +0 -78
- package/lib/sealedSecret-B7HlvIg2.js.map +0 -1
- package/src/IdentityBroker.ts +0 -849
- package/src/StackComment.ts +0 -329
- package/src/cli.ts +0 -33
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sealedSecret.js","names":[],"sources":["../src/store/sealedSecret.ts"],"sourcesContent":["// AES-256-GCM sealing under a key ring, with Web Crypto only, so that it runs in Convex and in Node.\n//\n// A key ring is the JSON text `{\"current\":\"<keyId>\",\"keys\":{\"<keyId>\":\"<base64 of 32 bytes>\"}}`,\n// which a deployment keeps in one environment variable. `sealSecret` seals with the `current` key.\n// `openSecret` opens with the key that the sealed object names, so an old key stays in the ring\n// until a reseal moves its values to the current key.\n//\n// Seal only where randomness is real: in a Convex action, or outside Convex. In a Convex query or\n// mutation `crypto.getRandomValues` is a seeded deterministic generator, and a repeated IV under one\n// key breaks AES-GCM. Opening draws no randomness, so a query can open.\n\n/** A value sealed with AES-256-GCM under the key `keyId` of a key ring. */\nexport interface SealedSecret {\n readonly keyId: string;\n /** 12 random bytes, base64url. */\n readonly iv: string;\n /** The AES-GCM output with its 16-byte tag, base64url. */\n readonly ciphertext: string;\n}\n\nexport interface KeyRing {\n readonly current: string;\n readonly keys: ReadonlyMap<string, Uint8Array<ArrayBuffer>>;\n}\n\nconst toBase64 = (bytes: Uint8Array) =>\n btoa(Array.from(bytes, (byte) => String.fromCharCode(byte)).join(\"\"));\n\nconst toBase64Url = (bytes: Uint8Array) =>\n toBase64(bytes).replaceAll(\"+\", \"-\").replaceAll(\"/\", \"_\").replace(/=+$/, \"\");\n\n/** The bytes of base64 or base64url text. */\nconst fromBase64 = (text: string) => {\n const standard = text.replaceAll(\"-\", \"+\").replaceAll(\"_\", \"/\");\n const padded = standard.padEnd(standard.length + ((4 - (standard.length % 4)) % 4), \"=\");\n return Uint8Array.from(atob(padded), (character) => character.charCodeAt(0));\n};\n\n/** A key of 32 bytes in base64 is 43 characters and one `=`. */\nconst KEY_TEXT = /^[A-Za-z0-9+/]{43}=$/;\n\n/**\n * The key ring in the text of the variable `name`. It fails closed when the text is missing or\n * malformed, or when the current key id has no key. An error never quotes the text, which holds\n * the keys.\n */\nexport const parseKeyRing = (name: string, text: string | undefined): KeyRing => {\n let value: unknown;\n try {\n value = JSON.parse(text ?? \"\");\n } catch {\n throw new Error(`${name} is missing or is not JSON.`);\n }\n const malformed = new Error(\n `${name} must be {\"current\":\"<keyId>\",\"keys\":{\"<keyId>\":\"<base64 of 32 bytes>\"}}.`,\n );\n if (typeof value !== \"object\" || value === null) throw malformed;\n if (!(\"current\" in value) || !(\"keys\" in value)) throw malformed;\n const { current, keys: keyTexts } = value;\n if (typeof current !== \"string\" || typeof keyTexts !== \"object\" || keyTexts === null) {\n throw malformed;\n }\n const keys = new Map<string, Uint8Array<ArrayBuffer>>();\n for (const [keyId, key] of Object.entries(keyTexts)) {\n if (typeof key !== \"string\" || !KEY_TEXT.test(key)) throw malformed;\n keys.set(keyId, fromBase64(key));\n }\n if (!keys.has(current)) throw new Error(`${name} has no key for its current key id.`);\n return { current, keys };\n};\n\n/** A random key id of 12 hexadecimal characters, which is also safe in a variable name. */\nexport const newKeyId = (): string =>\n Array.from(crypto.getRandomValues(new Uint8Array(6)), (byte) =>\n byte.toString(16).padStart(2, \"0\"),\n ).join(\"\");\n\n/** A random key of 32 bytes in base64. It needs real randomness, like `sealSecret`. */\nexport const newKey = (): string => toBase64(crypto.getRandomValues(new Uint8Array(32)));\n\nconst importKey = (ring: KeyRing, keyId: string) => {\n const bytes = ring.keys.get(keyId);\n if (bytes === undefined) throw new Error(`The key ring has no key \"${keyId}\".`);\n return crypto.subtle.importKey(\"raw\", bytes, \"AES-GCM\", false, [\"encrypt\", \"decrypt\"]);\n};\n\n/** Seals `plaintext` with the current key. `aad` names the owner, so the value opens only there. */\nexport const sealSecret = async (\n ring: KeyRing,\n args: { readonly plaintext: string; readonly aad: string },\n): Promise<SealedSecret> => {\n const key = await importKey(ring, ring.current);\n const iv = crypto.getRandomValues(new Uint8Array(12));\n const ciphertext = await crypto.subtle.encrypt(\n { name: \"AES-GCM\", iv, additionalData: new TextEncoder().encode(args.aad) },\n key,\n new TextEncoder().encode(args.plaintext),\n );\n return {\n keyId: ring.current,\n iv: toBase64Url(iv),\n ciphertext: toBase64Url(new Uint8Array(ciphertext)),\n };\n};\n\n/** Opens a sealed value with the key it names. A different `aad` or a changed value fails. */\nexport const openSecret = async (\n ring: KeyRing,\n args: { readonly sealed: SealedSecret; readonly aad: string },\n): Promise<string> => {\n const { sealed } = args;\n const key = await importKey(ring, sealed.keyId);\n const plaintext = await crypto.subtle.decrypt(\n {\n name: \"AES-GCM\",\n iv: fromBase64(sealed.iv),\n additionalData: new TextEncoder().encode(args.aad),\n },\n key,\n fromBase64(sealed.ciphertext),\n );\n return new TextDecoder().decode(plaintext);\n};\n"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samebase/alchemy-convex",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.48.0",
|
|
4
4
|
"description": "The Convex plane for Alchemy v2: projects, deployments, environment variables, and the push of the functions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"alchemy",
|
|
@@ -19,9 +19,6 @@
|
|
|
19
19
|
"url": "git+https://github.com/samebase/samebase.git",
|
|
20
20
|
"directory": "packages/alchemy-convex"
|
|
21
21
|
},
|
|
22
|
-
"bin": {
|
|
23
|
-
"alchemy-convex": "./lib/cli.js"
|
|
24
|
-
},
|
|
25
22
|
"files": [
|
|
26
23
|
"lib",
|
|
27
24
|
"src",
|
|
@@ -36,10 +33,6 @@
|
|
|
36
33
|
"types": "./lib/index.d.ts",
|
|
37
34
|
"import": "./lib/index.js"
|
|
38
35
|
},
|
|
39
|
-
"./identity-broker": {
|
|
40
|
-
"types": "./lib/IdentityBroker.d.ts",
|
|
41
|
-
"import": "./lib/IdentityBroker.js"
|
|
42
|
-
},
|
|
43
36
|
"./sealed-secret": {
|
|
44
37
|
"types": "./lib/sealedSecret.d.ts",
|
|
45
38
|
"import": "./lib/sealedSecret.js"
|
|
@@ -50,7 +43,7 @@
|
|
|
50
43
|
},
|
|
51
44
|
"dependencies": {
|
|
52
45
|
"@convex-dev/platform": "0.1.11",
|
|
53
|
-
"@samebase/alchemy-kit": "^0.
|
|
46
|
+
"@samebase/alchemy-kit": "^0.8.0"
|
|
54
47
|
},
|
|
55
48
|
"devDependencies": {
|
|
56
49
|
"@alchemy.run/cloudflare": "2.0.0-beta.87",
|
package/src/Providers.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
// The provider collection users merge into a stack:
|
|
2
2
|
// providers: Layer.mergeAll(Cloudflare.providers(), Convex.providers())
|
|
3
3
|
import * as Provider from "alchemy/Provider";
|
|
4
|
-
import { Random, RandomProvider } from "alchemy/Random";
|
|
5
4
|
import * as Layer from "effect/Layer";
|
|
6
5
|
import { type Credentials, fromConfig } from "./Credentials.ts";
|
|
7
6
|
import { DeploymentProvider, DeploymentResource } from "./Deployment.ts";
|
|
8
|
-
import * as IdentityBroker from "./IdentityBroker.ts";
|
|
9
7
|
import { LocalDeployment, LocalDeploymentProvider } from "./LocalDeployment.ts";
|
|
10
8
|
import { ManagementApiLive } from "./ManagementApi.ts";
|
|
11
9
|
import { Project, ProjectProvider } from "./Project.ts";
|
|
@@ -18,26 +16,9 @@ export class Providers extends Provider.ProviderCollection<Providers>()("Convex"
|
|
|
18
16
|
* `CONVEX_DEPLOY_KEY`).
|
|
19
17
|
*/
|
|
20
18
|
export const providers = (credentials: Layer.Layer<Credentials> = fromConfig()) =>
|
|
21
|
-
Layer.effect(
|
|
22
|
-
Providers,
|
|
23
|
-
// Random's provider serves `IdentityBroker.registrationKey` in a stack with no other collection
|
|
24
|
-
// that holds one; with Cloudflare's too, the first found serves it, and both are the same.
|
|
25
|
-
Provider.collection([
|
|
26
|
-
Project,
|
|
27
|
-
DeploymentResource,
|
|
28
|
-
LocalDeployment,
|
|
29
|
-
IdentityBroker.Client,
|
|
30
|
-
Random,
|
|
31
|
-
]),
|
|
32
|
-
).pipe(
|
|
19
|
+
Layer.effect(Providers, Provider.collection([Project, DeploymentResource, LocalDeployment])).pipe(
|
|
33
20
|
Layer.provide(
|
|
34
|
-
Layer.mergeAll(
|
|
35
|
-
ProjectProvider(),
|
|
36
|
-
DeploymentProvider(),
|
|
37
|
-
LocalDeploymentProvider(),
|
|
38
|
-
IdentityBroker.ClientProvider(),
|
|
39
|
-
RandomProvider(),
|
|
40
|
-
),
|
|
21
|
+
Layer.mergeAll(ProjectProvider(), DeploymentProvider(), LocalDeploymentProvider()),
|
|
41
22
|
),
|
|
42
23
|
Layer.provide(ManagementApiLive()),
|
|
43
24
|
Layer.provide(credentials),
|
|
@@ -1,430 +0,0 @@
|
|
|
1
|
-
import { Stack } from "alchemy/Stack";
|
|
2
|
-
import * as Effect from "effect/Effect";
|
|
3
|
-
import * as Schema from "effect/Schema";
|
|
4
|
-
import * as Option from "effect/Option";
|
|
5
|
-
import * as Redacted from "effect/Redacted";
|
|
6
|
-
import { Resource } from "alchemy";
|
|
7
|
-
import { isResolved } from "alchemy/Diff";
|
|
8
|
-
import * as Provider from "alchemy/Provider";
|
|
9
|
-
import * as Schedule from "effect/Schedule";
|
|
10
|
-
import { Random } from "alchemy/Random";
|
|
11
|
-
import * as Output from "alchemy/Output";
|
|
12
|
-
import { State } from "alchemy/State";
|
|
13
|
-
//#region src/IdentityBroker.ts
|
|
14
|
-
/** A hung service must not hold a deploy: the time limit of one request. */
|
|
15
|
-
const REQUEST_TIMEOUT_MS = 3e4;
|
|
16
|
-
/** The client ID and the client secret of a deployment without a broker client. */
|
|
17
|
-
const DISABLED = "broker-disabled";
|
|
18
|
-
/** The variables of a deployment without a broker client. */
|
|
19
|
-
const OFF_VALUES = {
|
|
20
|
-
issuer: "http://127.0.0.1:1",
|
|
21
|
-
clientId: DISABLED,
|
|
22
|
-
clientSecret: DISABLED
|
|
23
|
-
};
|
|
24
|
-
/** The name of a connector on the broker. */
|
|
25
|
-
const CONNECTOR_NAME = /^[a-z0-9-]{1,64}$/;
|
|
26
|
-
/** The return addresses of one client, as the broker caps them. */
|
|
27
|
-
const MAX_REDIRECT_URIS = 8;
|
|
28
|
-
/** The app's name, as the broker caps it. A deeper stack path needs an explicit `app`. */
|
|
29
|
-
const MAX_APP_LENGTH = 64;
|
|
30
|
-
/**
|
|
31
|
-
* The app's default name: the stack's path below `owner/repo`, or `app` for a stack named after
|
|
32
|
-
* the repository. Undefined for a stack whose name is not `owner/repo` or `owner/repo/<path>`.
|
|
33
|
-
*/
|
|
34
|
-
const defaultApp = (stackName) => {
|
|
35
|
-
const segments = stackName.split("/");
|
|
36
|
-
if (segments.length < 2 || segments.some((segment) => segment === "")) return;
|
|
37
|
-
return segments.slice(2).join("/") || "app";
|
|
38
|
-
};
|
|
39
|
-
const DeploymentType = Schema.Literals([
|
|
40
|
-
"production",
|
|
41
|
-
"preview",
|
|
42
|
-
"development"
|
|
43
|
-
]);
|
|
44
|
-
const Attributes = Schema.Struct({
|
|
45
|
-
/** `off`: no client, and the values below are the off values. */
|
|
46
|
-
status: Schema.Literals([
|
|
47
|
-
"on",
|
|
48
|
-
"off",
|
|
49
|
-
"suspended"
|
|
50
|
-
]),
|
|
51
|
-
/** The issuer of the broker, which the deployment names to sign in. */
|
|
52
|
-
issuer: Schema.String,
|
|
53
|
-
clientId: Schema.String,
|
|
54
|
-
/** Returned only at registration. */
|
|
55
|
-
clientSecret: Schema.Redacted(Schema.String),
|
|
56
|
-
/** Returned only at registration. It reads and deletes this client on the broker, and nothing else. */
|
|
57
|
-
registrationAccessToken: Schema.Redacted(Schema.String),
|
|
58
|
-
registrationClientUri: Schema.String,
|
|
59
|
-
deploymentType: DeploymentType,
|
|
60
|
-
/** The app's name on the broker, as sent. Empty for the off values. */
|
|
61
|
-
app: Schema.String,
|
|
62
|
-
/** The return addresses the client serves, sorted, each once. Empty for the off values. */
|
|
63
|
-
redirectUris: Schema.Array(Schema.String),
|
|
64
|
-
/** The connectors the client is enrolled in, sorted, each once. Empty for the off values. */
|
|
65
|
-
connectors: Schema.Array(Schema.String),
|
|
66
|
-
/**
|
|
67
|
-
* Returned only at a production registration (RFC 7591 section 1.2). It registers development
|
|
68
|
-
* clients of this app. Empty for a preview, for a development client, and for the off values.
|
|
69
|
-
*/
|
|
70
|
-
initialAccessToken: Schema.Redacted(Schema.String),
|
|
71
|
-
/** The numeric GitHub id of the repository, with the initial access token. Empty without it. */
|
|
72
|
-
repository: Schema.String
|
|
73
|
-
});
|
|
74
|
-
/**
|
|
75
|
-
* The attributes of a saved row. A row that does not have this version's shape, or any other
|
|
76
|
-
* failure of the decoder, stops the run with `reason`. The text of a schema issue can quote a value
|
|
77
|
-
* of the row, so it is never kept.
|
|
78
|
-
*/
|
|
79
|
-
const savedAttributes = (value, reason) => Schema.decodeUnknownEffect(Attributes)(value).pipe(Effect.catchCause(() => Effect.die(new IdentityBrokerClientFailed({ reason }))));
|
|
80
|
-
/** The attributes of no client. */
|
|
81
|
-
const offAttributes = (deploymentType) => ({
|
|
82
|
-
status: "off",
|
|
83
|
-
issuer: OFF_VALUES.issuer,
|
|
84
|
-
clientId: OFF_VALUES.clientId,
|
|
85
|
-
clientSecret: Redacted.make(OFF_VALUES.clientSecret),
|
|
86
|
-
registrationAccessToken: Redacted.make(""),
|
|
87
|
-
registrationClientUri: "",
|
|
88
|
-
deploymentType,
|
|
89
|
-
app: "",
|
|
90
|
-
redirectUris: [],
|
|
91
|
-
connectors: [],
|
|
92
|
-
initialAccessToken: Redacted.make(""),
|
|
93
|
-
repository: ""
|
|
94
|
-
});
|
|
95
|
-
/** The set of `values`: sorted, each once. */
|
|
96
|
-
const sortedSet = (values) => [...new Set(values)].sort();
|
|
97
|
-
const sameSet = (a, b) => a.length === b.length && a.every((name, index) => name === b[index]);
|
|
98
|
-
/** The app and its return addresses, as every log line names a client: no secret, no answer. */
|
|
99
|
-
const where = (client) => `${client.app} on ${client.redirectUris.join(", ")}`;
|
|
100
|
-
/** One request gave no usable answer. The reason holds no secret. */
|
|
101
|
-
var IdentityBrokerClientFailed = class extends Schema.TaggedError()("IdentityBrokerClientFailed", { reason: Schema.String }) {
|
|
102
|
-
get message() {
|
|
103
|
-
return `IdentityBroker.Client: ${this.reason}.`;
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
const OidcToken = Schema.Struct({ value: Schema.NonEmptyString });
|
|
107
|
-
/**
|
|
108
|
-
* The 201 answer of `POST /register` (RFC 7591 section 3.2.1), in the members that this file reads.
|
|
109
|
-
* The decoder ignores the other members, such as `software_id`, whose full form the broker writes.
|
|
110
|
-
*/
|
|
111
|
-
const NewClient = Schema.Struct({
|
|
112
|
-
issuer: Schema.NonEmptyString,
|
|
113
|
-
client_id: Schema.NonEmptyString,
|
|
114
|
-
client_secret: Schema.NonEmptyString,
|
|
115
|
-
registration_access_token: Schema.NonEmptyString,
|
|
116
|
-
registration_client_uri: Schema.NonEmptyString,
|
|
117
|
-
/** The sets the broker recorded, which must be the sets the request sent. */
|
|
118
|
-
redirect_uris: Schema.Array(Schema.String),
|
|
119
|
-
connectors: Schema.Array(Schema.String),
|
|
120
|
-
/** Only in the answer to a production registration with the GitHub token. */
|
|
121
|
-
initial_access_token: Schema.optionalKey(Schema.NonEmptyString),
|
|
122
|
-
repository: Schema.optionalKey(Schema.String.check(Schema.isPattern(/^\d+$/)))
|
|
123
|
-
});
|
|
124
|
-
/** The other answers of `POST /register`, by status. A 401 can have no body (RFC 6750). */
|
|
125
|
-
const REFUSALS = {
|
|
126
|
-
400: "the identity broker refused the client metadata, such as the app name",
|
|
127
|
-
401: "the identity broker refused the GitHub OIDC token",
|
|
128
|
-
403: "the identity broker does not allow this repository",
|
|
129
|
-
404: "the identity broker has no /register: it runs an identity-broker before 0.33.0",
|
|
130
|
-
429: "the identity broker limits new clients",
|
|
131
|
-
502: "GitHub's keys are not available to the identity broker",
|
|
132
|
-
503: "the identity broker could not apply its policy"
|
|
133
|
-
};
|
|
134
|
-
/** The text of an `invalid_redirect_uri` 400 without a known reason. */
|
|
135
|
-
const BAD_REDIRECT_URIS = "the identity broker refused the return addresses: one to eight, each https on a DNS name, or http on a loopback host for a development client";
|
|
136
|
-
/**
|
|
137
|
-
* The reasons of a 400 or a 403 that a log line shows. Any other text of the answer is never
|
|
138
|
-
* logged.
|
|
139
|
-
*/
|
|
140
|
-
const REASONS = /* @__PURE__ */ new Map([
|
|
141
|
-
["paused", "this app is paused on the identity broker"],
|
|
142
|
-
["run_not_allowed", "the identity broker gives clients only to runs of main and of pull requests"],
|
|
143
|
-
["blocked", "Samebase blocked this repository or its owner"],
|
|
144
|
-
["suspended", "the identity broker suspended this repository"],
|
|
145
|
-
["too_many_apps", "the repository has as many apps on the identity broker as it may"],
|
|
146
|
-
["too_many_clients", "the app has as many clients on the identity broker as it may"],
|
|
147
|
-
["app_taken", "the app's name belongs to another app on the identity broker"],
|
|
148
|
-
["host_unreachable", "the identity broker could not reach the host of the return address"],
|
|
149
|
-
["verification_file_missing", "the host of the return address does not serve /samebase-verification.json"],
|
|
150
|
-
["verification_file_invalid", "/samebase-verification.json on the host holds no repository id"],
|
|
151
|
-
["repository_mismatch", "/samebase-verification.json on the host names another repository"],
|
|
152
|
-
["app_mismatch", "the initial access token belongs to another app on the identity broker"],
|
|
153
|
-
["stale_proof", "the identity broker took a newer GitHub OIDC token of this repository"]
|
|
154
|
-
]);
|
|
155
|
-
/** The windows of a 429, by the name that the answer gives. */
|
|
156
|
-
const RATE_LIMITED = /* @__PURE__ */ new Map([
|
|
157
|
-
["repository", "the identity broker gives one repository at most 30 new clients an hour"],
|
|
158
|
-
["owner", "the identity broker gives one owner at most 100 new clients an hour"],
|
|
159
|
-
["new_owners", "the identity broker takes at most 50 new owners an hour"]
|
|
160
|
-
]);
|
|
161
|
-
const Refusal = Schema.Struct({ reason: Schema.String });
|
|
162
|
-
const InvalidRedirectUri = Schema.Struct({ error: Schema.Literal("invalid_redirect_uri") });
|
|
163
|
-
const RateLimited = Schema.Struct({ window: Schema.String });
|
|
164
|
-
/** A 400 for a connector the broker does not offer or allow, with the names it allows. */
|
|
165
|
-
const InvalidConnectors = Schema.Struct({
|
|
166
|
-
reason: Schema.Literal("invalid_connectors"),
|
|
167
|
-
allowed: Schema.Array(Schema.String)
|
|
168
|
-
});
|
|
169
|
-
/**
|
|
170
|
-
* The text of a refusal: a known reason of a 400 or a 403, or window of a 429, the allowed
|
|
171
|
-
* connectors of an `invalid_connectors` 400 (only names of the connector grammar, so no other text
|
|
172
|
-
* of the answer is printed), the text of an `invalid_redirect_uri` 400, else the text of its
|
|
173
|
-
* status. `body` is undefined for an answer without JSON, such as a bare 401.
|
|
174
|
-
*/
|
|
175
|
-
const refusalOf = (status, body) => {
|
|
176
|
-
const decoded = (schema) => Option.getOrUndefined(Schema.decodeUnknownOption(schema)(body));
|
|
177
|
-
const ofStatus = () => REFUSALS[status] ?? "the identity broker refused the request";
|
|
178
|
-
switch (status) {
|
|
179
|
-
case 400: {
|
|
180
|
-
const allowed = decoded(InvalidConnectors)?.allowed.filter((name) => CONNECTOR_NAME.test(name));
|
|
181
|
-
return allowed === void 0 ? REASONS.get(decoded(Refusal)?.reason ?? "") ?? (decoded(InvalidRedirectUri) === void 0 ? ofStatus() : BAD_REDIRECT_URIS) : `the identity broker does not offer or allow one of the connectors; it allows ${allowed.length === 0 ? "none for this app" : allowed.join(", ")}`;
|
|
182
|
-
}
|
|
183
|
-
case 403: return REASONS.get(decoded(Refusal)?.reason ?? "") ?? ofStatus();
|
|
184
|
-
case 429: return RATE_LIMITED.get(decoded(RateLimited)?.window ?? "") ?? ofStatus();
|
|
185
|
-
default: return ofStatus();
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
/**
|
|
189
|
-
* One request to `who` at `url`, with `token` as its Bearer. A token goes only to an https URL:
|
|
190
|
-
* to any other URL the request is not made.
|
|
191
|
-
*/
|
|
192
|
-
const send = (who, url, token, init) => token !== void 0 && !(URL.canParse(url) && new URL(url).protocol === "https:") ? Effect.fail(new IdentityBrokerClientFailed({ reason: `${who} is not at an https URL, so no credential was sent to it` })) : Effect.tryPromise({
|
|
193
|
-
try: async () => {
|
|
194
|
-
const response = await fetch(url, {
|
|
195
|
-
...init,
|
|
196
|
-
headers: {
|
|
197
|
-
...token === void 0 ? {} : { Authorization: `Bearer ${token}` },
|
|
198
|
-
"Content-Type": "application/json"
|
|
199
|
-
},
|
|
200
|
-
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
|
201
|
-
});
|
|
202
|
-
const body = await response.json().catch(() => void 0);
|
|
203
|
-
return {
|
|
204
|
-
status: response.status,
|
|
205
|
-
body
|
|
206
|
-
};
|
|
207
|
-
},
|
|
208
|
-
catch: () => new IdentityBrokerClientFailed({ reason: `${who} gave no answer` })
|
|
209
|
-
});
|
|
210
|
-
const decode = (schema, who) => (value) => Schema.decodeUnknownEffect(schema)(value).pipe(Effect.mapError(() => new IdentityBrokerClientFailed({ reason: `${who} gave a bad answer` })));
|
|
211
|
-
/**
|
|
212
|
-
* The OIDC token of this run for `audience`, read right before the call, because it lives for
|
|
213
|
-
* minutes only.
|
|
214
|
-
*/
|
|
215
|
-
const readOidcToken = (requestUrl, requestToken, audience) => Effect.gen(function* () {
|
|
216
|
-
const { body, status } = yield* send("GitHub", `${requestUrl}&audience=${encodeURIComponent(audience)}`, requestToken, { method: "GET" });
|
|
217
|
-
if (status !== 200) return yield* new IdentityBrokerClientFailed({ reason: `GitHub gave no OIDC token (HTTP ${status})` });
|
|
218
|
-
return (yield* decode(OidcToken, "GitHub")(body)).value;
|
|
219
|
-
});
|
|
220
|
-
/**
|
|
221
|
-
* `POST /register`, the client registration request of RFC 7591, with `bearer` as its initial
|
|
222
|
-
* access token: the GitHub OIDC token, the app's own token, or a key of the broker. `software_id` is
|
|
223
|
-
* the app's name, `client_name` the stage, and `connectors` and `deployment_type` are the broker's
|
|
224
|
-
* own metadata fields. Convex Auth posts the client's secret in the body at the token endpoint, so
|
|
225
|
-
* the client registers that method.
|
|
226
|
-
*/
|
|
227
|
-
const requestNewClient = (props, enrollment, bearer) => Effect.gen(function* () {
|
|
228
|
-
const { broker } = props;
|
|
229
|
-
const { app, connectors, redirectUris } = enrollment;
|
|
230
|
-
const { body, status } = yield* send("The identity broker", `${broker.issuer}/register`, bearer, {
|
|
231
|
-
method: "POST",
|
|
232
|
-
body: JSON.stringify({
|
|
233
|
-
redirect_uris: redirectUris,
|
|
234
|
-
software_id: app,
|
|
235
|
-
software_version: props.softwareVersion ?? "unknown",
|
|
236
|
-
client_name: enrollment.clientName,
|
|
237
|
-
deployment_type: props.deploymentType,
|
|
238
|
-
connectors,
|
|
239
|
-
token_endpoint_auth_method: "client_secret_post"
|
|
240
|
-
})
|
|
241
|
-
});
|
|
242
|
-
if (status !== 201) return yield* new IdentityBrokerClientFailed({ reason: `${status !== 401 ? refusalOf(status, body) : "key" in props ? "the identity broker refused the registration key" : props.deploymentType === "development" ? "the identity broker refused the initial access token" : refusalOf(status, body)} (HTTP ${status})` });
|
|
243
|
-
const client = yield* decode(NewClient, "The identity broker")(body);
|
|
244
|
-
if (client.issuer !== broker.issuer || !sameSet(sortedSet(client.redirect_uris), redirectUris) || !sameSet(client.connectors, connectors)) return yield* new IdentityBrokerClientFailed({ reason: "The identity broker gave a bad answer" });
|
|
245
|
-
yield* Effect.logInfo(`IdentityBroker.Client: ${where(enrollment)} has a new client of the identity broker ${broker.issuer}.`);
|
|
246
|
-
const production = props.deploymentType === "production";
|
|
247
|
-
return {
|
|
248
|
-
status: "on",
|
|
249
|
-
issuer: broker.issuer,
|
|
250
|
-
clientId: client.client_id,
|
|
251
|
-
clientSecret: Redacted.make(client.client_secret),
|
|
252
|
-
registrationAccessToken: Redacted.make(client.registration_access_token),
|
|
253
|
-
registrationClientUri: client.registration_client_uri,
|
|
254
|
-
deploymentType: props.deploymentType,
|
|
255
|
-
app,
|
|
256
|
-
redirectUris,
|
|
257
|
-
connectors,
|
|
258
|
-
initialAccessToken: Redacted.make(production ? client.initial_access_token ?? "" : ""),
|
|
259
|
-
repository: production ? client.repository ?? "" : ""
|
|
260
|
-
};
|
|
261
|
-
});
|
|
262
|
-
const Discovery = Schema.Struct({ issuer: Schema.String });
|
|
263
|
-
/**
|
|
264
|
-
* The time a broker has to answer discovery before a registration with a key: the stage that
|
|
265
|
-
* deploys the broker registers its own client in the same run, and a new workers.dev route answers
|
|
266
|
-
* after about a minute. Delays of 1, 2, 4, 8 and then 15 seconds, about three minutes in all.
|
|
267
|
-
*/
|
|
268
|
-
const DISCOVERY_RETRY = {
|
|
269
|
-
times: 15,
|
|
270
|
-
schedule: Schedule.min([Schedule.exponential("1 second"), Schedule.spaced("15 seconds")])
|
|
271
|
-
};
|
|
272
|
-
/**
|
|
273
|
-
* Succeeds when the broker's discovery document names the broker's issuer, after bounded retries;
|
|
274
|
-
* fails when it never does.
|
|
275
|
-
*/
|
|
276
|
-
const brokerAnswers = (broker) => send("The identity broker", `${broker.issuer}/.well-known/openid-configuration`, void 0, { method: "GET" }).pipe(Effect.flatMap(({ body, status }) => status === 200 && Option.getOrUndefined(Schema.decodeUnknownOption(Discovery)(body))?.issuer === broker.issuer ? Effect.void : Effect.fail(new IdentityBrokerClientFailed({ reason: "the identity broker does not answer discovery" }))), Effect.retry(DISCOVERY_RETRY));
|
|
277
|
-
/** A new client, or the off values. */
|
|
278
|
-
const newClient = (props, enrollment) => {
|
|
279
|
-
const label = `IdentityBroker.Client: ${where(enrollment)} deploys without the broker`;
|
|
280
|
-
const off = offAttributes(props.deploymentType);
|
|
281
|
-
const request = (bearer) => bearer.pipe(Effect.flatMap((token) => requestNewClient(props, enrollment, token)), Effect.catchTag("IdentityBrokerClientFailed", (error) => Effect.logWarning(`${label}: ${error.reason}. The next deploy asks again.`).pipe(Effect.as(off))));
|
|
282
|
-
if ("key" in props) return props.key === void 0 ? Effect.logInfo(`${label}: it has no registration key yet. Deploy the stage that makes the key first.`).pipe(Effect.as(off)) : request(Effect.as(brokerAnswers(props.broker), Redacted.value(props.key)));
|
|
283
|
-
if (props.deploymentType === "development") {
|
|
284
|
-
const token = Redacted.value(props.initialAccessToken);
|
|
285
|
-
return token === "" ? Effect.logInfo("IdentityBroker.Client: deploy to main once for broker sign-in in development.").pipe(Effect.as(off)) : request(Effect.succeed(token));
|
|
286
|
-
}
|
|
287
|
-
const requestUrl = process.env["ACTIONS_ID_TOKEN_REQUEST_URL"];
|
|
288
|
-
const requestToken = process.env["ACTIONS_ID_TOKEN_REQUEST_TOKEN"];
|
|
289
|
-
if (!requestUrl || !requestToken) return (process.env["GITHUB_ACTIONS"] === "true" ? Effect.logWarning(`${label}: this workflow needs the permission id-token: write.`) : Effect.logInfo(`${label}: this run has no GitHub OIDC token.`)).pipe(Effect.as(off));
|
|
290
|
-
return request(readOidcToken(requestUrl, requestToken, props.broker.issuer));
|
|
291
|
-
};
|
|
292
|
-
/**
|
|
293
|
-
* The client after a read on the broker, or undefined when the broker says that the client is gone.
|
|
294
|
-
* A read with no usable answer keeps the saved client, except for a required one: a required client
|
|
295
|
-
* passes a run only when the broker confirms it in that run, so a suspension that an earlier run
|
|
296
|
-
* saw, and did not save because that run failed, cannot pass a run that the broker does not answer.
|
|
297
|
-
*/
|
|
298
|
-
const renew = (saved, required) => Effect.gen(function* () {
|
|
299
|
-
const { status } = yield* send("The identity broker", saved.registrationClientUri, Redacted.value(saved.registrationAccessToken), { method: "GET" });
|
|
300
|
-
switch (status) {
|
|
301
|
-
case 200: return {
|
|
302
|
-
...saved,
|
|
303
|
-
status: "on"
|
|
304
|
-
};
|
|
305
|
-
case 401: return;
|
|
306
|
-
case 403:
|
|
307
|
-
yield* Effect.logWarning(`IdentityBroker.Client: the broker suspended the client of ${where(saved)}. It keeps its values, and nobody can sign in with it.`);
|
|
308
|
-
return {
|
|
309
|
-
...saved,
|
|
310
|
-
status: "suspended"
|
|
311
|
-
};
|
|
312
|
-
default: return yield* new IdentityBrokerClientFailed({ reason: `The identity broker answered HTTP ${status}` });
|
|
313
|
-
}
|
|
314
|
-
}).pipe(Effect.catchTag("IdentityBrokerClientFailed", (error) => required ? Effect.fail(new IdentityBrokerClientFailed({ reason: `${where(saved)} is required, and the broker did not confirm its client: ${error.reason}` })) : Effect.logWarning(`IdentityBroker.Client: ${where(saved)} keeps the saved client: ${error.reason}.`).pipe(Effect.as(saved))));
|
|
315
|
-
/**
|
|
316
|
-
* The types of this resource before 0.46.0. A deploy updates a row of such a type in place and
|
|
317
|
-
* gives it the new type, with the same client; a destroy or an orphan delete finds this provider
|
|
318
|
-
* for it by these aliases. Remove them, here and in `SavedRow`, once no saved row has an old type.
|
|
319
|
-
*/
|
|
320
|
-
const OLD_TYPE = "IdentityHub.Client";
|
|
321
|
-
const OLDER_TYPE = "IdentityHub.SharedClient";
|
|
322
|
-
/** A saved row of this resource, whose `attr` `savedAttributes` reads. */
|
|
323
|
-
const SavedRow = Schema.Struct({
|
|
324
|
-
resourceType: Schema.Literals([
|
|
325
|
-
"IdentityBroker.Client",
|
|
326
|
-
OLD_TYPE,
|
|
327
|
-
OLDER_TYPE
|
|
328
|
-
]),
|
|
329
|
-
attr: Schema.Unknown
|
|
330
|
-
});
|
|
331
|
-
/**
|
|
332
|
-
* The attributes of the client `id` in another stage of this stack, such as
|
|
333
|
-
* `read("BrokerClient", { stage: "prod" })` in a development stage, as Outputs: the
|
|
334
|
-
* `initialAccessToken` and the `repository` of the production client. Without that row, the off
|
|
335
|
-
* attributes, with `deploymentType: "production"`.
|
|
336
|
-
*
|
|
337
|
-
* `IdentityBroker.Client.ref` is not enough: Alchemy fails the plan with `InvalidReferenceError` when
|
|
338
|
-
* the row is missing, and a development stage before the first deploy to main has none. This read
|
|
339
|
-
* runs when the plan resolves it, with the State service of the running stack and its `get`, so it
|
|
340
|
-
* never creates or upgrades a store. The store revives each secret as `Redacted`, as it does for
|
|
341
|
-
* every read.
|
|
342
|
-
*/
|
|
343
|
-
const read = (id, options) => Output.fromEffect(Effect.gen(function* () {
|
|
344
|
-
const stack = yield* Stack;
|
|
345
|
-
const row = yield* (yield* yield* State).get({
|
|
346
|
-
stack: stack.name,
|
|
347
|
-
stage: options.stage,
|
|
348
|
-
fqn: id
|
|
349
|
-
}).pipe(Effect.orDie);
|
|
350
|
-
if (row === void 0) return offAttributes("production");
|
|
351
|
-
const { attr } = yield* Schema.decodeUnknownEffect(SavedRow)(row).pipe(Effect.catchCause(() => Effect.die(new IdentityBrokerClientFailed({ reason: `the row ${id} of stage ${options.stage} is not an IdentityBroker.Client` }))));
|
|
352
|
-
return yield* savedAttributes(attr, `the row ${id} of stage ${options.stage} holds attributes that this version cannot read`);
|
|
353
|
-
}));
|
|
354
|
-
/** A saved row of an `Alchemy.Random`, whose `text` the store revives as `Redacted`. */
|
|
355
|
-
const SavedKey = Schema.Struct({
|
|
356
|
-
resourceType: Schema.Literal("Alchemy.Random"),
|
|
357
|
-
attr: Schema.Struct({ text: Schema.Redacted(Schema.String) })
|
|
358
|
-
});
|
|
359
|
-
/**
|
|
360
|
-
* A key of a broker on the stack layer, as a redacted Output, for the broker's secret binding and
|
|
361
|
-
* for `IdentityBroker.Client`'s `key`. `id` is the key's resource id, the author's name for it, so a
|
|
362
|
-
* stack can make several. In `options.stage`, the stage that deploys the broker, the key is an
|
|
363
|
-
* `Alchemy.Random` at the stack's root, made once and kept in state; the stack's providers need
|
|
364
|
-
* Random's provider (`Convex.providers()` and `Cloudflare.providers()` hold one). Every other stage
|
|
365
|
-
* reads that row from the state store, as `IdentityBroker.Client.read` does, so it never creates the
|
|
366
|
-
* key; before the broker's stage ran, there is no row and no key (`undefined`).
|
|
367
|
-
*/
|
|
368
|
-
const registrationKey = (id, options) => Effect.gen(function* () {
|
|
369
|
-
const { stage } = yield* Stack;
|
|
370
|
-
if (stage === options.stage) return (yield* Random(id)).text;
|
|
371
|
-
return Output.fromEffect(Effect.gen(function* () {
|
|
372
|
-
const stack = yield* Stack;
|
|
373
|
-
const row = yield* (yield* yield* State).get({
|
|
374
|
-
stack: stack.name,
|
|
375
|
-
stage: options.stage,
|
|
376
|
-
fqn: id
|
|
377
|
-
}).pipe(Effect.orDie);
|
|
378
|
-
if (row === void 0) return void 0;
|
|
379
|
-
return (yield* Schema.decodeUnknownEffect(SavedKey)(row).pipe(Effect.catchCause(() => Effect.die(new IdentityBrokerClientFailed({ reason: `the row ${id} of stage ${options.stage} is not an Alchemy.Random` }))))).attr.text;
|
|
380
|
-
}));
|
|
381
|
-
});
|
|
382
|
-
/**
|
|
383
|
-
* The three values that the app's deployment gets, such as `IDENTITY_BROKER_ISSUER`,
|
|
384
|
-
* `IDENTITY_BROKER_CLIENT_ID`, and `IDENTITY_BROKER_CLIENT_SECRET`, as Outputs: the client's values
|
|
385
|
-
* when `status` is `on`, else the off values, so that a suspended client gives guest sign-in and
|
|
386
|
-
* keeps its values in state.
|
|
387
|
-
*/
|
|
388
|
-
const variables = (client) => Output.map(Output.of(client), (attributes) => attributes.status === "on" ? {
|
|
389
|
-
issuer: attributes.issuer,
|
|
390
|
-
clientId: attributes.clientId,
|
|
391
|
-
clientSecret: attributes.clientSecret
|
|
392
|
-
} : {
|
|
393
|
-
issuer: OFF_VALUES.issuer,
|
|
394
|
-
clientId: OFF_VALUES.clientId,
|
|
395
|
-
clientSecret: Redacted.make(OFF_VALUES.clientSecret)
|
|
396
|
-
});
|
|
397
|
-
const Client = Resource("IdentityBroker.Client", { aliases: [OLD_TYPE, OLDER_TYPE] })({
|
|
398
|
-
read,
|
|
399
|
-
variables
|
|
400
|
-
});
|
|
401
|
-
const ClientProvider = () => Provider.succeed(Client, {
|
|
402
|
-
diff: ({ news }) => Effect.succeed(isResolved(news) ? { action: "update" } : void 0),
|
|
403
|
-
read: ({ output }) => Effect.succeed(output),
|
|
404
|
-
reconcile: Effect.fn(function* ({ news, output }) {
|
|
405
|
-
const stack = yield* Stack;
|
|
406
|
-
const app = news.app ?? defaultApp(stack.name);
|
|
407
|
-
if (app === void 0) return yield* new IdentityBrokerClientFailed({ reason: `the stack ${stack.name} is not named owner/repo or owner/repo/<path>, so set app` });
|
|
408
|
-
if (app.length > MAX_APP_LENGTH) return yield* new IdentityBrokerClientFailed({ reason: `the app name ${app} is over ${MAX_APP_LENGTH} characters, the broker's limit, so set a shorter app` });
|
|
409
|
-
const enrollment = {
|
|
410
|
-
app,
|
|
411
|
-
clientName: stack.stage,
|
|
412
|
-
connectors: sortedSet(news.connectors),
|
|
413
|
-
redirectUris: sortedSet(news.redirectUris)
|
|
414
|
-
};
|
|
415
|
-
if (enrollment.connectors.length === 0 || !enrollment.connectors.every((name) => CONNECTOR_NAME.test(name))) return yield* new IdentityBrokerClientFailed({ reason: `${where(enrollment)} needs connectors: one or more names of [a-z0-9-]{1,64}` });
|
|
416
|
-
if (enrollment.redirectUris.length === 0 || enrollment.redirectUris.length > MAX_REDIRECT_URIS || !enrollment.redirectUris.every((uri) => URL.canParse(uri))) return yield* new IdentityBrokerClientFailed({ reason: `${app} needs redirectUris: one to ${MAX_REDIRECT_URIS} absolute URLs` });
|
|
417
|
-
const saved = output === void 0 || output.status === "off" ? void 0 : yield* savedAttributes(output, `the saved attributes of ${where(enrollment)} cannot be read by this version`);
|
|
418
|
-
const client = (saved?.issuer === news.broker.issuer && saved.app === app && sameSet(saved.redirectUris, enrollment.redirectUris) && saved.deploymentType === news.deploymentType && sameSet(saved.connectors, enrollment.connectors) ? yield* renew(saved, news.required === true) : void 0) ?? (yield* newClient(news, enrollment));
|
|
419
|
-
if (news.required === true && client.status !== "on") return yield* new IdentityBrokerClientFailed({ reason: `${where(enrollment)} is required and has no working client (${client.status}). The warning above names the reason` });
|
|
420
|
-
return client;
|
|
421
|
-
}),
|
|
422
|
-
delete: ({ output }) => output.deploymentType === "production" || output.status === "off" ? Effect.void : Effect.gen(function* () {
|
|
423
|
-
const { status } = yield* send("The identity broker", output.registrationClientUri, Redacted.value(output.registrationAccessToken), { method: "DELETE" });
|
|
424
|
-
if (status !== 204 && status !== 401) return yield* new IdentityBrokerClientFailed({ reason: `The identity broker answered HTTP ${status}` });
|
|
425
|
-
}).pipe(Effect.catchTag("IdentityBrokerClientFailed", (error) => Effect.logWarning(`IdentityBroker.Client: the client ${output.clientId} stays on the broker until its removal date: ${error.reason}.`)))
|
|
426
|
-
});
|
|
427
|
-
//#endregion
|
|
428
|
-
export { registrationKey as a, defaultApp as i, ClientProvider as n, DISABLED as r, Client as t };
|
|
429
|
-
|
|
430
|
-
//# sourceMappingURL=IdentityBroker-BqA_2V3_.js.map
|