Files
tibi-types/types-test/admin-auth.ts
T
apairon 2546d05b25 ✨ types(auth): Admin-Login-, MFA- und WebAuthn-Verträge
Eine Quelle für Server und Admin-UI:
- `AdminLoginResponse`: Union aus `AdminMfaChallenge` (`mfaRequired: true`)
  und `AdminLoginSession` (`token`, `user`, `amr`, `mfaEnrollmentRecommended`).
  Die Diskriminante ist `mfaRequired`, damit Clients sauber narrowen.
- `AdminAuthMethod` (`pwd|totp|recovery|webauthn`) vs. `AdminMfaMethod`
  (ohne `pwd`) — Go schreibt `pwd` immer in `amr`, bietet es aber nie als
  Faktor an.
- `AdminMfaStatus`, `AdminMfaTotpEnrollment`, `AdminMfaConfirmResult`,
  `AdminStepUpResult`.
- WebAuthn: `AdminWebAuthnCreationOptions`,
  `AdminWebAuthnRequestOptions`, `AdminWebAuthnCredentialJSON`,
  `AdminWebAuthnCredentialView`, `AdminAuthenticatorTransport`.

Type-Tests in `types-test/admin-auth.ts`: `Expect<Equal<…>>`-Assertions und
`@ts-expect-error` für die nicht möglichen Felder der Challenge. `npm test`
führt jetzt Schemas **und** `tsc` aus (TypeScript als devDependency).
2026-11-06 19:09:14 +00:00

58 lines
2.0 KiB
TypeScript

// Compile-time contract tests for the admin auth types. `tsc --noEmit` is the
// runner: every Expect<...> must resolve to true, and the @ts-expect-error
// lines must actually fail without the directive.
type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false
type Expect<T extends true> = T
const challenge: AdminMfaChallenge = {
mfaRequired: true,
pendingToken: "pending",
expiresIn: 300,
methods: ["webauthn", "totp", "recovery"],
}
type _ChallengeFlag = Expect<Equal<typeof challenge.mfaRequired, true>>
type _ChallengeMethods = Expect<Equal<typeof challenge.methods, AdminMfaMethod[]>>
type _AuthMethodIncludesPassword = Expect<Equal<AdminMfaMethod extends AdminAuthMethod ? true : false, true>>
declare const response: AdminLoginResponse<{ id: string }>
if (response.mfaRequired) {
const pending: string = response.pendingToken
const methods: AdminMfaMethod[] = response.methods
// @ts-expect-error a challenge carries no session token
response.token
} else {
const token: string = response.token
const user: { id: string } = response.user
const amr: AdminAuthMethod[] | undefined = response.amr
}
const descriptor: AdminWebAuthnDescriptor = { type: "public-key", id: "abc" }
const requestOptions: AdminWebAuthnRequestOptions = {
challenge: "abc",
rpId: "admin.example.com",
allowCredentials: [descriptor],
timeout: 60000,
userVerification: "preferred",
}
const credential: AdminWebAuthnCredentialJSON = {
id: "abc",
rawId: "abc",
type: "public-key",
response: { clientDataJSON: "e30", signature: "sig" },
}
type _AssertionSignature = Expect<Equal<typeof credential.response.signature, string | undefined>>
const view: AdminWebAuthnCredentialView = {
id: "1",
label: "YubiKey",
createdAt: "2026-01-01T00:00:00Z",
backupEligible: false,
backupState: false,
}
// The session branch must stay assignable without a factor.
const session: AdminLoginSession<{ id: string }> = { token: "t", user: { id: "1" } }