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

RelayDeliveryProof.ts

89 lines3.7 KB

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