| @@ -2,9 +2,9 @@ import { · 1 unmodified line |
| 2 | 2 | computeDpopAccessTokenHash, |
| 3 | 3 | computeDpopJwkThumbprint, |
| 4 | 4 | DpopPublicJwk, |
| 5 | + redactDpopRequestTarget, |
| 5 | 6 | } from "@t3tools/shared/dpop"; |
| 6 | 7 | import * as Crypto from "effect/Crypto"; |
| 7 | | −import * as Data from "effect/Data"; |
| 8 | 8 | import * as Effect from "effect/Effect"; |
| 9 | 9 | import * as Layer from "effect/Layer"; |
| 10 | 10 | import * as Schema from "effect/Schema"; |
| @@ -16,10 +16,62 @@ export interface BrowserDpopKey { · 5 unmodified lines |
| 16 | 16 | readonly thumbprint: string; |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | | −export class BrowserDpopError extends Data.TaggedError("BrowserDpopError")<{ |
| 20 | | − readonly message: string; |
| 21 | | − readonly cause?: unknown; |
| 22 | | −}> {} |
| 19 | +export class BrowserDpopStorageError extends Schema.TaggedErrorClass<BrowserDpopStorageError>()( |
| 20 | + "BrowserDpopStorageError", |
| 21 | + { |
| 22 | + operation: Schema.Literals(["open", "read", "write"]), |
| 23 | + databaseName: Schema.String, |
| 24 | + storeName: Schema.String, |
| 25 | + keyId: Schema.String, |
| 26 | + cause: Schema.optional(Schema.Defect()), |
| 27 | + }, |
| 28 | +) { |
| 29 | + override get message(): string { |
| 30 | + return `Browser DPoP key storage operation "${this.operation}" failed for database "${this.databaseName}".`; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export class BrowserDpopKeyError extends Schema.TaggedErrorClass<BrowserDpopKeyError>()( |
| 35 | + "BrowserDpopKeyError", |
| 36 | + { |
| 37 | + operation: Schema.Literals([ |
| 38 | + "generate", |
| 39 | + "export-private", |
| 40 | + "export-public", |
| 41 | + "validate-public", |
| 42 | + "import-private", |
| 43 | + ]), |
| 44 | + cause: Schema.Defect(), |
| 45 | + }, |
| 46 | +) { |
| 47 | + override get message(): string { |
| 48 | + return `Browser DPoP key operation "${this.operation}" failed.`; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export class BrowserDpopProofError extends Schema.TaggedErrorClass<BrowserDpopProofError>()( |
| 53 | + "BrowserDpopProofError", |
| 54 | + { |
| 55 | + operation: Schema.Literals(["normalize-url", "generate-id", "sign"]), |
| 56 | + method: Schema.String, |
| 57 | + requestTarget: Schema.String, |
| 58 | + urlLength: Schema.Number, |
| 59 | + thumbprint: Schema.String, |
| 60 | + cause: Schema.Defect(), |
| 61 | + }, |
| 62 | +) { |
| 63 | + override get message(): string { |
| 64 | + return `Browser DPoP proof operation "${this.operation}" failed for ${this.method.toUpperCase()} ${this.requestTarget}.`; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export const BrowserDpopError = Schema.Union([ |
| 69 | + BrowserDpopStorageError, |
| 70 | + BrowserDpopKeyError, |
| 71 | + BrowserDpopProofError, |
| 72 | +]); |
| 73 | +export type BrowserDpopError = typeof BrowserDpopError.Type; |
| 74 | +export const isBrowserDpopError = Schema.is(BrowserDpopError); |
| 23 | 75 | |
| 24 | 76 | const DPOP_DATABASE_NAME = "t3code:cloud-auth"; |
| 25 | 77 | const DPOP_DATABASE_VERSION = 1; |
| @@ -40,16 +92,20 @@ export const browserCryptoLayer = Layer.succeed( · 14 unmodified lines |
| 40 | 92 | }), |
| 41 | 93 | ); |
| 42 | 94 | |
| 43 | | −function dpopError(message: string, cause?: unknown) { |
| 44 | | − return new BrowserDpopError({ message, ...(cause === undefined ? {} : { cause }) }); |
| 45 | | −} |
| 46 | | − |
| 47 | 95 | function openDpopDatabase(): Effect.Effect<IDBDatabase, BrowserDpopError> { |
| 48 | 96 | return Effect.callback<IDBDatabase, BrowserDpopError>((resume) => { |
| 49 | 97 | const request = indexedDB.open(DPOP_DATABASE_NAME, DPOP_DATABASE_VERSION); |
| 50 | 98 | request.addEventListener("error", () => |
| 51 | 99 | resume( |
| 52 | | − Effect.fail(dpopError("Could not open DPoP key storage.", request.error ?? undefined)), |
| 100 | + Effect.fail( |
| 101 | + new BrowserDpopStorageError({ |
| 102 | + operation: "open", |
| 103 | + databaseName: DPOP_DATABASE_NAME, |
| 104 | + storeName: DPOP_KEY_STORE_NAME, |
| 105 | + keyId: DPOP_KEY_ID, |
| 106 | + ...(request.error === null ? {} : { cause: request.error }), |
| 107 | + }), |
| 108 | + ), |
| 53 | 109 | ), |
| 54 | 110 | ); |
| 55 | 111 | request.addEventListener("upgradeneeded", () => { |
| @@ -74,7 +130,17 @@ export function readStoredBrowserDpopKey(): Effect.Effect<BrowserDpopKey | null, · 18 unmodified lines |
| 74 | 130 | .objectStore(DPOP_KEY_STORE_NAME) |
| 75 | 131 | .get(DPOP_KEY_ID); |
| 76 | 132 | request.addEventListener("error", () => |
| 77 | | − resume(Effect.fail(dpopError("Could not read DPoP key.", request.error ?? undefined))), |
| 133 | + resume( |
| 134 | + Effect.fail( |
| 135 | + new BrowserDpopStorageError({ |
| 136 | + operation: "read", |
| 137 | + databaseName: DPOP_DATABASE_NAME, |
| 138 | + storeName: DPOP_KEY_STORE_NAME, |
| 139 | + keyId: DPOP_KEY_ID, |
| 140 | + ...(request.error === null ? {} : { cause: request.error }), |
| 141 | + }), |
| 142 | + ), |
| 143 | + ), |
| 78 | 144 | ); |
| 79 | 145 | request.addEventListener("success", () => |
| 80 | 146 | resume(Effect.succeed((request.result as BrowserDpopKey | undefined) ?? null)), |
| @@ -97,7 +163,15 @@ export function writeStoredBrowserDpopKey( · 16 unmodified lines |
| 97 | 163 | const transaction = database.transaction(DPOP_KEY_STORE_NAME, "readwrite"); |
| 98 | 164 | transaction.addEventListener("error", () => |
| 99 | 165 | resume( |
| 100 | | − Effect.fail(dpopError("Could not write DPoP key.", transaction.error ?? undefined)), |
| 166 | + Effect.fail( |
| 167 | + new BrowserDpopStorageError({ |
| 168 | + operation: "write", |
| 169 | + databaseName: DPOP_DATABASE_NAME, |
| 170 | + storeName: DPOP_KEY_STORE_NAME, |
| 171 | + keyId: DPOP_KEY_ID, |
| 172 | + ...(transaction.error === null ? {} : { cause: transaction.error }), |
| 173 | + }), |
| 174 | + ), |
| 101 | 175 | ), |
| 102 | 176 | ); |
| 103 | 177 | transaction.addEventListener("complete", () => resume(Effect.void)); |
| @@ -114,26 +188,22 @@ export const generateBrowserDpopKey = Effect.gen(function* () { · 10 unmodified lines |
| 114 | 188 | "sign", |
| 115 | 189 | "verify", |
| 116 | 190 | ]) as Promise<CryptoKeyPair>, |
| 117 | | − catch: (cause) => dpopError("Could not generate DPoP proof key.", cause), |
| 191 | + catch: (cause) => new BrowserDpopKeyError({ operation: "generate", cause }), |
| 118 | 192 | }); |
| 119 | 193 | const privateJwk = yield* Effect.tryPromise({ |
| 120 | 194 | try: () => crypto.subtle.exportKey("jwk", generated.privateKey), |
| 121 | | − catch: (cause) => dpopError("Could not export DPoP private key.", cause), |
| 195 | + catch: (cause) => new BrowserDpopKeyError({ operation: "export-private", cause }), |
| 122 | 196 | }); |
| 123 | | − const publicJwk = yield* Effect.tryPromise({ |
| 197 | + const encodedPublicJwk = yield* Effect.tryPromise({ |
| 124 | 198 | try: () => crypto.subtle.exportKey("jwk", generated.publicKey), |
| 125 | | − catch: (cause) => dpopError("Could not export DPoP public key.", cause), |
| 126 | | − }).pipe( |
| 127 | | − Effect.flatMap((jwk) => decodeDpopPublicJwk(jwk)), |
| 128 | | − Effect.mapError((cause) => |
| 129 | | − cause instanceof BrowserDpopError |
| 130 | | − ? cause |
| 131 | | − : dpopError("Generated DPoP public key is invalid.", cause), |
| 132 | | − ), |
| 199 | + catch: (cause) => new BrowserDpopKeyError({ operation: "export-public", cause }), |
| 200 | + }); |
| 201 | + const publicJwk = yield* decodeDpopPublicJwk(encodedPublicJwk).pipe( |
| 202 | + Effect.mapError((cause) => new BrowserDpopKeyError({ operation: "validate-public", cause })), |
| 133 | 203 | ); |
| 134 | 204 | const privateKey = yield* Effect.tryPromise({ |
| 135 | 205 | try: () => importJWK(privateJwk as JWK, "ES256", { extractable: false }) as Promise<CryptoKey>, |
| 136 | | − catch: (cause) => dpopError("Could not import DPoP private key.", cause), |
| 206 | + catch: (cause) => new BrowserDpopKeyError({ operation: "import-private", cause }), |
| 137 | 207 | }); |
| 138 | 208 | return { |
| 139 | 209 | privateKey, |
| @@ -153,15 +223,35 @@ export function createBrowserDpopProof(input: { · 13 unmodified lines |
| 153 | 223 | Crypto.Crypto |
| 154 | 224 | > { |
| 155 | 225 | return Effect.gen(function* () { |
| 226 | + const requestTarget = redactDpopRequestTarget(input.url); |
| 227 | + const urlLength = input.url.length; |
| 156 | 228 | const normalizedUrl = yield* Effect.try({ |
| 157 | 229 | try: () => new URL(input.url), |
| 158 | | − catch: (cause) => dpopError("Could not normalize DPoP proof URL.", cause), |
| 230 | + catch: (cause) => |
| 231 | + new BrowserDpopProofError({ |
| 232 | + operation: "normalize-url", |
| 233 | + method: input.method, |
| 234 | + requestTarget, |
| 235 | + urlLength, |
| 236 | + thumbprint: input.proofKey.thumbprint, |
| 237 | + cause, |
| 238 | + }), |
| 159 | 239 | }); |
| 160 | 240 | normalizedUrl.search = ""; |
| 161 | 241 | normalizedUrl.hash = ""; |
| 162 | 242 | const jti = yield* Crypto.Crypto.pipe( |
| 163 | 243 | Effect.flatMap((crypto) => crypto.randomUUIDv4), |
| 164 | | − Effect.mapError((cause) => dpopError("Could not generate DPoP proof identifier.", cause)), |
| 244 | + Effect.mapError( |
| 245 | + (cause) => |
| 246 | + new BrowserDpopProofError({ |
| 247 | + operation: "generate-id", |
| 248 | + method: input.method, |
| 249 | + requestTarget, |
| 250 | + urlLength, |
| 251 | + thumbprint: input.proofKey.thumbprint, |
| 252 | + cause, |
| 253 | + }), |
| 254 | + ), |
| 165 | 255 | ); |
| 166 | 256 | const proof = yield* Effect.tryPromise({ |
| 167 | 257 | try: () => |
| @@ -178,7 +268,15 @@ export function createBrowserDpopProof(input: { · 10 unmodified lines |
| 178 | 268 | }) |
| 179 | 269 | .setIssuedAt() |
| 180 | 270 | .sign(input.proofKey.privateKey), |
| 181 | | − catch: (cause) => dpopError("Could not sign DPoP proof.", cause), |
| 271 | + catch: (cause) => |
| 272 | + new BrowserDpopProofError({ |
| 273 | + operation: "sign", |
| 274 | + method: input.method, |
| 275 | + requestTarget, |
| 276 | + urlLength, |
| 277 | + thumbprint: input.proofKey.thumbprint, |
| 278 | + cause, |
| 279 | + }), |
| 182 | 280 | }); |
| 183 | 281 | return { proof, thumbprint: input.proofKey.thumbprint }; |
| 184 | 282 | }); |