Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

RelayDeliveryProof.ts

84 lines3.5 KB

1// Trust relay headers only with a mint-key-signed proof; otherwise handle the webhook as a direct request.
2import { RelayHookDeliveryProofPayload } from "@t3tools/contracts/relay";
3import {
4 normalizeRelayIssuer,
5 RELAY_HOOK_DELIVERY_HEADER,
6 RELAY_HOOK_DELIVERY_TYP,
7 verifyRelayJwt,
8} from "@t3tools/shared/relayJwt";
9import * as Clock from "effect/Clock";
10import * as Context from "effect/Context";
11import * as Effect from "effect/Effect";
12import * as Layer from "effect/Layer";
13import * as Option from "effect/Option";
14import * as Schema from "effect/Schema";
15​
16import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
17import { CLOUD_MINT_PUBLIC_KEY, RELAY_ISSUER_SECRET, RELAY_URL_SECRET } from "../cloud/config.ts";
18import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
19​
20/** Covers the relay's 24-hour hold plus a margin. */
21const MAX_PROOF_AGE_SECONDS = 25 * 60 * 60;
22​
23const decodePayload = Schema.decodeUnknownOption(RelayHookDeliveryProofPayload);
24const text = (bytes: Option.Option<Uint8Array>) =>
25 Option.map(bytes, (value) => new TextDecoder().decode(value));
26​
27export class RelayDeliveryProof extends Context.Service<
28 RelayDeliveryProof,
29 {
30 readonly verify: (input: {
31 readonly headers: Readonly<Record<string, string>>;
32 readonly hookId: string;
33 }) => Effect.Effect<
34 Option.Option<{ readonly deliveryId: string; readonly receivedAt: string }>
35 >;
36 }
37>()("t3/scheduledTasks/RelayDeliveryProof") {}
38​
39const make = Effect.gen(function* () {
40 const secrets = yield* ServerSecretStore.ServerSecretStore;
41 const environment = yield* ServerEnvironment.ServerEnvironment;
42 const verify: RelayDeliveryProof["Service"]["verify"] = Effect.fn("webhook.verifyRelayDelivery")(
43 function* (input) {
44 const proof = input.headers[RELAY_HOOK_DELIVERY_HEADER];
45 const deliveryId = input.headers["x-t3-relay-delivery-id"];
46 const receivedAt = input.headers["x-t3-relay-received-at"];
47 if (proof === undefined || deliveryId === undefined || receivedAt === undefined) {
48 return Option.none();
49 }
50 const publicKey = text(yield* secrets.get(CLOUD_MINT_PUBLIC_KEY));
51 const issuer = Option.orElse(text(yield* secrets.get(RELAY_ISSUER_SECRET)), () =>
52 Option.none<string>(),
53 );
54 const relayUrl = text(yield* secrets.get(RELAY_URL_SECRET));
55 const relayIssuer = Option.isSome(issuer) ? issuer : relayUrl;
56 if (Option.isNone(publicKey) || Option.isNone(relayIssuer)) return Option.none();
57 const environmentId = yield* environment.getEnvironmentId;
58 const payload = yield* verifyRelayJwt({
59 publicKey: publicKey.value,
60 token: proof,
61 typ: RELAY_HOOK_DELIVERY_TYP,
62 issuer: normalizeRelayIssuer(relayIssuer.value),
63 audience: `t3-env:${environmentId}`,
64 nowEpochSeconds: Math.floor((yield* Clock.currentTimeMillis) / 1_000),
65 maxTokenAge: MAX_PROOF_AGE_SECONDS,
66 }).pipe(Effect.map(decodePayload), Effect.orElseSucceed(Option.none));
67 // The proof must name exactly this delivery, so it cannot be lifted onto another one.
68 if (
69 Option.isNone(payload) ||
70 payload.value.environmentId !== environmentId ||
71 payload.value.deliveryId !== deliveryId ||
72 payload.value.receivedAt !== receivedAt ||
73 payload.value.hookId !== input.hookId
74 ) {
75 return Option.none();
76 }
77 return Option.some({ deliveryId, receivedAt });
78 },
79 Effect.orElseSucceed(Option.none),
80 );
81 return RelayDeliveryProof.of({ verify });
82});
83​
84export const layer = Layer.effect(RelayDeliveryProof, make);