Open History to see the last commit
1import * as Mac from "effect/crypto/Mac";
2import * as Effect from "effect/Effect";
3import * as Hex from "effect/encoding/Hex";
4
5/** Where a backend launched without a control channel proves which launch it belongs to. */
6export const DESKTOP_READINESS_PROOF_PATH = "/.well-known/t3/desktop-readiness";
7export const DESKTOP_READINESS_CHALLENGE_PARAM = "challenge";
8export const DESKTOP_READINESS_CHALLENGE_MAX_LENGTH = 128;
9
10const utf8 = (value: string) => new TextEncoder().encode(value);
11
12export const desktopReadinessProof = Effect.fn("desktopReadinessProof.derive")(function* (
13 secret: string,
14 challenge: string,
15) {
16 const mac = yield* Mac.Mac;
17 const key = yield* Mac.key(Mac.hmacSha256, utf8(secret));
18 return Hex.encode(yield* mac.sign(key, utf8(`t3-desktop-readiness:${challenge}`)));
19}, Effect.orDie);
20
21export const isValidDesktopReadinessProof = Effect.fn("desktopReadinessProof.isValid")(function* (
22 secret: string,
23 challenge: string,
24 proof: string,
25) {
26 return Mac.timingSafeEqual(
27 utf8(yield* desktopReadinessProof(secret, challenge)),
28 utf8(proof.trim()),
29 );
30});