2.0.0 • Published 11 months ago

password-toolkit v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

PasswordToolKit

Github Actions npm version License

The PasswordToolKit is a simple and easy-to-use class that allows generating and evaluating password strength.

Table of Contents

Installation

You can install PasswordToolKit via npm:

npm install password-toolkit

Importing

To use PasswordToolKit in your JavaScript application, first import it:

// Using ES6 imports
import PasswordToolKit from 'password-toolkit';

// Using Node.js `require()`
const PasswordToolKit = require('password-toolkit');

Usage

The PasswordToolKit module is a class that needs to be instantiated with a settings a object with configuration settings, before it can be used. Once instantiated, you can use the create() and evaluate() methods to create and evaluate passwords.

// Import the PasswordToolKit module
const PasswordToolKit = require('password-toolkit');

// Create an instance of PasswordToolKit
const passwordToolKit = new PasswordToolKit();

// It is equivalent
const passwordToolKit = new PasswordToolKit({
  maximum: 30,
  suggestions: [
    'The password must have at least 8 characters.',
    'Add uppercase and lowercase letters to make the password more secure.',
    'Add numbers to make the password more secure.',
    'Add symbols to make the password more secure.',
    'Avoid using repeated characters in the password.',
    'Avoid using common password patterns.',
    'Excellent! The password is secure.',
  ],
  qualities: ['insecure', 'low', 'medium', 'high', 'perfect'],
});

// Or You can translate the messages for the evaluation of the password
const passwordToolKit = new PasswordToolKit({
  maximum: 30,
  suggestions: [
    'La contraseña debe tener al menos 8 caracteres.',
    'Agregue letras mayúsculas y minúsculas para que la contraseña sea más segura.',
    'Agregue números para que la contraseña sea más segura.',
    'Agregue símbolos para que la contraseña sea más segura.',
    'Evitar el uso de caracteres repetidos en la contraseña.',
    'Evitar el uso de patrones de contraseña comunes.',
    '¡Excelente! La contraseña es segura.',
  ],
  qualities: ['Inseguro', 'Bajo', 'Medio', 'Alto', 'Perfecto'],
});

// Generate a password
const options = {
  size: 12,
  numbers: true,
  symbols: true,
  uppercases: true,
  lowercases: true
};

const validation = passwordToolKit.checkOptions(options);

if (validation.ok) {
  const password = passwordToolKit.create(options);
  // Evaluate the security of a password
  const evaluation = passwordToolKit.evaluate(password);

  // Output the results
  console.log('Generated Password:', password);
  console.log('Password Evaluation:', evaluation);
} else {
  throw new Error(validation.reason);
}

Type Definitions

PasswordToolKitSettings

The options for the PasswordToolKit instance.

type: Object

PropertyTypeDescription
maximumnumberThe maximum length allowed for a password.
suggestionsArray.<string>Texts offering suggestions to improve password security.
qualitiesArray.<string>Quality levels for password strength.

OptionsValidation

The result of validating password options.

type: Object

PropertyTypeDescription
okbooleanIndicates whether the validation passed true or failed false.
reasonstringReason for the validation result.

GenerateOptions

Configuration options to generate passwords.

type: Object

PropertyTypeDescription
sizenumberThe desired size of the password.
numbersbooleanIndicates whether numbers are allowed in the password.
symbolsbooleanIndicates whether symbols are allowed in the password.
uppercasesbooleanIndicates whether uppercase letters are allowed in the password.
lowercasesbooleanIndicates whether lowercase letters are allowed in the password.

PasswordEvaluation

The result of validating a password's security.

type: Object

PropertyTypeDescription
levelnumberA number indicating the security level of the password (0-5).
suggestionstringText offering suggestions to improve password security.
qualitystringThe quality level of the password.

API

PasswordToolKit(settings)

Creates an instance of the PasswordToolKit class.

Arguments

NameTypeDescription
settingsobjectThe options for the PasswordToolKit instance.
settings.maximumnumberThe maximum length allowed for a password.
settings.suggestionsArray.<string>Text offering suggestions to improve password security.
settings.qualitiesArray.<string>Array of quality levels for password strength.

Throws

TypeDescription
TypeErrorif the options parameter is not an object.
TypeErrorif the suggestions value is not an array.
TypeErrorif the qualities value is not an array.
TypeErrorif the maximum value is not a number.
TypeErrorif any suggestions value is not a string.
TypeErrorif any qualities value is not a string.
RangeErrorif the suggestions array length is not 7.
RangeErrorif the qualities array length is not 5.

Example

const PasswordToolKit = require('password-toolkit');

const passwordToolKit = new PasswordToolKit();

// or

PasswordToolKit#checkOptions(options)

Checks if the provided options for generating a password are valid.

Arguments

NameTypeDescription
optionsobjectThe options for the password to be generated.
options.sizenumberThe desired size of the password.
options.numbersbooleanIndicates whether numbers are allowed in the password.
options.symbolsbooleanIndicates whether symbols are allowed in the password.
options.uppercasesbooleanIndicates whether uppercase letters are allowed in the password.
options.lowercasesbooleanIndicates whether lowercase letters are allowed in the password.

Returns

An object with the following properties:

PropertyTypeDescription
okbooleanIndicates whether the validation passed true or failed false.
reasonstringReason for the validation result.

Example

const PasswordToolKit = require('password-toolkit');

const passwordToolKit = new PasswordToolKit();
const options = {
  size: 12,
  numbers: true,
  symbols: true,
  uppercases: true,
  lowercases: true
};
const validation = passwordToolKit.checkOptions(options);

PasswordToolKit#generate(options)

The generate() method generates a new password with the provided options. It accepts an options object with the following properties:

Arguments

NameTypeDescription
optionsobjectThe options for the password to be generated.
options.sizenumberThe desired size of the password.
options.numbersbooleanIndicates whether numbers are allowed in the password.
options.symbolsbooleanIndicates whether symbols are allowed in the password.
options.uppercasesbooleanIndicates whether uppercase letters are allowed in the password.
options.lowercasesbooleanIndicates whether lowercase letters are allowed in the password.

Returns

The method returns the generated password as a string, or null if the provided options are invalid.

Example

const options = {
  size: 12,
  numbers: true,
  symbols: true,
  uppercases: true,
  lowercases: true
};

const password = passwordToolKit.create(options);

PasswordToolKit#evaluate(password)

The evaluate() method evaluates the security level of a password. It accepts a password string as input.

Arguments

NameTypeDescription
passwordstringThe password to be evaluated.

Returns

An object with the following properties:

PropertyTypeDescription
levelnumberA number indicating the security level of the password (0-5).
suggestionstringText offering suggestions to improve password security.
qualitystringThe quality level of the password.

Throws

TypeDescription
TypeErrorIf the password value is not a string.

Example

const evaluation = passwordToolKit.evaluate('MySecurePassword123!');

Contributing

If you encounter any bugs or issues with PasswordToolKit, issues and feature requests are welcome. Feel free to check issues page if you want to contribute.

License

Distributed under the MIT License. See LICENSE for more information.