Skip to content

Instantly share code, notes, and snippets.

@chasecmiller
Last active June 7, 2022 05:44
Show Gist options
  • Save chasecmiller/d0270b83a10414998a7866e90570c244 to your computer and use it in GitHub Desktop.
Save chasecmiller/d0270b83a10414998a7866e90570c244 to your computer and use it in GitHub Desktop.
Laravel Rule encouraging Stanford's Password Policy
<?php
namespace App\Rules;
use Illuminate\Validation\Rules\Password as Rule;
/**
* A password rule for Laravel based on Stanford's best practices.
* https://uit.stanford.edu/service/accounts/passwords
*
* 8-11: mixed case letters, numbers, & symbols
* 12-15: mixed case letters & numbers
* 16-19: mixed case letters
* 20+: no restrictions
*
* It must not be equal to your current password, previous passwords, SUNet ID, or password reset answer
* It must not be a single word that appears in the dictionary (English or non-English)
* It must be composed only of characters in the Roman alphabet, numbers, or symbols on the US keyboard. Examples include characters such as # $ % ! @.
*/
class Password extends Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct(int $min = 8)
{
parent::__construct($min);
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$length = strlen($value);
/**
* No restrictions.
*/
$this->mixedCase = false;
$this->letters = false;
$this->numbers = false;
$this->symbols = false;
$this->uncompromised = false;
if ($length < 20) {
$this->mixedCase = true;
}
if ($length < 16) {
$this->numbers = true;
}
if ($length < 11) {
$this->symbols = true;
}
return parent::passes($attribute, $value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment