Code Monkey home page Code Monkey logo

passwordpolicy's Introduction

PasswordPolicy

This class provides a password validation against the standard password requirements rules.

Rules

  • minLength()
  • maxLength()
  • minDigit()
  • minSpecialChar()
  • minUpperCase()
  • minLowerCase()
  • occurrences()
  • sequential()
  • cantContain()
  • blackList()
  • notIn()

Configuration

Let's first create an instance of the class:

include 'PasswordPolicy.php';

$policy = new PasswordPolicy();

Now set your own rules:

$policyBuilder = $policy->minDigit(1)
                        ->minLength(8)
                        ->maxLength(30)
                        ->specialCharacter(1)
                        ->upperCase(2)
                        ->lowerCase(2)
                        ->occurrences(2)
                        ->sequential(3);

Advanced

You can use constraints functions, which are:

  • cantContain() // Checks if password has any part from this array
  • blackList() // Checks if password is equal to any string in this array
  • notIn() // Checks if password is equal to any old passwords in this array
$cantContain = [
  'f*ck',
];
$blackListed = [
  'abcd',
  'asd123'
];
$oldPasswords  = [ // Can be passed in hashed form (but you will have to pass the new password hash in `notIn($needle, $hashedPassword)`)
  'mohamed',
  'riyad',
];

$policyBuilder = $policy->cantContain($cantContain)
                        ->blackList($blackListed)
                        ->notIn($oldPasswords);

How to use

You simply need to call the checkPassword() method, which returns either true or false;

If false, you may want to get errors and print them to the user from the getErrors() method:

if ($policyBuilder->checkPassword('Zy1234f*ck')) {
  echo 'Password is OK!';
}

else {
  echo 'Errors found:';
  echo '<br />- ' . implode('<br />- ', $policyBuilder->getErrors());
}

^ Will print:

Errors found:

  • Password must contain at least 2 lowercase characters
  • Password can not contain 3 sequentials letters or numbers
  • Password can not contain f*ck

Contribute

  • Fork the repo
  • Create your branch
  • Commit your changes
  • Create a pull request

Discussion

For any queries contact me at: [email protected]

passwordpolicy's People

Contributors

ryadpasha avatar

Watchers

 avatar  avatar

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.