UNPKG

@scirexs/srp6a

Version:

SRP-6a (Secure Remote Password) implementation in TypeScript for browser and server.

134 lines 5.71 kB
export { addRandomDelay, authenticate, createDummyHello, createServerHello, extractClientHello, extractLoginEvidence, extractSignupCredentials, }; import { CryptoNumber, SRPConfig } from "../shared/crypto.js"; import { addRandomDelay } from "../shared/functions.js"; import type { AuthResult, ClientHello, CryptoKeyPair, KeyPair, LoginEvidence, ServerHello, SignupCredentials } from "../shared/types.js"; /** * Creates server hello response for SRP6a authentication. * Generates server key pair and returns hello message along with the key pair for session use. * * @param salt - Salt value from user registration (string or CryptoNumber) * @param verifier - Password verifier from user registration (string or CryptoNumber) * @param config - SRP configuration object * @returns Promise that resolves to tuple containing [ServerHello message, KeyPair for the session] * * @example * ```ts * const userRecord = getUserFromDatabase(username); * const [hello, keyPair] = await createServerHello(userRecord.salt, userRecord.verifier, config); * // Send hello to client, store keyPair for authentication * ``` */ declare function createServerHello(salt: string | CryptoNumber, verifier: string | CryptoNumber, config: SRPConfig): Promise<[ServerHello, KeyPair]>; /** * Creates a dummy server hello response. * Generates a dummy salt and server key to prevent user enumeration attacks. * * @param config - SRP configuration object * @returns Dummy ServerHello message * * @example * ```ts * const userRecord = getUserFromDatabase(username); * if (!userRecord) { * const dummy = await createDummyHello(config); * addRandomDelay(); * // Send dummy response to client * } * ``` */ declare function createDummyHello(config: SRPConfig): ServerHello; /** * Authenticates client evidence and generates server evidence for mutual authentication. * Verifies client's knowledge of password and computes server evidence if authentication succeeds. * * @param username - The username attempting to authenticate * @param salt - Salt value from user registration (string or CryptoNumber) * @param verifier - Password verifier from user registration (string or CryptoNumber) * @param pair - Server's key pair from hello phase * @param client - Client's public key from hello phase (string or CryptoNumber) * @param evidence - Client evidence to verify (string or CryptoNumber) * @param config - SRP configuration object * @returns Promise that resolves to AuthResult with result status and server evidence * @throws Error if client's public key is invalid * * @example * ```ts * const result = await authenticate( * username, * userRecord.salt, * userRecord.verifier, * serverKeyPair, * clientPublicKey, * clientEvidence, * config * ); * * if (result.success) { * // Authentication successful, send server evidence * return { success: true, evidence: result.evidence }; * } else { * // Authentication failed * return { success: false, evidence: "" }; * } * ``` */ declare function authenticate(username: string, salt: string | CryptoNumber, verifier: string | CryptoNumber, pair: KeyPair | CryptoKeyPair, client: string | CryptoNumber, evidence: string | CryptoNumber, config: SRPConfig): Promise<AuthResult>; /** * Extracts client signup information from HTTP request, if client used this library. * Parses the request body and validates that it contains required username and credential properties. * * @param request - HTTP request containing signup credential data * @returns Promise that resolves to SignupCredentials object containing username and credential data * @throws Error if request is not POST, not JSON, or missing required properties * * @example * ```ts * // In your HTTP handler * const { username, salt, verifier } = await extractSignupCredentials(request); * storeDatabase(username, salt, verifier); * ``` */ declare function extractSignupCredentials(request: Request): Promise<SignupCredentials>; /** * Extracts client hello information from HTTP request, if client used this library. * Parses the request body and validates that it contains required username and client properties. * * @param request - HTTP request containing client hello data * @returns Promise that resolves to ClientHello object containing username and client public key * @throws Error if request is not POST, not JSON, or missing required properties * * @example * ```ts * // In your HTTP handler * const { username, client } = await extractClientHello(request); * const userRecord = getUserFromDatabase(username); * const [hello, keyPair] = await createServerHello(userRecord.salt, userRecord.verifier, config); * ``` */ declare function extractClientHello(request: Request): Promise<ClientHello>; /** * Extracts login evidence from HTTP request, if client used this library. * Parses the request body and validates that it contains required username and evidence properties. * * @param request - HTTP request containing login evidence data * @returns Promise that resolves to LoginEvidence object containing username and client evidence * @throws Error if request is not POST, not JSON, or missing required properties * * @example * ```ts * // In your HTTP handler * const { username, evidence } = await extractLoginEvidence(request); * const userRecord = getUserFromDatabase(username); * const result = await authenticate( * username, * userRecord.salt, * userRecord.verifier, * storedKeyPair, * storedClientPublicKey, * evidence, * config * ); * ``` */ declare function extractLoginEvidence(request: Request): Promise<LoginEvidence>; //# sourceMappingURL=main.d.ts.map