mirror of
https://github.com/hanzoai/dev-desktop.git
synced 2026-11-20 01:07:23 +00:00
140 lines
4.9 KiB
TypeScript
140 lines
4.9 KiB
TypeScript
import {
|
|
type DpopVerificationFailureCode as DpopVerificationFailureCodeType,
|
|
verifyDpopProof,
|
|
} from "@hanzodev/shared/dpop";
|
|
import type { DpopFailureReason } from "@hanzodev/contracts";
|
|
import * as Digest from "effect/crypto/Digest";
|
|
import * as DateTime from "effect/DateTime";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Base64Url from "effect/encoding/Base64Url";
|
|
import * as Option from "effect/Option";
|
|
import * as HttpServerRequest from "effect/http/HttpServerRequest";
|
|
|
|
import {
|
|
ServerAuthDpopReplayKeyCalculationError,
|
|
ServerAuthDpopReplayStateRecordError,
|
|
ServerAuthInvalidCredentialError,
|
|
type ServerAuthInternalError,
|
|
} from "./EnvironmentAuth.ts";
|
|
import * as ServerSecretStore from "./ServerSecretStore.ts";
|
|
import { requestUrl } from "../requestUrl.ts";
|
|
|
|
export const DPOP_REPLAY_MARKER_PREFIX = "dpop-proof-";
|
|
|
|
const mapDpopFailureReason = (code: DpopVerificationFailureCodeType): DpopFailureReason => {
|
|
switch (code) {
|
|
case "time_window":
|
|
return "time_window";
|
|
case "key_mismatch":
|
|
return "key_mismatch";
|
|
case "method_mismatch":
|
|
case "url_mismatch":
|
|
return "request_mismatch";
|
|
case "access_token_hash_mismatch":
|
|
return "token_mismatch";
|
|
case "missing_proof":
|
|
case "malformed_proof":
|
|
case "invalid_signature":
|
|
case "invalid_proof":
|
|
return "invalid_proof";
|
|
}
|
|
};
|
|
|
|
export const mapDpopReplayStoreError = (
|
|
error: ServerSecretStore.SecretStoreError,
|
|
): ServerAuthInvalidCredentialError | ServerAuthInternalError =>
|
|
ServerSecretStore.isSecretAlreadyExistsError(error)
|
|
? new ServerAuthInvalidCredentialError({
|
|
diagnostic: "DPoP proof replayed.",
|
|
dpopFailureReason: "replay",
|
|
cause: error,
|
|
})
|
|
: new ServerAuthDpopReplayStateRecordError({
|
|
cause: error,
|
|
});
|
|
|
|
/** Checks a proof without recording it; `consume` records its jti so it cannot be replayed. */
|
|
export const checkRequestDpopProof = Effect.fn("dpop.checkRequestDpopProof")(function* (input: {
|
|
readonly request: HttpServerRequest.HttpServerRequest;
|
|
/** Overrides the `DPoP` header for requests that cannot carry one, such as a WebSocket upgrade. */
|
|
readonly proof?: string;
|
|
readonly expectedThumbprint?: string;
|
|
readonly expectedAccessToken?: string;
|
|
}) {
|
|
const proof = input.proof ?? input.request.headers.dpop;
|
|
const url = requestUrl(input.request);
|
|
if (Option.isNone(url)) {
|
|
return yield* new ServerAuthInvalidCredentialError({
|
|
diagnostic: "Invalid DPoP request URL.",
|
|
});
|
|
}
|
|
const now = yield* DateTime.now;
|
|
const result = verifyDpopProof({
|
|
proof,
|
|
method: input.request.method,
|
|
url: url.value.href,
|
|
nowEpochSeconds: Math.floor(now.epochMilliseconds / 1_000),
|
|
...(input.expectedThumbprint ? { expectedThumbprint: input.expectedThumbprint } : {}),
|
|
...(input.expectedAccessToken ? { expectedAccessToken: input.expectedAccessToken } : {}),
|
|
});
|
|
if (!result.ok) {
|
|
yield* Effect.annotateCurrentSpan({
|
|
"environment.dpop.failure_code": result.code,
|
|
});
|
|
return yield* new ServerAuthInvalidCredentialError({
|
|
diagnostic: result.reason,
|
|
dpopFailureReason: mapDpopFailureReason(result.code),
|
|
});
|
|
}
|
|
const secretStore = yield* ServerSecretStore.ServerSecretStore;
|
|
const digest = yield* Digest.Digest;
|
|
const consume: Effect.Effect<void, ServerAuthInvalidCredentialError | ServerAuthInternalError> =
|
|
Effect.gen(function* () {
|
|
const replayKey = yield* digest
|
|
.digest(Digest.sha256, new TextEncoder().encode(`${result.thumbprint}:${result.jti}`))
|
|
.pipe(
|
|
Effect.map(Base64Url.encode),
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new ServerAuthDpopReplayKeyCalculationError({
|
|
cause,
|
|
}),
|
|
),
|
|
);
|
|
yield* secretStore
|
|
.create(
|
|
`${DPOP_REPLAY_MARKER_PREFIX}${replayKey}`,
|
|
new TextEncoder().encode(
|
|
[
|
|
`thumbprint=${result.thumbprint}`,
|
|
`jti=${result.jti}`,
|
|
`iat=${result.iat}`,
|
|
`consumedAt=${DateTime.formatIso(now)}`,
|
|
].join("\n"),
|
|
),
|
|
)
|
|
.pipe(
|
|
Effect.catchIf(ServerSecretStore.isSecretStoreError, (error) =>
|
|
Effect.gen(function* () {
|
|
const mapped = mapDpopReplayStoreError(error);
|
|
if (mapped._tag === "ServerAuthInvalidCredentialError") {
|
|
yield* Effect.annotateCurrentSpan({
|
|
"environment.dpop.failure_code": mapped.dpopFailureReason,
|
|
});
|
|
}
|
|
return yield* mapped;
|
|
}),
|
|
),
|
|
);
|
|
});
|
|
return { thumbprint: result.thumbprint, consume };
|
|
});
|
|
|
|
export const verifyRequestDpopProof = Effect.fn("dpop.verifyRequestDpopProof")(function* (
|
|
input: Parameters<typeof checkRequestDpopProof>[0],
|
|
) {
|
|
const proof = yield* checkRequestDpopProof(input);
|
|
yield* proof.consume;
|
|
return proof.thumbprint;
|
|
});
|