import type { Ref } from 'vue'; import type { NuxtError, RequestEvent, ValidateResult } from 'nuxt/server'; import type { AuthenticationResponseJSON, AuthenticatorTransportFuture, GenerateAuthenticationOptionsOpts, GenerateRegistrationOptionsOpts, RegistrationResponseJSON, VerifiedAuthenticationResponse, VerifiedRegistrationResponse } from '@simplewebauthn/server'; export interface WebAuthnCredential { id: string; publicKey: string; counter: number; backedUp: boolean; transports?: AuthenticatorTransportFuture[]; aaguid?: string; [key: string]: unknown; } export interface WebAuthnUser { userName: string; displayName?: string; [key: string]: unknown; } type CredentialsList = NonNullable; type WebAuthnEventHandlerBase> = { storeChallenge: (event: RequestEvent, challenge: string, attemptId: string) => void | Promise; getChallenge: (event: RequestEvent, attemptId: string) => string | Promise; onSuccess: (event: RequestEvent, data: T) => unknown; onError?: (event: RequestEvent, error: NuxtError) => unknown; } | { storeChallenge?: undefined; getChallenge?: undefined; onSuccess: (event: RequestEvent, data: T) => unknown; onError?: (event: RequestEvent, error: NuxtError) => unknown; }; export type RegistrationBody = { user: T; verify: false; } | { user: T; verify: true; attemptId: string; response: RegistrationResponseJSON; }; export type ValidateUserFunction = (userBody: WebAuthnUser, event: RequestEvent) => ValidateResult | Promise>; export type WebAuthnRegisterEventHandlerOptions = WebAuthnEventHandlerBase<{ user: T; credential: WebAuthnCredential; registrationInfo: Exclude; }> & { getOptions?: (event: RequestEvent, body: RegistrationBody) => Partial | Promise>; validateUser?: ValidateUserFunction; excludeCredentials?: (event: RequestEvent, userName: string) => CredentialsList | Promise; }; export type AuthenticationBody = { verify: false; userName?: string; } | { verify: true; attemptId: string; userName?: string; response: AuthenticationResponseJSON; }; export type WebAuthnAuthenticateEventHandlerOptions = WebAuthnEventHandlerBase<{ credential: T; authenticationInfo: Exclude; }> & { getOptions?: (event: RequestEvent, body: AuthenticationBody) => Partial | Promise>; getCredential: (event: RequestEvent, credentialID: string) => T | Promise; allowCredentials?: (event: RequestEvent, userName: string) => CredentialsList | Promise; }; export interface WebAuthnComposable { /** * Vue ref (boolean) that checks if the webauthn API is available */ isSupported: Ref; /** * Helper function that checks if the browser supports "Conditional UI" for webauthn * @see https://github.com/w3c/webauthn/wiki/Explainer:-WebAuthn-Conditional-UI */ isAutofillSupported: () => Promise; /** * Helper function that returns if platform specific authenticators are available (Touch ID, Face ID, Windows Hello, etc.) * You can use this information to prioritize these authenticators over others. */ isPlatformAvailable: () => Promise; /** * Register a credential * @param user - The user data to register * @param user.userName - The user name to register, can be an email address, a username, or any other form of unique ID chosen by the user * @param user.displayName - The display name to register, used for convenience and personalization and should not be relied upon as a secure identifier for authentication processes * @returns true if the registration was successful */ register: (user: T) => Promise; /** * Authenticate a credential * @param userName - The user name to authenticate, can be an email address, a username, or any other form of unique ID chosen by the user * @returns true if the authentication was successful */ authenticate: (userName?: string) => Promise; } export {};