Skip to content
Myr

authenticate.test.ts

This file

Path

packages/protocol/test/authenticate.test.ts

150 lines · 4.2 KiB · TypeScript

Last changed by

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

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–16why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import { Effect, Layer, Option, Ref } from "effect";3import { RpcTest } from "effect/rpc";45import { authenticated, bearerToken } from "../src/authenticate.ts";6import { Contracts } from "../src/contracts.ts";7import { Handle, ProjectName } from "../src/ids.ts";8import {9 Anonymous,10 CurrentPrincipal,11 Person,12 Unauthenticated,13} from "../src/principal.ts";14import type { Principal } from "../src/principal.ts";15import { unanswered } from "../src/testing.ts";16

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 17why, one step back
17const project = ProjectName.make("oscabriel/datekit");

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 18–76why, one step back
18
19const alice = Person.make({ handle: Handle.make("alice") });2021const verify = (credential: string) =>22 Effect.succeed(23 credential === "alice-token" ? Option.some(alice) : Option.none()24 );2526const bearer = (token: string) => ({27 headers: { authorization: `Bearer ${token}` },28});2930const served = Effect.gen(function* () {31 const noted = yield* Ref.make<readonly Principal[]>([]);3233 const note = CurrentPrincipal.use((caller) =>34 Ref.update(noted, (callers) => [...callers, caller])35 );3637 const handlers = Layer.mergeAll(38 unanswered,39 Contracts.toLayerHandler("listAsks", () =>40 note.pipe(Effect.as({ asks: [] }))41 ),42 Contracts.toLayerHandler("status", () =>43 note.pipe(44 Effect.as({ answers: [], claims: [], handle: Handle.make("tester") })45 )46 )47 );4849 const client = yield* RpcTest.makeClient(Contracts).pipe(50 Effect.provide(Layer.mergeAll(handlers, authenticated(verify)))51 );5253 return { callers: Ref.get(noted), client };54});5556describe("the server side of Authenticated", () => {57 it.effect("runs a public contract without a credential as anyone", () =>58 Effect.gen(function* () {59 const { callers, client } = yield* served;6061 expect(yield* client.listAsks({ project })).toStrictEqual({ asks: [] });62 expect(yield* callers).toStrictEqual([Anonymous.make({})]);63 })64 );6566 it.effect("refuses a signed-in contract without a credential", () =>67 Effect.gen(function* () {68 const { callers, client } = yield* served;6970 expect(yield* Effect.flip(client.status({}))).toStrictEqual(71 new Unauthenticated()72 );73 expect(yield* callers).toStrictEqual([]);74 })75 );76

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

line 77why, one step back
77 it.effect("refuses a maintainer's contract without a credential", () =>

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 78–150why, one step back
78 Effect.gen(function* () {
79 const { client } = yield* served;8081 expect(yield* Effect.flip(client.today({ project }))).toStrictEqual(82 new Unauthenticated()83 );84 })85 );8687 it.effect(88 "never takes an unknown credential for anyone, even on a public contract",89 () =>90 Effect.gen(function* () {91 const { callers, client } = yield* served;9293 const refused = yield* Effect.flip(94 client.listAsks({ project }, bearer("mallory-token"))95 );9697 expect(refused).toStrictEqual(new Unauthenticated());98 expect(yield* callers).toStrictEqual([]);99 })100 );101102 it.effect("refuses a credential that is not a bearer token", () =>103 Effect.gen(function* () {104 const { callers, client } = yield* served;105106 const refused = yield* Effect.flip(107 client.listAsks(108 { project },109 { headers: { authorization: "Basic YWxpY2UtdG9rZW4=" } }110 )111 );112113 expect(refused).toStrictEqual(new Unauthenticated());114 expect(yield* callers).toStrictEqual([]);115 })116 );117118 it.effect("runs every contract a known credential calls as its person", () =>119 Effect.gen(function* () {120 const { callers, client } = yield* served;121122 yield* client.listAsks({ project }, bearer("alice-token"));123 yield* client.status({}, bearer("alice-token"));124125 expect(yield* callers).toStrictEqual([alice, alice]);126 })127 );128});129130describe(bearerToken, () => {131 it("reads the token whatever the scheme's case, and nothing from another scheme", () => {132 expect(133 [134 "Bearer alice-token",135 "bearer alice-token",136 "BEARER alice-token",137 "Basic YWxpY2U6dG9rZW4=",138 "Bearer",139 undefined,140 ].map(bearerToken)141 ).toStrictEqual([142 Option.some("alice-token"),143 Option.some("alice-token"),144 Option.some("alice-token"),145 Option.none(),146 Option.none(),147 Option.none(),148 ]);149 });150});