UNPKG

@j2inn/scram

Version:

TypeScript client SCRAM authentication library

54 lines (53 loc) 1.76 kB
import { BitArray } from 'sjcl'; /** * The authentication data. */ export interface AuthResponse { authToken: string; data: string; hash: string; 'x-csrf-token': string; } /** * Performs the client authentication steps required by the SCRAM specification. */ export declare class ScramAuth { #private; fetch: typeof fetch; /** * Construct a new authenticator. * * @param username The username. * @param password The password. * @param uri Server URI to authenticate with. * @param fetchFunc Alternative fetch function. */ constructor(username: string, password: string, uri: string, fetchFunc: typeof fetch); /** * Performs the client authentication * * @returns Optional `authToken` in an Record<string, string> */ authenticate(): Promise<AuthResponse>; /** * Sends the 'hello' handshake message * @returns Record<string, string> or error */ hello(): Promise<Record<string, string>>; /** * Performs the SCRAM authentication logic. * * @param headers The HTTP handshake response headers from the server */ scram(headers: Record<string, string>): Promise<Record<string, string>>; clientFirstMessage(handshakeToken: string): Promise<{ clientFinal: string; saltedPassword: BitArray; authMessage: string; }>; clientFinalMessage(clientFinal: string, handshakeToken: string): Promise<Record<string, string>>; validateFinalResponse(finalAuthResponse: Record<string, string>, saltedPassword: BitArray, authMessage: string): void; generateClientNonce(): string; xor(key: BitArray, sig: BitArray): BitArray; parseAuthHeaders(header: string): Record<string, string>; }