@shivaedev/test-kits 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +42 -0
- package/dist/contract/settledQueries.d.ts +13 -0
- package/dist/contract/settledQueries.d.ts.map +1 -0
- package/dist/contract/settledQueries.js +20 -0
- package/dist/contract/settledQueries.js.map +1 -0
- package/dist/github/adapters/appProvider.d.ts +31 -0
- package/dist/github/adapters/appProvider.d.ts.map +1 -0
- package/dist/github/adapters/appProvider.js +31 -0
- package/dist/github/adapters/appProvider.js.map +1 -0
- package/dist/github/adapters/credentials.d.ts +21 -0
- package/dist/github/adapters/credentials.d.ts.map +1 -0
- package/dist/github/adapters/credentials.js +48 -0
- package/dist/github/adapters/credentials.js.map +1 -0
- package/dist/github/adapters/webhookDelivery.d.ts +7 -0
- package/dist/github/adapters/webhookDelivery.d.ts.map +1 -0
- package/dist/github/adapters/webhookDelivery.js +15 -0
- package/dist/github/adapters/webhookDelivery.js.map +1 -0
- package/dist/github/fake.d.ts +52 -0
- package/dist/github/fake.d.ts.map +1 -0
- package/dist/github/fake.js +18 -0
- package/dist/github/fake.js.map +1 -0
- package/dist/host/ProcessError.d.ts +10 -0
- package/dist/host/ProcessError.d.ts.map +1 -0
- package/dist/host/ProcessError.js +4 -0
- package/dist/host/ProcessError.js.map +1 -0
- package/dist/host/adapters/process.d.ts +24 -0
- package/dist/host/adapters/process.d.ts.map +1 -0
- package/dist/host/adapters/process.js +111 -0
- package/dist/host/adapters/process.js.map +1 -0
- package/dist/host/adapters/processReadouts.d.ts +10 -0
- package/dist/host/adapters/processReadouts.d.ts.map +1 -0
- package/dist/host/adapters/processReadouts.js +26 -0
- package/dist/host/adapters/processReadouts.js.map +1 -0
- package/dist/host/process.d.ts +26 -0
- package/dist/host/process.d.ts.map +1 -0
- package/dist/host/process.js +56 -0
- package/dist/host/process.js.map +1 -0
- package/dist/host/processChild.d.ts +7 -0
- package/dist/host/processChild.d.ts.map +1 -0
- package/dist/host/processChild.js +19 -0
- package/dist/host/processChild.js.map +1 -0
- package/dist/host/processProtocol.d.ts +20 -0
- package/dist/host/processProtocol.d.ts.map +1 -0
- package/dist/host/processProtocol.js +11 -0
- package/dist/host/processProtocol.js.map +1 -0
- package/dist/host/processReadouts.d.ts +7 -0
- package/dist/host/processReadouts.d.ts.map +1 -0
- package/dist/host/processReadouts.js +37 -0
- package/dist/host/processReadouts.js.map +1 -0
- package/dist/host/time.d.ts +9 -0
- package/dist/host/time.d.ts.map +1 -0
- package/dist/host/time.js +27 -0
- package/dist/host/time.js.map +1 -0
- package/dist/identity/adapters/oauthAuthorization.d.ts +20 -0
- package/dist/identity/adapters/oauthAuthorization.d.ts.map +1 -0
- package/dist/identity/adapters/oauthAuthorization.js +39 -0
- package/dist/identity/adapters/oauthAuthorization.js.map +1 -0
- package/dist/identity/adapters/oauthProtocol.d.ts +9 -0
- package/dist/identity/adapters/oauthProtocol.d.ts.map +1 -0
- package/dist/identity/adapters/oauthProtocol.js +105 -0
- package/dist/identity/adapters/oauthProtocol.js.map +1 -0
- package/dist/identity/adapters/oidcTokens.d.ts +39 -0
- package/dist/identity/adapters/oidcTokens.d.ts.map +1 -0
- package/dist/identity/adapters/oidcTokens.js +39 -0
- package/dist/identity/adapters/oidcTokens.js.map +1 -0
- package/dist/identity/oauth.d.ts +43 -0
- package/dist/identity/oauth.d.ts.map +1 -0
- package/dist/identity/oauth.js +20 -0
- package/dist/identity/oauth.js.map +1 -0
- package/dist/webhooks/adapters/githubHeaders.d.ts +5 -0
- package/dist/webhooks/adapters/githubHeaders.d.ts.map +1 -0
- package/dist/webhooks/adapters/githubHeaders.js +6 -0
- package/dist/webhooks/adapters/githubHeaders.js.map +1 -0
- package/dist/webhooks/deliver.d.ts +13 -0
- package/dist/webhooks/deliver.d.ts.map +1 -0
- package/dist/webhooks/deliver.js +20 -0
- package/dist/webhooks/deliver.js.map +1 -0
- package/package.json +13 -11
- package/src/contract/settledQueries.ts +39 -0
- package/src/github/adapters/appProvider.ts +34 -0
- package/src/github/adapters/credentials.ts +62 -0
- package/src/github/adapters/webhookDelivery.ts +20 -0
- package/src/github/fake.ts +30 -0
- package/src/host/ProcessError.ts +6 -0
- package/src/host/adapters/process.ts +122 -0
- package/src/host/adapters/processReadouts.ts +29 -0
- package/src/host/process.ts +69 -0
- package/src/host/processChild.ts +25 -0
- package/src/host/processProtocol.ts +11 -0
- package/src/host/processReadouts.ts +46 -0
- package/src/host/time.ts +33 -0
- package/src/identity/adapters/oauthAuthorization.ts +54 -0
- package/src/identity/adapters/oauthProtocol.ts +111 -0
- package/src/identity/adapters/oidcTokens.ts +42 -0
- package/src/identity/oauth.ts +35 -0
- package/src/webhooks/adapters/githubHeaders.ts +6 -0
- package/src/webhooks/deliver.ts +33 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
export const HostReady = Schema.Struct({ pid: Schema.Number, type: Schema.Literal("host-ready"), url: Schema.String });
|
|
4
|
+
export const HostPrepared = Schema.Struct({ type: Schema.Literal("host-prepared") });
|
|
5
|
+
export const HostSignal = Schema.Struct({ signal: Schema.String, type: Schema.Literal("host-signal") });
|
|
6
|
+
export const HostReadouts = Schema.Struct({
|
|
7
|
+
activeWork: Schema.Array(Schema.String),
|
|
8
|
+
finalizers: Schema.Array(Schema.String),
|
|
9
|
+
shutdownDuration: Schema.NullOr(Schema.Number),
|
|
10
|
+
type: Schema.Literal("host-readouts"),
|
|
11
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Clock, Duration, Effect } from "effect";
|
|
2
|
+
import { sendHostMessage } from "#host/adapters/process.ts";
|
|
3
|
+
import { listenHostReadouts } from "#host/adapters/processReadouts.ts";
|
|
4
|
+
import { HostProcessError } from "./ProcessError.ts";
|
|
5
|
+
|
|
6
|
+
export const hostProcessReadouts = Effect.gen(function* () {
|
|
7
|
+
const active = new Map<symbol, string>();
|
|
8
|
+
const finalizers: string[] = [];
|
|
9
|
+
let shutdownDuration: number | null = null;
|
|
10
|
+
let shutdownWork: readonly string[] | undefined;
|
|
11
|
+
function read() {
|
|
12
|
+
return { activeWork: shutdownWork ?? [...active.values()], finalizers: [...finalizers], shutdownDuration, type: "host-readouts" as const };
|
|
13
|
+
}
|
|
14
|
+
function finishShutdown(started: bigint) {
|
|
15
|
+
return Effect.gen(function* () {
|
|
16
|
+
shutdownDuration = Duration.toMillis(Duration.nanos((yield* Clock.currentTimeNanos) - started));
|
|
17
|
+
yield* Effect.tryPromise({
|
|
18
|
+
catch: (cause) => new HostProcessError({ cause, operation: "readout" }),
|
|
19
|
+
try: () => sendHostMessage(read()),
|
|
20
|
+
}).pipe(Effect.orDie);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
yield* Effect.acquireRelease(
|
|
24
|
+
Effect.sync(() => listenHostReadouts(read)),
|
|
25
|
+
(close) => Effect.sync(close),
|
|
26
|
+
);
|
|
27
|
+
return {
|
|
28
|
+
finalizer: <A, E, R>(name: string, effect: Effect.Effect<A, E, R>) => effect.pipe(Effect.ensuring(Effect.sync(() => finalizers.push(name)))),
|
|
29
|
+
shutdown: <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
30
|
+
Effect.gen(function* () {
|
|
31
|
+
shutdownWork = [...active.values()];
|
|
32
|
+
const started = yield* Clock.currentTimeNanos;
|
|
33
|
+
return yield* effect.pipe(Effect.ensuring(finishShutdown(started)));
|
|
34
|
+
}),
|
|
35
|
+
work: <A, E, R>(name: string, effect: Effect.Effect<A, E, R>) =>
|
|
36
|
+
Effect.acquireUseRelease(
|
|
37
|
+
Effect.sync(() => {
|
|
38
|
+
const id = Symbol(name);
|
|
39
|
+
active.set(id, name);
|
|
40
|
+
return id;
|
|
41
|
+
}),
|
|
42
|
+
() => effect,
|
|
43
|
+
(id) => Effect.sync(() => active.delete(id)),
|
|
44
|
+
),
|
|
45
|
+
};
|
|
46
|
+
});
|
package/src/host/time.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Duration, Effect, Layer } from "effect";
|
|
2
|
+
import { TestClock } from "effect/testing";
|
|
3
|
+
import { WorkerReleaseClock } from "@shivaedev/sql/WorkerReleaseClock.ts";
|
|
4
|
+
|
|
5
|
+
export const hostTime = Effect.fn("HostTime.hostTime")(function* () {
|
|
6
|
+
const clock = yield* TestClock.testClockWith(Effect.succeed);
|
|
7
|
+
const ticks = new Set<Parameters<typeof WorkerReleaseClock.Service.register>[0]>();
|
|
8
|
+
return {
|
|
9
|
+
layer: Layer.succeed(WorkerReleaseClock, {
|
|
10
|
+
clock,
|
|
11
|
+
register: (tick) =>
|
|
12
|
+
Effect.acquireRelease(
|
|
13
|
+
Effect.sync(() => {
|
|
14
|
+
ticks.add(tick);
|
|
15
|
+
}),
|
|
16
|
+
() =>
|
|
17
|
+
Effect.sync(() => {
|
|
18
|
+
ticks.delete(tick);
|
|
19
|
+
}),
|
|
20
|
+
).pipe(Effect.asVoid),
|
|
21
|
+
}),
|
|
22
|
+
timePasses: (duration: Duration.Input, options?: { readonly poll?: boolean }) =>
|
|
23
|
+
Effect.gen(function* () {
|
|
24
|
+
yield* clock.adjust(duration);
|
|
25
|
+
for (const tick of ticks) {
|
|
26
|
+
yield* tick.heartbeat;
|
|
27
|
+
if (options?.poll !== false) {
|
|
28
|
+
yield* tick.poll;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}),
|
|
32
|
+
};
|
|
33
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { FakeHttpReply, FakeHttpRequest } from "@shivaedev/test-story/server/fakeHttpService.ts";
|
|
3
|
+
import type { OauthProfile } from "#identity/oauth.ts";
|
|
4
|
+
|
|
5
|
+
export interface OauthCode {
|
|
6
|
+
readonly expires: number;
|
|
7
|
+
readonly profile: OauthProfile;
|
|
8
|
+
readonly query: URLSearchParams;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface OauthProtocolOptions {
|
|
12
|
+
readonly clientId: string;
|
|
13
|
+
readonly clientSecret: string;
|
|
14
|
+
readonly codeLifetime: number;
|
|
15
|
+
readonly now: () => number;
|
|
16
|
+
readonly profile: OauthProfile | ((request: FakeHttpRequest) => OauthProfile);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function oauthAuthorization(options: OauthProtocolOptions) {
|
|
20
|
+
const codes = new Map<string, OauthCode>();
|
|
21
|
+
let consent = true;
|
|
22
|
+
return {
|
|
23
|
+
authorize: (request: FakeHttpRequest): FakeHttpReply => {
|
|
24
|
+
const { query } = request;
|
|
25
|
+
if (query.get("client_id") !== options.clientId) {
|
|
26
|
+
return { body: { error: "invalid_client" }, status: 400 };
|
|
27
|
+
}
|
|
28
|
+
if (query.get("response_type") !== "code") {
|
|
29
|
+
return { body: { error: "unsupported_response_type" }, status: 400 };
|
|
30
|
+
}
|
|
31
|
+
const callback = new URL(query.get("redirect_uri") ?? "");
|
|
32
|
+
const state = query.get("state");
|
|
33
|
+
if (state !== null) {
|
|
34
|
+
callback.searchParams.set("state", state);
|
|
35
|
+
}
|
|
36
|
+
if (consent) {
|
|
37
|
+
const value = randomUUID();
|
|
38
|
+
codes.set(value, {
|
|
39
|
+
expires: options.now() + options.codeLifetime,
|
|
40
|
+
profile: typeof options.profile === "function" ? options.profile(request) : options.profile,
|
|
41
|
+
query,
|
|
42
|
+
});
|
|
43
|
+
callback.searchParams.set("code", value);
|
|
44
|
+
} else {
|
|
45
|
+
callback.searchParams.set("error", "access_denied");
|
|
46
|
+
}
|
|
47
|
+
return { headers: { location: callback.href }, status: 302 };
|
|
48
|
+
},
|
|
49
|
+
codes,
|
|
50
|
+
consent: (allowed: boolean) => {
|
|
51
|
+
consent = allowed;
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { Clock, Effect } from "effect";
|
|
3
|
+
import type { FakeHttpReply, FakeHttpRequest, FakeHttpRoute } from "@shivaedev/test-story/server/fakeHttpService.ts";
|
|
4
|
+
import type { OauthProfile } from "#identity/oauth.ts";
|
|
5
|
+
import { type OauthProtocolOptions, oauthAuthorization } from "./oauthAuthorization.ts";
|
|
6
|
+
import { oidcTokens } from "./oidcTokens.ts";
|
|
7
|
+
|
|
8
|
+
function validVerifier(query: URLSearchParams, form: URLSearchParams) {
|
|
9
|
+
return (
|
|
10
|
+
query.get("code_challenge_method") === "S256"
|
|
11
|
+
&& createHash("sha256")
|
|
12
|
+
.update(form.get("code_verifier") ?? "")
|
|
13
|
+
.digest("base64url") === query.get("code_challenge")
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function clientCredentials(authorization: string | undefined, form: URLSearchParams) {
|
|
18
|
+
if (authorization?.startsWith("Basic ")) {
|
|
19
|
+
const [clientId, clientSecret] = Buffer.from(authorization.slice(6), "base64").toString().split(":");
|
|
20
|
+
return { clientId, clientSecret };
|
|
21
|
+
}
|
|
22
|
+
return { clientId: form.get("client_id"), clientSecret: form.get("client_secret") };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const oauthProtocol = Effect.fnUntraced(function* (options: Omit<OauthProtocolOptions, "now">) {
|
|
26
|
+
const clock = yield* Clock.Clock;
|
|
27
|
+
const authorization = oauthAuthorization({ ...options, now: () => clock.currentTimeMillisUnsafe() });
|
|
28
|
+
const { codes } = authorization;
|
|
29
|
+
const tokens = new Map<string, { readonly profile: OauthProfile; readonly expires: number }>();
|
|
30
|
+
const signing = oidcTokens();
|
|
31
|
+
let issuer = "";
|
|
32
|
+
function refused(error: string, status = 400): FakeHttpReply {
|
|
33
|
+
return { body: { error }, status };
|
|
34
|
+
}
|
|
35
|
+
function profileOf({ headers }: FakeHttpRequest) {
|
|
36
|
+
const token = tokens.get(headers.authorization?.replace(/^Bearer /iu, "") ?? "");
|
|
37
|
+
return token !== undefined && token.expires > clock.currentTimeMillisUnsafe() ? token.profile : undefined;
|
|
38
|
+
}
|
|
39
|
+
function exchange({ body, headers }: FakeHttpRequest): FakeHttpReply {
|
|
40
|
+
const form = new URLSearchParams(body.toString());
|
|
41
|
+
const credentials = clientCredentials(headers.authorization, form);
|
|
42
|
+
if (credentials.clientId !== options.clientId || credentials.clientSecret !== options.clientSecret) {
|
|
43
|
+
return refused("invalid_client", 401);
|
|
44
|
+
}
|
|
45
|
+
if (form.get("grant_type") !== "authorization_code") {
|
|
46
|
+
return refused("unsupported_grant_type");
|
|
47
|
+
}
|
|
48
|
+
const value = form.get("code") ?? "";
|
|
49
|
+
const code = codes.get(value);
|
|
50
|
+
if (code === undefined || code.expires <= clock.currentTimeMillisUnsafe() || code.query.get("redirect_uri") !== form.get("redirect_uri")) {
|
|
51
|
+
return refused("invalid_grant");
|
|
52
|
+
}
|
|
53
|
+
const challenge = code.query.get("code_challenge");
|
|
54
|
+
if (challenge !== null && !validVerifier(code.query, form)) {
|
|
55
|
+
return refused("invalid_grant");
|
|
56
|
+
}
|
|
57
|
+
codes.delete(value);
|
|
58
|
+
const accessToken = randomUUID();
|
|
59
|
+
tokens.set(accessToken, { expires: clock.currentTimeMillisUnsafe() + 3_600_000, profile: code.profile });
|
|
60
|
+
return {
|
|
61
|
+
body: {
|
|
62
|
+
"access_token": accessToken,
|
|
63
|
+
"expires_in": 3600,
|
|
64
|
+
"id_token": signing.sign(code.profile, issuer, options.clientId, clock.currentTimeMillisUnsafe(), code.query.get("nonce")),
|
|
65
|
+
"refresh_token": randomUUID(),
|
|
66
|
+
scope: code.query.get("scope") ?? "",
|
|
67
|
+
"token_type": "Bearer",
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const routes: Record<string, FakeHttpRoute> = {
|
|
72
|
+
authorize: { path: "/login/oauth/authorize", reply: authorization.authorize },
|
|
73
|
+
discovery: { path: "/.well-known/openid-configuration", reply: () => ({ body: signing.discovery(issuer) }) },
|
|
74
|
+
emails: {
|
|
75
|
+
path: "/user/emails",
|
|
76
|
+
reply: (request) => {
|
|
77
|
+
const profile = profileOf(request);
|
|
78
|
+
return profile === undefined
|
|
79
|
+
? refused("invalid_token", 401)
|
|
80
|
+
: { body: [{ email: profile.email, primary: true, verified: profile.emailVerified ?? true }] };
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
jwks: { path: "/jwks", reply: () => ({ body: signing.jwks }) },
|
|
84
|
+
token: { method: "POST", path: "/login/oauth/access_token", reply: exchange },
|
|
85
|
+
user: {
|
|
86
|
+
path: "/user",
|
|
87
|
+
reply: (request) => {
|
|
88
|
+
const profile = profileOf(request);
|
|
89
|
+
return profile === undefined
|
|
90
|
+
? refused("invalid_token", 401)
|
|
91
|
+
: { body: { "avatar_url": null, email: profile.email, id: profile.sub, login: profile.name, name: profile.name } };
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
userinfo: {
|
|
95
|
+
path: "/userinfo",
|
|
96
|
+
reply: (request) => {
|
|
97
|
+
const profile = profileOf(request);
|
|
98
|
+
return profile === undefined
|
|
99
|
+
? refused("invalid_token", 401)
|
|
100
|
+
: { body: { email: profile.email, "email_verified": profile.emailVerified ?? true, name: profile.name, sub: profile.sub } };
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
return {
|
|
105
|
+
consent: authorization.consent,
|
|
106
|
+
issuer: (url: string) => {
|
|
107
|
+
issuer = url;
|
|
108
|
+
},
|
|
109
|
+
routes,
|
|
110
|
+
};
|
|
111
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { generateKeyPairSync, sign } from "node:crypto";
|
|
2
|
+
import type { OauthProfile } from "#identity/oauth.ts";
|
|
3
|
+
|
|
4
|
+
export function oidcTokens() {
|
|
5
|
+
const { privateKey, publicKey } = generateKeyPairSync("rsa", { modulusLength: 2048 });
|
|
6
|
+
const key = { ...publicKey.export({ format: "jwk" }), alg: "RS256", kid: "story-key", use: "sig" };
|
|
7
|
+
return {
|
|
8
|
+
discovery: (issuer: string) => ({
|
|
9
|
+
"authorization_endpoint": `${issuer}/login/oauth/authorize`,
|
|
10
|
+
"code_challenge_methods_supported": ["S256"],
|
|
11
|
+
"grant_types_supported": ["authorization_code"],
|
|
12
|
+
"id_token_signing_alg_values_supported": ["RS256"],
|
|
13
|
+
issuer,
|
|
14
|
+
"jwks_uri": `${issuer}/jwks`,
|
|
15
|
+
"response_types_supported": ["code"],
|
|
16
|
+
"scopes_supported": ["openid", "profile", "email"],
|
|
17
|
+
"subject_types_supported": ["public"],
|
|
18
|
+
"token_endpoint": `${issuer}/login/oauth/access_token`,
|
|
19
|
+
"token_endpoint_auth_methods_supported": ["client_secret_post", "client_secret_basic"],
|
|
20
|
+
"userinfo_endpoint": `${issuer}/userinfo`,
|
|
21
|
+
}),
|
|
22
|
+
jwks: { keys: [key] },
|
|
23
|
+
sign: (profile: OauthProfile, issuer: string, audience: string, now: number, nonce: string | null) => {
|
|
24
|
+
const header = Buffer.from(JSON.stringify({ alg: "RS256", kid: key.kid, typ: "JWT" })).toString("base64url");
|
|
25
|
+
const payload = Buffer.from(
|
|
26
|
+
JSON.stringify({
|
|
27
|
+
aud: audience,
|
|
28
|
+
email: profile.email,
|
|
29
|
+
"email_verified": profile.emailVerified ?? true,
|
|
30
|
+
exp: Math.floor(now / 1000) + 3600,
|
|
31
|
+
iat: Math.floor(now / 1000),
|
|
32
|
+
iss: issuer,
|
|
33
|
+
name: profile.name,
|
|
34
|
+
...(nonce === null ? {} : { nonce }),
|
|
35
|
+
sub: profile.sub,
|
|
36
|
+
}),
|
|
37
|
+
).toString("base64url");
|
|
38
|
+
const body = `${header}.${payload}`;
|
|
39
|
+
return `${body}.${sign("RSA-SHA256", Buffer.from(body), privateKey).toString("base64url")}`;
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Duration, Effect } from "effect";
|
|
2
|
+
import { type FakeHttpRequest, type FakeHttpRoute, fakeHttpService } from "@shivaedev/test-story/server/fakeHttpService.ts";
|
|
3
|
+
import { oauthProtocol } from "#identity/adapters/oauthProtocol.ts";
|
|
4
|
+
|
|
5
|
+
export interface OauthProfile {
|
|
6
|
+
readonly email: string;
|
|
7
|
+
readonly emailVerified?: boolean;
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly sub: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface FakeOauthOptions {
|
|
13
|
+
readonly clientId?: string;
|
|
14
|
+
readonly clientSecret?: string;
|
|
15
|
+
readonly codeLifetime?: Duration.Input;
|
|
16
|
+
readonly profile?: OauthProfile | ((request: FakeHttpRequest) => OauthProfile);
|
|
17
|
+
readonly routes?: Readonly<Record<string, FakeHttpRoute>>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const fakeOauthProvider = Effect.fnUntraced(function* (options: FakeOauthOptions = {}) {
|
|
21
|
+
const credentials = { clientId: options.clientId ?? "story-client", clientSecret: options.clientSecret ?? "story-secret" };
|
|
22
|
+
const protocol = yield* oauthProtocol({
|
|
23
|
+
...credentials,
|
|
24
|
+
codeLifetime: Duration.toMillis(options.codeLifetime ?? "1 minute"),
|
|
25
|
+
profile: options.profile ?? { email: "alice@social.test", name: "Alice", sub: "123" },
|
|
26
|
+
});
|
|
27
|
+
const server = yield* fakeHttpService({ routes: { ...protocol.routes, ...options.routes } });
|
|
28
|
+
protocol.issuer(server.url);
|
|
29
|
+
return {
|
|
30
|
+
credentials,
|
|
31
|
+
discoveryUrl: `${server.url}/.well-known/openid-configuration`,
|
|
32
|
+
refuseConsent: () => Effect.sync(() => protocol.consent(false)),
|
|
33
|
+
server,
|
|
34
|
+
};
|
|
35
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { createHmac } from "node:crypto";
|
|
2
|
+
import { Effect, Redacted } from "effect";
|
|
3
|
+
|
|
4
|
+
export function githubHeaders(body: string, key: Redacted.Redacted<string>) {
|
|
5
|
+
return Effect.sync(() => ({ "x-hub-signature-256": `sha256=${createHmac("sha256", Redacted.value(key)).update(body).digest("hex")}` }));
|
|
6
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Effect, type Redacted } from "effect";
|
|
2
|
+
import { HttpBody, HttpClient } from "effect/http";
|
|
3
|
+
import type { WebhookSource } from "@shivaedev/webhooks/server/registration.ts";
|
|
4
|
+
import { githubHeaders } from "#webhooks/adapters/githubHeaders.ts";
|
|
5
|
+
|
|
6
|
+
export type WebhookSigner<E = never, R = never> = (
|
|
7
|
+
body: string,
|
|
8
|
+
key: Redacted.Redacted<string>,
|
|
9
|
+
) => Effect.Effect<Readonly<Record<string, string>>, E, R>;
|
|
10
|
+
|
|
11
|
+
export interface WebhookDeliveryOptions<E = never, R = never> {
|
|
12
|
+
readonly body: string;
|
|
13
|
+
readonly headers?: Readonly<Record<string, string | null>>;
|
|
14
|
+
readonly sign?: WebhookSigner<E, R>;
|
|
15
|
+
readonly source: Pick<WebhookSource, "key">;
|
|
16
|
+
readonly url: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const deliverWebhook = Effect.fnUntraced(function* <E = never, R = never>(options: WebhookDeliveryOptions<E, R>) {
|
|
20
|
+
const key = yield* options.source.key;
|
|
21
|
+
const signed = yield* (options.sign ?? githubHeaders)(options.body, key);
|
|
22
|
+
const headers: Record<string, string> = {};
|
|
23
|
+
for (const [name, value] of [...Object.entries(signed), ...Object.entries(options.headers ?? {})]) {
|
|
24
|
+
const lower = name.toLowerCase();
|
|
25
|
+
if (value === null) {
|
|
26
|
+
delete headers[lower];
|
|
27
|
+
} else {
|
|
28
|
+
headers[lower] = value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const client = yield* HttpClient.HttpClient;
|
|
32
|
+
return yield* client.post(options.url, { body: HttpBody.text(options.body, "application/json"), headers });
|
|
33
|
+
});
|