state.ts
This file
Path
apps/platform/src/auth/state.ts
134 lines · 3.7 KiB · TypeScript
Last changed by
Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–2why, one step back1import { Clock, Data, Duration, Effect, Redacted, Schema } from "effect";2Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
line 3why, one step back3// The provider writes an absent challenge as `undefined`, so these keys are `optional` (internal JSON, never a wire schema).Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 4–34why, one step back4export const AuthRequest = Schema.Struct({5 clientId: Schema.String,6 codeChallenge: Schema.optional(Schema.String),7 codeChallengeMethod: Schema.optional(Schema.String),8 issuer: Schema.optional(Schema.String),9 redirectUri: Schema.String,10 resource: Schema.optional(Schema.String),11 responseType: Schema.String,12 scope: Schema.Array(Schema.String),13 state: Schema.String,14});1516export type AuthRequest = typeof AuthRequest.Type;1718export const PendingSignIn = Schema.Struct({19 csrf: Schema.String,20 request: AuthRequest,21});2223export type PendingSignIn = typeof PendingSignIn.Type;2425export const SIGN_IN_LIFETIME = Duration.minutes(10);2627const Signature = Schema.Uint8ArrayFromBase64Url;2829const encodeSignature = Schema.encodeEffect(Signature);3031const decodeSignature = Schema.decodeEffect(Signature);3233export class SignInFailed extends Data.TaggedError("SignInFailed")<{34 readonly reason: string;Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
line 35why, one step back35 // Set only once the provider validated the client and its redirect URI.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 36–56why, one step back36 readonly redirectTo?: string;37}> {}3839export const startAgain = new SignInFailed({40 reason:41 "This sign-in page expired or was not opened here. Start signing in again from your app.",42});4344const utf8 = (text: string) => new TextEncoder().encode(text);4546const hmacKey = (key: Redacted.Redacted) =>47 Effect.promise(() =>48 crypto.subtle.importKey(49 "raw",50 utf8(Redacted.value(key)),51 { hash: "SHA-256", name: "HMAC" },52 false,53 ["sign", "verify"]54 )55 );56Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
line 57why, one step back57// The signature covers `purpose` too: a schema ignores extra keys, so a value sealed for another purpose must not open.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 58–134why, one step back58export const sealing = <A, I>(purpose: string, schema: Schema.Codec<A, I>) => {59 const signed = (payload: string) => utf8(`${purpose}.${payload}`);6061 const Payload = Schema.StringFromBase64Url.pipe(62 Schema.decodeTo(63 Schema.fromJsonString(64 Schema.Struct({ expiresAt: Schema.Int, value: schema })65 )66 )67 );6869 const encodePayload = Schema.encodeEffect(Payload);7071 const decodePayload = Schema.decodeEffect(Payload);7273 const seal = (key: Redacted.Redacted, value: A) =>74 Effect.gen(function* () {75 const now = yield* Clock.currentTimeMillis;7677 const payload = yield* encodePayload({78 expiresAt: now + Duration.toMillis(SIGN_IN_LIFETIME),79 value,80 }).pipe(Effect.orDie);8182 const hmac = yield* hmacKey(key);8384 const signature = yield* Effect.promise(() =>85 crypto.subtle.sign("HMAC", hmac, signed(payload))86 );8788 return `${payload}.${yield* encodeSignature(new Uint8Array(signature)).pipe(Effect.orDie)}`;89 });9091 const open = (key: Redacted.Redacted, sealed: string) =>92 Effect.gen(function* () {93 const [payload = "", encoded = ""] = sealed.split(".");9495 const signature = yield* decodeSignature(encoded).pipe(96 Effect.mapError(() => startAgain)97 );9899 const hmac = yield* hmacKey(key);100101 const genuine = yield* Effect.promise(() =>102 crypto.subtle.verify(103 "HMAC",104 hmac,105 new Uint8Array(signature),106 signed(payload)107 )108 );109110 if (!genuine) {111 return yield* startAgain;112 }113114 const { expiresAt, value } = yield* decodePayload(payload).pipe(115 Effect.mapError(() => startAgain)116 );117118 const now = yield* Clock.currentTimeMillis;119120 if (now >= expiresAt) {121 return yield* startAgain;122 }123124 return value;125 });126127 return { open, seal };128};129130const pendingSignIns = sealing("sign-in", PendingSignIn);131132export const sealState = pendingSignIns.seal;133134export const openState = pendingSignIns.open;