Coder Social home page Coder Social logo

rahulrao0209 / checkpass Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 143 KB

A micro-library for checking and enforcing password constraints

Home Page: https://checkpass-playground.netlify.app/

License: MIT License

TypeScript 75.77% HTML 8.41% SCSS 15.82%
password-check password-strength-checker passwords

checkpass's Introduction

Checkpass

Checkpass is a TypeScript micro-library for enforcing password constraints and improving password strength with a simple API.

Checkpass allows you to specify the required password strength and then verify whether the requirements are being met. For example you could specify the minimum required length of the password, whether numberic characters are required, whether special characters are required, whether capital letters are required and more. You can also pass in a deny list for disallowing certain characters from the password if necessary.

Checkout the Checkpass Playground

Getting Started

Documentation

Installation

npm install checkpass

API and Usage

import checkpass, { Constraints } from "checkpass";

const constraints: Constraints = {
  minLength: 6,
  minCapitalLetters: 2,
  minNumbers: 2,
  maxNumbers: 5,
  minSpecialCharacters: 2,
  maxSpecialCharacters: 6,
  minUniqueCharacters: 4,
  disallowCharacters: ["$", "*"],
};

const password = "TestingR02#Pass!?<";

/* Call checkpass with the arguments */
const result = checkpass(password, constraints);

/* 
  Checkpass returns an object providing details of all the constraints.
  You can use the return value to see if any of the constraints that you 
  had applied return true; which means that the constraint has failed the check.
*/

if (result.minLength.value) return result.minLength.message;
if (result.minCapitalLetters.value) return result.minCapitalLetters.message;

... and so on for all the required checks. The message will be an empty string if the value is false which indicates that the constraint has passed the check.

The above snippet shows a use case where you check the password against the minimum length, minimum capital letters, min and max numeric characters and special characters and disallows the use of characters($ and *).

If all the required checks pass, then the corresponding result object will have all the values for the constraints set to false and the corresponding message string will be empty as shown below.

Return type for the result

const result: CheckErrors = {
  minLength: { value: false, message: "" },
  maxLength: { value: false, message: "" },
  minCapitalLetters: { value: false, message: "" },
  maxCapitalLetters: { value: false, message: "" },
  minNumbers: { value: false, message: "" },
  maxNumbers: { value: false, message: "" },
  minSpecialCharacters: { value: false, message: "" },
  maxSpecialCharacters: { value: false, message: "" },
  minUniqueCharacters: { value: false, message: "" },
  disallowCharacters: { value: false, message: "" },
  disallowSpaces: { value: false, message: "" },
};

Here is a brief list of checks against which checkpass can check your password.

  • Check 1: Check for spaces and disallow any spaces that remain after trimming the string.
  • Check 2: Check the specified length constraints.
  • Check 3: Check the specified capital letter constraints.
  • Check 4: Check the specified numeric character constraints.
  • Check 5: Check the specified special character constraints.
  • Check 6: Check for unique character constraints if specified. (optional)
  • Check 7: Check if any characters are to be disallowed if specified. (optional)

Besides this, Checkpass also checks whether the specifed constraints can be exercised and throws an error in case of erroneous user inputs such as if a negative number is specified for any constraint or if the minimum value constraint is larger than the maximum value for example.

Specifying the constraints

Constraints against which the password needs to be checked can be specified using an object with the following properties.

  minLength: number;
  maxLength?: number; /* optional */
  minCapitalLetters: number;
  maxCapitalLetters?: number; /* optional */
  minNumbers: number;
  maxNumbers?: number; /* optional */
  minSpecialCharacters: number;
  maxSpecialCharacters?: number; /* optional */
  minUniqueCharacters?: number; /* optional */
  disallowCharacters?: string[]; /* optional */

In case of the required properties you can pass 0 if you do not wish to check against that constraint.

For example

const constraints = {
  minLength: 8,
  minSpecialCharacters: 3,
  minCapitalLetters: 0,
  minNumbers: 0,
};

The password being checked against the above constraints object would require minimum 8 characters with 3 special characters. Here we do not need to use any numeric characters or capital letters since the value specified for them is 0. In case no constraints object is passed checkpass will assume a default object wherein all the required properties are set to zero by default and hence the password is not being checked against anything at all!


License

Checkpass is licensed under MIT License.

Thank You! for reading.

checkpass's People

Contributors

rahulrao0209 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

checkpass's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.