Skip to content

Commit 97b54f2

Browse files
committed
feat: add support for wp_hash_password_algorithm hook in wordpress 6.8
1 parent 5251f54 commit 97b54f2

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

src/Configuration/EventManagementConfiguration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PasswordsEvolved\Subscriber\AuthenticationSubscriber;
1919
use PasswordsEvolved\Subscriber\CapabilitiesSubscriber;
2020
use PasswordsEvolved\Subscriber\NetworkAdminPageSubscriber;
21+
use PasswordsEvolved\Subscriber\PasswordHashingSubscriber;
2122
use PasswordsEvolved\Subscriber\ResetPasswordSubscriber;
2223
use PasswordsEvolved\Subscriber\TranslationsSubscriber;
2324
use PasswordsEvolved\Subscriber\UserProfileSubscriber;
@@ -43,6 +44,7 @@ public function modify(Container $container)
4344
$subscribers = array(
4445
new AuthenticationSubscriber($container['api_client']),
4546
new CapabilitiesSubscriber($container['options']->get('enforced_roles', array('administrator')), $container['wordpress.roles']),
47+
new PasswordHashingSubscriber(),
4648
new ResetPasswordSubscriber($container['api_client'], $container['translator']),
4749
new TranslationsSubscriber($container['plugin_domain'], $container['plugin_path'] . '/resources/translations'),
4850
new UserProfileSubscriber($container['api_client']),
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Passwords Evolved WordPress plugin.
5+
*
6+
* (c) Carl Alexander <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace PasswordsEvolved\Subscriber;
13+
14+
use PasswordsEvolved\EventManagement\SubscriberInterface;
15+
16+
/**
17+
* Subscriber that handles WordPress password hashing.
18+
*
19+
* @author Carl Alexander <[email protected]>
20+
*/
21+
class PasswordHashingSubscriber implements SubscriberInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public static function get_subscribed_events()
27+
{
28+
return array(
29+
'wp_hash_password_algorithm' => 'set_password_hashing_algorithm',
30+
);
31+
}
32+
33+
/**
34+
* Set the password hashing algorithm.
35+
*
36+
* @param mixed $algorithm
37+
*/
38+
public function set_password_hashing_algorithm($algorithm)
39+
{
40+
if (defined('PASSWORD_ARGON2ID')) {
41+
$algorithm = PASSWORD_ARGON2ID;
42+
} elseif (defined('PASSWORD_ARGON2I')) {
43+
$algorithm = PASSWORD_ARGON2I;
44+
}
45+
46+
return $algorithm;
47+
}
48+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Passwords Evolved WordPress plugin.
5+
*
6+
* (c) Carl Alexander <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace PasswordsEvolved\Tests\Unit\Subscriber;
13+
14+
use PasswordsEvolved\Subscriber\PasswordHashingSubscriber;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class PasswordHashingSubscriberTest extends TestCase
18+
{
19+
public function test_get_subscribed_events()
20+
{
21+
$callbacks = PasswordHashingSubscriber::get_subscribed_events();
22+
23+
foreach ($callbacks as $callback) {
24+
$this->assertTrue(method_exists(PasswordHashingSubscriber::class, is_array($callback) ? $callback[0] : $callback));
25+
}
26+
}
27+
28+
public function test_set_password_hashing_algorithm()
29+
{
30+
$expectedAlgorithm = 'algorithm';
31+
32+
if (PHP_VERSION_ID >= 70300) {
33+
$expectedAlgorithm = PASSWORD_ARGON2ID;
34+
} elseif (PHP_VERSION_ID >= 70200) {
35+
$expectedAlgorithm = PASSWORD_ARGON2I;
36+
}
37+
38+
$this->assertEquals($expectedAlgorithm, (new PasswordHashingSubscriber())->set_password_hashing_algorithm('algorithm'));
39+
}
40+
41+
public function test_set_password_hashing_algorithm_prioritizes_argon2id_over_argon2i()
42+
{
43+
if (PHP_VERSION_ID < 70300) {
44+
$this->markTestSkipped('This test requires PHP 7.3 or higher where both PASSWORD_ARGON2ID and PASSWORD_ARGON2I are defined.');
45+
}
46+
47+
$this->assertTrue(defined('PASSWORD_ARGON2ID'));
48+
$this->assertTrue(defined('PASSWORD_ARGON2I'));
49+
50+
$result = (new PasswordHashingSubscriber())->set_password_hashing_algorithm('algorithm');
51+
$this->assertEquals(PASSWORD_ARGON2ID, $result);
52+
$this->assertNotEquals(PASSWORD_ARGON2I, $result);
53+
}
54+
}

0 commit comments

Comments
 (0)