Skip to content

Commit

Permalink
Merge pull request #1 from bhavin-nakrani/master
Browse files Browse the repository at this point in the history
Initial code
  • Loading branch information
chintandave-simformsolutions committed May 12, 2023
2 parents f89bae1 + e39115e commit 94320c6
Show file tree
Hide file tree
Showing 13 changed files with 563 additions and 2 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/symfony.yml
@@ -0,0 +1,47 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Symfony

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
symfony-tests:
runs-on: ubuntu-latest
steps:
# To automatically get bug fixes and new Php versions for shivammathur/setup-php,
# change this to (see https://github.com/shivammathur/setup-php#bookmark-versioning):
# uses: shivammathur/setup-php@v2
- uses: shivammathur/setup-php@2cb9b829437ee246e9b3cac53555a39208ca6d28
with:
php-version: '8.1'
- uses: actions/checkout@v3
- name: Copy .env.test.local
run: php -r "file_exists('.env.test.local') || copy('.env.test', '.env.test.local');"
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Create Database
run: |
mkdir -p data
touch data/database.sqlite
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DATABASE_URL: sqlite:///%kernel.project_dir%/data/database.sqlite
run: vendor/bin/phpunit
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
/vendor/
composer.lock
.idea
.php-cs-fixer.cache
9 changes: 9 additions & 0 deletions .php-cs-fixer.php
@@ -0,0 +1,9 @@
<?php

$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR12' => true,
'strict_param' => false,
'array_syntax' => ['syntax' => 'short'],
'@Symfony' => true
]);
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 SimformSolutionsPvtLtd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 22 additions & 2 deletions README.md
@@ -1,2 +1,22 @@
# PasswordStrengthBundle
This bundle will help to validate request parameter for password and retype password.
PasswordStrengthBundle
=============================

<p align="center">
<a href="https://github.com/bhavin-nakrani/PasswordStrengthBundle/actions"><img src="https://github.com/bhavin-nakrani/PasswordStrengthBundle/actions/workflows/symfony.yml/badge.svg" alt="Build Status"></a>
<img src="https://img.shields.io/github/downloads/bhavin-nakrani/PasswordStrengthBundle/total" />
</p>

The PasswordStrengthBundle offers a validator that guarantees strong passwords in Symfony6 applications.

This bundle includes a variety of validations, such as minimum length, the inclusion of alphabetic and/or numeric characters, the use of mixed-case characters, and the use of "special" characters (unicode characters in the Control, Mark, Punctuation, Symbol, and Separator classes).

## Documentation

