@scirexs/srp6a
Version:
SRP-6a (Secure Remote Password) implementation in TypeScript for browser and server.
134 lines • 5.5 kB
TypeScript
export { addRandomDelay, authenticate, createDummyHello, createServerHello, extractClientHello, extractLoginEvidence, extractSignupCredentials, };
import { SRPConfig } from "../shared/crypto.js";
import { addRandomDelay } from "../shared/functions.js";
import type { AuthResult, ClientHello, CryptoKeyPair, CryptoSource, 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
* @param verifier - Password verifier from user registration
* @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: CryptoSource, verifier: CryptoSource, 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 = 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
* @param verifier - Password verifier from user registration
* @param pair - Server's key pair from hello phase
* @param client - Client's public key from hello phase
* @param evidence - Client evidence to verify
* @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: CryptoSource, verifier: CryptoSource, pair: KeyPair | CryptoKeyPair, client: CryptoSource, evidence: CryptoSource, 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