UNPKG

jwt-token-pair-generator

Version:

A secure RSA token pair generator CLI and library for generating and storing RSA keys for JWT

61 lines (60 loc) 2.01 kB
import { SecureKeyGeneratorConfig } from "./interface"; /** * SecureKeyGenerator generates RSA key pairs for tokens and stores them securely. */ export declare class SecureKeyGenerator { private readonly SECURE_FILE_PERMISSIONS; private readonly KEY_DIRECTORY?; private readonly keyPath?; private readonly envFileName; private readonly modulusLength; private readonly logger; constructor(config?: SecureKeyGeneratorConfig); /** * Generates an RSA key pair. * @param keyType - Type of key ("access" or "refresh"). * @returns The generated KeyPair. */ private generateRSAKeyPair; /** * Generates a secure random passphrase. * @returns A hex string representing the passphrase. */ private generateSecurePassphrase; /** * Validates the generated key pair by signing and verifying a test message. * @param publicKey - The public key. * @param privateKey - The private key. */ private validateKeyPair; /** * Creates the directory for storing keys with secure permissions. */ private createSecureKeyDirectory; /** * Loads existing environment variables from the configured env file. * @returns An object containing key/value pairs from the env file. */ private loadExistingEnv; /** * Writes a file with the specified permissions, with fallback on failure. * @param filePath - The path to the file. * @param content - The file content. * @param mode - The file permission mode. */ private writeFileWithFallback; /** * Saves the generated token keys to disk and updates the environment file. * @param tokenKeys - The generated token key pairs. */ private saveKeys; /** * Prints security instructions to the console. */ private printSecurityInstructions; /** * Public method to generate RSA token key pairs. */ generate(): Promise<void>; } export default SecureKeyGenerator;