Documentation is available at [docs/index.md](https://github.com/bhavin-nakrani/PasswordStrengthBundle/blob/master/docs/index.md)


## License

This bundle is under the MIT license. See the complete license in the bundle:

- [LICENSE](https://github.com/SimformSolutionsPvtLtd/PasswordStrengthBundle/blob/master/LICENSE)
37 changes: 37 additions & 0 deletions composer.json
@@ -0,0 +1,37 @@
{
"name": "simformsolutions/password-strength-bundle",
"description": "Provides a password strength validator for Symfony 6 application",
"type": "symfony-bundle",
"version": "0.1.0",
"keywords": ["simform", "symfony", "password", "strength", "bundle"],
"homepage": "https://github.com/SimformSolutionsPvtLtd/PasswordStrengthBundle",
"require": {
"php": ">=8.1",
"symfony/validator": ">=6.2",
"symfony/http-kernel": ">=6.2"
},
"license": "MIT",
"autoload": {
"psr-4": {
"Simformsolutions\\PasswordStrengthBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Simformsolutions\\PasswordStrengthBundle\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "SimformSolutionsPvtLtd"
},
{
"name": "Bhavin Nakrani",
"email": "bhavin.n@simformsolutions.com"
}
],
"require-dev": {
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^3.16"
}
}
65 changes: 65 additions & 0 deletions docs/index.md
@@ -0,0 +1,65 @@

## Installation

### Get the bundle

Add the following in your composer.json:

``` json
{
"require": {
"simformsolutions/password-strength-bundle": ">=0.1"
}
}
```

Or,

``` bash
composer require simformsolutions/password-strength-bundle
```

### Initialize the bundle

This bundle is already Symfony 6 compatible and support flex structure. So below class is auto registered. If it is not exist then please add following code.

``` php
// config/bundles.php
return [
// ...
Simformsolutions\PasswordStrengthBundle\SimformsolutionsPasswordStrengthBundle::class => ['all' => true],
];
```

## Usage

If you are using annotations for validations, include the constraints namespace:

``` php
use Simformsolutions\PasswordStrengthBundle\Validator\Constraints as PasswordAssert;
```

and then add the ```PasswordStrength``` validator to the relevant field:

``` php
/**
* #[PasswordAssert\PasswordStrength(['minLength'=> 7, 'requireNumbers'=> true])]
*/
protected $password;
```

Default options include:

- minLength = _5_
- requireLetters = _true_
- requireCaseDiff = _false_
- requireNumbers = _false_
- requireSpecials = _false_

You can customize the validation error messages:

- tooShortMessage = _'Password must be at least {{length}} characters long.'_
- missingLettersMessage = _'Password must include at least one letter.'_
- requireCaseDiffMessage = _'Password must include both upper and lower case letters.'_
- missingNumbersMessage = _'Password must include at least one number.'_
- missingSpecialsMessage = _'Password must include at least one special character.'_
32 changes: 32 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
bootstrap="./tests/bootstrap.php"
colors="true"
verbose="true"
failOnRisky="true"
failOnWarning="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
executionOrder="random"
resolveDependencies="true"
>
<php>
<ini name="error_reporting" value="-1"/>
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
</php>

<testsuites>
<testsuite name="Runner tests">
<directory>./tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
</phpunit>
13 changes: 13 additions & 0 deletions src/SimformsolutionsPasswordStrengthBundle.php
@@ -0,0 +1,13 @@
<?php

namespace Simformsolutions\PasswordStrengthBundle;

use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class SimformsolutionsPasswordStrengthBundle extends AbstractBundle
{
public function getPath(): string
{
return dirname(__DIR__);
}
}
28 changes: 28 additions & 0 deletions src/Validator/Constraints/PasswordStrength.php
@@ -0,0 +1,28 @@
<?php

namespace Simformsolutions\PasswordStrengthBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*
* @Target({"CLASS", "ANNOTATION"})
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class PasswordStrength extends Constraint
{
public string $tooShortMessage = 'Password must be at least {{length}} characters long.';
public string $missingLettersMessage = 'Password must include at least one letter.';
public string $requireCaseDiffMessage = 'Password must include both upper and lower case letters.';
public string $missingNumbersMessage = 'Password must include at least one number.';
public string $missingSpecialsMessage = 'Password must include at least one special character.';

public int $minLength = 5;
public bool $requireLetters = true;
public bool $requireCaseDiff = false;
public bool $requireNumbers = false;
public bool $requireSpecials = false;

public string $charset = 'UTF-8';
}
44 changes: 44 additions & 0 deletions src/Validator/Constraints/PasswordStrengthValidator.php
@@ -0,0 +1,44 @@
<?php

namespace Simformsolutions\PasswordStrengthBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class PasswordStrengthValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint): void
{
if (null === $value) {
$value = '';
}

if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
$length = grapheme_strlen($value);
} else {
$length = mb_strlen($value, $constraint->charset);
}

if ($constraint->minLength > 0 && (mb_strlen($value, $constraint->charset) < $constraint->minLength)) {
$this->context->addViolation($constraint->tooShortMessage, ['{{length}}' => $constraint->minLength]);
}

if ($constraint->requireLetters && !preg_match('/\pL/', $value)) {
$this->context->addViolation($constraint->missingLettersMessage);
}

if ($constraint->requireCaseDiff && !preg_match('/(\p{Ll}+.*\p{Lu})|(\p{Lu}+.*\p{Ll})/', $value)) {
$this->context->addViolation($constraint->requireCaseDiffMessage);
}

if ($constraint->requireNumbers && !preg_match('/\pN/', $value)) {
$this->context->addViolation($constraint->missingNumbersMessage);
}

// Cc: Control; M: Mark; P: Punctuation; S: Symbol; Z: Separator
// Not checked: L: Letter; N: Number; C{fosn}: format, private-use, surrogate, unassigned
if ($constraint->requireSpecials && !preg_match('/[\p{Cc}\pM\pP\pS\pZ]/', $value)) {
$this->context->addViolation($constraint->missingSpecialsMessage);
}
}
}

0 comments on commit 94320c6

Please sign in to comment.