Skip to content

Commit

Permalink
Merge pull request #8 from oooholdings/dev
Browse files Browse the repository at this point in the history
Implement customCharlist method and example charlists
  • Loading branch information
btrsco authored Apr 13, 2022
2 parents 3e15406 + 301c5a7 commit fbd4954
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 42 deletions.
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Latest Version on Packagist][ico-version]][link-packagist]
[![Total Downloads][ico-downloads]][link-downloads]
[![License][ico-license]][link-license]
[![Laravel Nova v4][ico-nova-v4]][link-nova-v4]

---

Expand All @@ -14,6 +15,7 @@
3. [Hide Toolbar Elements](#hide-toolbar-elements)
4. [Customize Password Generation](#customize-password-generation)
5. [Operation Base Filling](#operation-base-filling)
6. [Other Methods](#other-methods)
4. [Credits](#credits)
5. [License](#license)

Expand All @@ -26,7 +28,7 @@ updating resources.

![Password Generator Field Preview](preview.jpg)

Currently, you can show and hide the password. Include or exclude lowercase, uppercase, numbers and symbols from the
Currently, you can show and hide the password. Include or exclude uppercase, lowercase, numbers and symbols from the
generated passwords. Change the password length, minimum and maximum length or total length that respects the length of
any prefix or suffix used on in the password. Easily copy it to your clipboard and regenerate passwords when you update
the options or manually via the button.
Expand Down Expand Up @@ -173,10 +175,10 @@ public function fields()
return [

PasswordGenerator::make( 'Password' )
// Included by default, you can exclude all lowercase characters (e.g. abc)
->excludeLowercase( bool $exclude = true )
// Included by default, you can exclude all uppercase characters (e.g. ABC)
->excludeUppercase( bool $exclude = true )
// Included by default, you can exclude all lowercase characters (e.g. abc)
->excludeLowercase( bool $exclude = true )
// Included by default, you can exclude all numbers characters (e.g. 123)
->excludeNumbers( bool $exclude = true )
// Included by default, you can exclude all symbols characters (e.g. $@!)
Expand All @@ -188,9 +190,13 @@ public function fields()
// (e.g. "{ } [ ] ( ) / \ ' " ` ~ , ; : . < >")
->excludeAmbiguous( bool $exclude = true ),
// One method to exclude multiple options, accepts the following:
// 'lowercase' or 'lower', 'uppercase' or 'upper', 'numbers' or 'digits',
// 'uppercase' or 'upper', 'lowercase' or 'lower', 'numbers' or 'digits',
// 'symbols' or 'special', 'similar', 'ambiguous'
->excludeRules( array $excludeRules ),
// Customize the character list for the generated password, just pass
// a string to this method, check PasswordGenerator class for more charlists
// using this method auto-hides the option element from the toolbar
->customCharlist( string $charlist = PasswordGenerator::BASE16_MOD )

];
}
Expand Down Expand Up @@ -265,6 +271,8 @@ MIT - Please see the [license file](license.md) for more information.

[ico-license]: https://img.shields.io/packagist/l/outofoffice/password-generator?style=flat-square

[ico-nova-v4]: https://img.shields.io/badge/nova-v4-333?logo=laravel-nova&style=flat-square

[link-packagist]: https://packagist.org/packages/outofoffice/password-generator

[link-downloads]: https://packagist.org/packages/outofoffice/password-generator
Expand All @@ -275,6 +283,8 @@ MIT - Please see the [license file](license.md) for more information.

[link-license]: https://github.com/oooholdings/nova-password-generator/blob/master/license.md

[link-nova-v4]: https://nova.laravel.com/docs/4.0/

[link-contributors]: https://github.com/oooholdings/nova-password-generator/contributors

[link-beyondcode-banners]: https://banners.beyondco.de/
84 changes: 47 additions & 37 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,28 @@
class="pg-option pg-pill-wrapper"
:class="classes.disabled">
<ul class="pg-pill-options">
<li class="pg-pill-option"
:class="!excluded.uppercase ? classes.enabledPill : classes.disabledPill"
@click="toggleUppercase"
v-tooltip="!excluded.uppercase ? tooltips.uppercase.enabled : tooltips.uppercase.disabled"
:style="[ borderRadiusRightStyles( !excluded.uppercase && !excluded.lowercase ) ]">
<svg xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
stroke-width="2.5"
stroke-linecap="round"
stroke-linejoin="round">
<path d="m5 20 7-16 7 16" />
<path d="m16.832703 15.669922h-9.664673" />
</svg>
</li>
<li class="pg-pill-option"
:class="!excluded.lowercase ? classes.enabledPill : classes.disabledPill"
@click="toggleLowercase"
v-tooltip="!excluded.lowercase ? tooltips.lowercase.enabled : tooltips.lowercase.disabled"
:style="[ borderRadiusRightStyles( !excluded.lowercase && !excluded.uppercase ) ]">
:style="[ borderRadiusLeftStyles( !excluded.uppercase && !excluded.lowercase ), borderRadiusRightStyles( !excluded.lowercase && !excluded.numbers ) ]">
<svg xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
fill="none"
Expand All @@ -68,28 +85,11 @@
stroke-linejoin="round" />
</svg>
</li>
<li class="pg-pill-option"
:class="!excluded.uppercase ? classes.enabledPill : classes.disabledPill"
@click="toggleUppercase"
v-tooltip="!excluded.uppercase ? tooltips.uppercase.enabled : tooltips.uppercase.disabled"
:style="[ borderRadiusLeftStyles( !excluded.lowercase && !excluded.uppercase ), borderRadiusRightStyles( !excluded.uppercase && !excluded.numbers ) ]">
<svg xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
stroke-width="2.5"
stroke-linecap="round"
stroke-linejoin="round">
<path d="m5 20 7-16 7 16" />
<path d="m16.832703 15.669922h-9.664673" />
</svg>
</li>
<li class="pg-pill-option"
:class="!excluded.numbers ? classes.enabledPill : classes.disabledPill"
@click="toggleNumbers"
v-tooltip="!excluded.numbers ? tooltips.numbers.enabled : tooltips.numbers.disabled"
:style="[ borderRadiusLeftStyles( !excluded.uppercase && !excluded.numbers ), borderRadiusRightStyles( !excluded.numbers && !excluded.symbols ) ]">
:style="[ borderRadiusLeftStyles( !excluded.lowercase && !excluded.numbers ), borderRadiusRightStyles( !excluded.numbers && !excluded.symbols ) ]">
<svg xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
fill="none"
Expand Down Expand Up @@ -224,18 +224,19 @@ export default {
regenerateOnToggle: this.field.regenerateOnToggle ?? true,
passwordPrefix: this.field.passwordPrefix ?? '',
passwordSuffix: this.field.passwordSuffix ?? '',
customCharlist: this.field.customCharlist ?? null,
excluded: {
lowercase: this.field.excludeLowercase ?? false,
uppercase: this.field.excludeUppercase ?? false,
lowercase: this.field.excludeLowercase ?? false,
numbers: this.field.excludeNumbers ?? false,
symbols: this.field.excludeSymbols ?? false,
similar: this.field.excludedSimilar ?? true,
ambiguous: this.field.excludedAmbiguous ?? true,
},
tooltips: {
showPassword: { enabled: 'Hide Password', disabled: 'Show Password' },
lowercase: { enabled: 'Exclude Lowercase', disabled: 'Include Lowercase' },
uppercase: { enabled: 'Exclude Uppercase', disabled: 'Include Uppercase' },
lowercase: { enabled: 'Exclude Lowercase', disabled: 'Include Lowercase' },
numbers: { enabled: 'Exclude Numbers', disabled: 'Include Numbers' },
symbols: { enabled: 'Exclude Symbols', disabled: 'Include Symbols' },
decreaseLength: 'Decrease Length',
Expand Down Expand Up @@ -289,10 +290,10 @@ export default {
],
},
charlists: {
lowercase: 'abcdefghijklmnopqrstuvwxyz',
lowercaseNoSimilar: 'abcdefghjkmnpqrstuvwxyz',
uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
uppercaseNoSimilar: 'ABCDEFGHJKMNPQRSTUVWXYZ',
lowercase: 'abcdefghijklmnopqrstuvwxyz',
lowercaseNoSimilar: 'abcdefghjkmnpqrstuvwxyz',
numbers: '1234567890',
numbersNoSimilar: '23456789',
symbols: '`~\'"!@#$%^&*()_+-=[]{};:,.<>\\/|?',
Expand All @@ -314,6 +315,10 @@ export default {
this.status = 'create';
if ( this.fillOnCreate ) this.regeneratePassword();
}
if ( this.customCharlist !== null ) {
this.hideOptionsToggles = true;
}
},
computed: {
Expand Down Expand Up @@ -376,17 +381,22 @@ export default {
let charlist = '';
if ( this.validateToggles() ) {
if ( !this.excluded.lowercase && !this.excluded.similar ) charlist += this.charlists.lowercase;
if ( !this.excluded.lowercase && this.excluded.similar ) charlist += this.charlists.lowercaseNoSimilar;
if ( !this.excluded.uppercase && !this.excluded.similar ) charlist += this.charlists.uppercase;
if ( !this.excluded.uppercase && this.excluded.similar ) charlist += this.charlists.uppercaseNoSimilar;
if ( this.customCharlist !== null ) {
charlist = this.customCharlist;
} else {
if ( !this.excluded.uppercase && !this.excluded.similar ) charlist += this.charlists.uppercase;
if ( !this.excluded.uppercase && this.excluded.similar ) charlist += this.charlists.uppercaseNoSimilar;
if ( !this.excluded.numbers && !this.excluded.similar ) charlist += this.charlists.numbers;
if ( !this.excluded.numbers && this.excluded.similar ) charlist += this.charlists.numbersNoSimilar;
if ( !this.excluded.lowercase && !this.excluded.similar ) charlist += this.charlists.lowercase;
if ( !this.excluded.lowercase && this.excluded.similar ) charlist += this.charlists.lowercaseNoSimilar;
if ( !this.excluded.symbols && !this.excluded.ambiguous ) charlist += this.charlists.symbols;
if ( !this.excluded.symbols && this.excluded.ambiguous ) charlist += this.charlists.symbolsNoAmbiguous;
if ( !this.excluded.numbers && !this.excluded.similar ) charlist += this.charlists.numbers;
if ( !this.excluded.numbers && this.excluded.similar ) charlist += this.charlists.numbersNoSimilar;
if ( !this.excluded.symbols && !this.excluded.ambiguous ) charlist += this.charlists.symbols;
if ( !this.excluded.symbols && this.excluded.ambiguous ) charlist += this.charlists.symbolsNoAmbiguous;
}
let adjustedLength = this.passwordLength;
Expand Down Expand Up @@ -462,8 +472,8 @@ export default {
},
validateToggles() {
if ( this.excluded.lowercase
&& this.excluded.uppercase
if ( this.excluded.uppercase
&& this.excluded.lowercase
&& this.excluded.numbers
&& this.excluded.symbols ) {
Nova.error( 'Can\'t generate a password if no options are enabled.' );
Expand All @@ -477,13 +487,13 @@ export default {
this.showPassword = !this.showPassword;
},
toggleLowercase() {
this.excluded.lowercase = !this.excluded.lowercase;
toggleUppercase() {
this.excluded.uppercase = !this.excluded.uppercase;
if ( this.regenerateOnToggle ) this.regeneratePassword();
},
toggleUppercase() {
this.excluded.uppercase = !this.excluded.uppercase;
toggleLowercase() {
this.excluded.lowercase = !this.excluded.lowercase;
if ( this.regenerateOnToggle ) this.regeneratePassword();
},
Expand Down
35 changes: 35 additions & 0 deletions src/PasswordGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@

class PasswordGenerator extends Field
{
/**
* Character Lists For Your Convenience
*/
public const UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
public const UPPERCASE_NS = 'ABCDEFGHJKMNPQRSTUVWXYZ';
public const LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';
public const LOWERCASE_NS = 'abcdefghjkmnpqrstuvwxyz';
public const BASE10 = '1234567890';
public const BASE16 = 'ABCDEF' . self::BASE10;
public const BASE16_MOD = 'ABCDEFabcdef' . self::BASE10;
public const BASE32 = self::UPPERCASE . '234567';
public const BASE36 = self::UPPERCASE . self::BASE10;
public const BASE45_NS = self::UPPERCASE . self::BASE10 . '_$%*+-./:';
public const BASE58 = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz123456789';
public const BASE62 = self::UPPERCASE . self::LOWERCASE . self::BASE10;
public const BASE64 = self::BASE62 . '+/';
public const NUMBERS = self::BASE10;
public const NUMBERS_NS = '23456789';
public const SYMBOLS = '`~\'"!@#$%^&*()_+-=[]{};:,.<>\/|?';
public const SYMBOLS_NA = '!@#$%^&*_+-=?';

/**
* The field's component.
*
Expand Down Expand Up @@ -303,6 +324,20 @@ public function excludeRules( array $excludeRules ): PasswordGenerator
] );
}

/**
* Ignore built-in charlist for a custom one.
* Also hides password options element on toolbar.
*
* @param string $charlist
* @return PasswordGenerator
*/
public function customCharlist( string $charlist = PasswordGenerator::BASE16_MOD ): PasswordGenerator
{
return $this->withMeta( [
'customCharlist' => $charlist,
] );
}

/**
* Fill password field with generated password when creating resource.
*
Expand Down

0 comments on commit fbd4954

Please sign in to comment.