Skip to content

Commit 585d221

Browse files
committed
Refactor Password into a instantiable Hasher class, service, and Password facade (#827)
1 parent 3b0df57 commit 585d221

13 files changed

Lines changed: 125 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Change Log
22

33
## v4.1.15-alpha
4+
- Refactor `Password` into a instantiable `Hasher` class, service, and `Password` facade (#827)
45
- Change default hash cost back to 10 and fix legacy hash detection issue
56

67
## v4.1.14-alpha

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Srinivas's a web applications architect, with a passion for open source technolo
100100
- @splitt3r - German
101101
- @X-Anonymous-Y - German
102102
- Dmitriy (@rendername) - Russian
103+
- Amin Akbari (@aminakbari) - Farsi
103104

104105
## Contributing
105106

app/defines.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace UserFrosting;
44

55
// Some standard defines
6-
define('UserFrosting\VERSION', '4.1.14-alpha');
6+
define('UserFrosting\VERSION', '4.1.15-alpha');
77
define('UserFrosting\DS', '/');
88
define('UserFrosting\PHP_MIN_VERSION', '5.6');
99
define('UserFrosting\DEBUG_CONFIG', false);

app/sprinkles/account/src/Authenticate/Authenticator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use UserFrosting\Sprinkle\Account\Authenticate\Exception\AuthExpiredException;
2121
use UserFrosting\Sprinkle\Account\Authenticate\Exception\InvalidCredentialsException;
2222
use UserFrosting\Sprinkle\Account\Database\Models\User;
23-
use UserFrosting\Sprinkle\Account\Util\Password;
23+
use UserFrosting\Sprinkle\Account\Facades\Password;
2424
use UserFrosting\Sprinkle\Core\Util\ClassMapper;
2525

2626
/**

app/sprinkles/account/src/Util/Password.php renamed to app/sprinkles/account/src/Authenticate/Hasher.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
* @link https://github.com/userfrosting/UserFrosting
66
* @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
77
*/
8-
namespace UserFrosting\Sprinkle\Account\Util;
8+
namespace UserFrosting\Sprinkle\Account\Authenticate;
99

1010
/**
11-
* Password utility class
11+
* Password hashing and validation class
1212
*
1313
* @author Alex Weissman (https://alexanderweissman.com)
1414
*/
15-
class Password
15+
class Hasher
1616
{
1717
/**
1818
* Default crypt cost factor.
1919
*
2020
* @var int
2121
*/
22-
protected static $rounds = 10;
22+
protected $defaultRounds = 10;
2323

2424
/**
2525
* Returns the hashing type for a specified password hash.
@@ -28,7 +28,7 @@ class Password
2828
* @param string $password the hashed password.
2929
* @return string "sha1"|"legacy"|"modern".
3030
*/
31-
public static function getHashType($password)
31+
public function getHashType($password)
3232
{
3333
// If the password in the db is 65 characters long, we have an sha1-hashed password.
3434
if (strlen($password) == 65) {
@@ -48,10 +48,10 @@ public static function getHashType($password)
4848
* @return string the hashed password.
4949
* @throws HashFailedException
5050
*/
51-
public static function hash($password, array $options = [])
51+
public function hash($password, array $options = [])
5252
{
5353
$hash = password_hash($password, PASSWORD_BCRYPT, [
54-
'cost' => static::cost($options),
54+
'cost' => $this->cost($options),
5555
]);
5656

5757
if (!$hash) {
@@ -69,9 +69,9 @@ public static function hash($password, array $options = [])
6969
* @param array $options
7070
* @return boolean True if the password matches, false otherwise.
7171
*/
72-
public static function verify($password, $hash, array $options = [])
72+
public function verify($password, $hash, array $options = [])
7373
{
74-
$hashType = static::getHashType($hash);
74+
$hashType = $this->getHashType($hash);
7575

7676
if ($hashType == 'sha1') {
7777
// Legacy UserCake passwords
@@ -101,8 +101,8 @@ public static function verify($password, $hash, array $options = [])
101101
* @param array $options
102102
* @return int
103103
*/
104-
protected static function cost(array $options = [])
104+
protected function cost(array $options = [])
105105
{
106-
return isset($options['rounds']) ? $options['rounds'] : static::$rounds;
106+
return isset($options['rounds']) ? $options['rounds'] : $this->defaultRounds;
107107
}
108108
}

app/sprinkles/account/src/Bakery/CreateAdminUser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use UserFrosting\System\Database\Model\Migrations;
1818
use UserFrosting\Sprinkle\Account\Database\Models\User;
1919
use UserFrosting\Sprinkle\Account\Database\Models\Role;
20-
use UserFrosting\Sprinkle\Account\Util\Password;
20+
use UserFrosting\Sprinkle\Account\Facades\Password;
2121

2222
/**
2323
* Create root user CLI command.

app/sprinkles/account/src/Controller/AccountController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use UserFrosting\Sprinkle\Account\Controller\Exception\SpammyRequestException;
2222
use UserFrosting\Sprinkle\Account\Database\Models\Group;
2323
use UserFrosting\Sprinkle\Account\Database\Models\User;
24-
use UserFrosting\Sprinkle\Account\Util\Password;
24+
use UserFrosting\Sprinkle\Account\Facades\Password;
2525
use UserFrosting\Sprinkle\Account\Util\Util as AccountUtil;
2626
use UserFrosting\Sprinkle\Core\Controller\SimpleController;
2727
use UserFrosting\Sprinkle\Core\Facades\Debug;

app/sprinkles/account/src/Database/Models/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Carbon\Carbon;
1111
use Illuminate\Database\Capsule\Manager as Capsule;
1212
use Illuminate\Database\Eloquent\SoftDeletes;
13-
use UserFrosting\Sprinkle\Account\Util\Password;
13+
use UserFrosting\Sprinkle\Account\Facades\Password;
1414
use UserFrosting\Sprinkle\Core\Database\Models\Model;
1515
use UserFrosting\Sprinkle\Core\Facades\Debug;
1616

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* UserFrosting (http://www.userfrosting.com)
4+
*
5+
* @link https://github.com/userfrosting/UserFrosting
6+
* @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
7+
*/
8+
namespace UserFrosting\Sprinkle\Account\Facades;
9+
10+
use UserFrosting\System\Facade;
11+
12+
/**
13+
* Implements facade for the "password" service
14+
*
15+
* @author Alex Weissman (https://alexanderweissman.com)
16+
*/
17+
class Password extends Facade
18+
{
19+
/**
20+
* Get the registered name of the component.
21+
*
22+
* @return string
23+
*/
24+
protected static function getFacadeAccessor()
25+
{
26+
return 'passwordHasher';
27+
}
28+
}

app/sprinkles/account/src/Repository/PasswordResetRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
namespace UserFrosting\Sprinkle\Account\Repository;
99

10-
use UserFrosting\Sprinkle\Account\Util\Password;
10+
use UserFrosting\Sprinkle\Account\Facades\Password;
1111

1212
/**
1313
* Token repository class for password reset requests.

0 commit comments

Comments
 (0)