Skip to content
Myr

credential.ts

This file

Path

apps/platform/src/credential.ts

31 lines · 1.1 KiB · TypeScript

Last changed by

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

History of this file

Take 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–31why, one step back
1import { parseAuditUser } from "@repo/protocol/audit";
2import type { HarnessAudit } from "@repo/protocol/audit";3import { bearerToken } from "@repo/protocol/authenticate";4import { Option, Schema } from "effect";56const BASIC = /^Basic (?<encoded>\S+)$/u;78const fromBase64 = Schema.decodeOption(Schema.StringFromBase64);910const basicPairOf = (headers: Headers): Option.Option<string> =>11 Option.fromNullishOr(12 BASIC.exec(headers.get("authorization") ?? "")?.groups?.encoded13 ).pipe(Option.flatMap(fromBase64));1415const passwordOf = (pair: string) => {16 const colon = pair.indexOf(":");1718 return colon === -1 || colon === pair.length - 119 ? Option.none()20 : Option.some(pair.slice(colon + 1));21};2223const userOf = (pair: string) => pair.slice(0, Math.max(pair.indexOf(":"), 0));2425export const credentialOf = (headers: Headers): Option.Option<string> =>26 Option.orElse(bearerToken(headers.get("authorization") ?? undefined), () =>27 Option.flatMap(basicPairOf(headers), passwordOf)28 );2930export const auditOf = (headers: Headers): Option.Option<HarnessAudit> =>31 Option.flatMap(basicPairOf(headers), (pair) => parseAuditUser(userOf(pair)));