Skip to content

Commit

Permalink
Merge pull request #41 from johnbillion/return-hash
Browse files Browse the repository at this point in the history
Ensure the hashed password is always returned
  • Loading branch information
swalkinshaw authored Sep 10, 2024
2 parents 3de5abe + 348cc40 commit bd26ab9
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
13 changes: 13 additions & 0 deletions tests/EmptyWPApplicationPasswords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
// phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
class WP_Application_Passwords
{
public const USERMETA_KEY_APPLICATION_PASSWORDS = '_application_passwords';

public static function get_user_application_passwords($userId)
{
return [];
}
}
7 changes: 6 additions & 1 deletion tests/Unit/ApplicationPasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
class ApplicationPasswordTest extends TestCase
{

/** @test */
/**
* @test
* @runInSeparateProcess
*/
public function phpass_application_passwords_should_be_verified_and_converted_to_bcrypt()
{
require_once __DIR__ . '/../WPApplicationPasswords.php';
Expand Down Expand Up @@ -69,5 +72,7 @@ public function phpass_application_passwords_should_be_verified_and_converted_to
});

$hash = wp_set_password(Constants::PASSWORD, Constants::USER_ID);

$this->assertIsString($hash);
}
}
34 changes: 34 additions & 0 deletions tests/Unit/EmptyApplicationPasswordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Roots\PasswordBcrypt\Tests\Unit;

use Roots\PasswordBcrypt\Tests\TestCase;
use Roots\PasswordBcrypt\Tests\Constants;

use function Brain\Monkey\Filters\expectApplied;

class EmptyApplicationPasswordTest extends TestCase
{

/**
* @test
* @runInSeparateProcess
*/
public function phpass_application_passwords_should_be_verified_and_converted_to_bcrypt()
{
require_once __DIR__ . '/../EmptyWPApplicationPasswords.php';

expectApplied('application_password_is_api_request')
->andReturn(true);

$this
->wpHasher()
->shouldReceive('CheckPassword')
->times(3)
->andReturnValues([true, true, false]);

$hash = wp_set_password(Constants::PASSWORD, Constants::USER_ID);

$this->assertIsString($hash);
}
}
33 changes: 33 additions & 0 deletions tests/Unit/RESTAPITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Roots\PasswordBcrypt\Tests\Unit;

use Roots\PasswordBcrypt\Tests\TestCase;
use Roots\PasswordBcrypt\Tests\Constants;

use function Brain\Monkey\Filters\expectApplied;

class RESTAPIPasswordTest extends TestCase
{

/**
* @test
* @runInSeparateProcess
*/
public function phpass_application_passwords_should_be_verified_and_converted_to_bcrypt()
{
expectApplied('application_password_is_api_request')
->andReturn(true);

$this
->wpHasher()
->shouldReceive('CheckPassword')
->times(3)
->andReturnValues([true, true, false]);

$hash = wp_set_password(Constants::PASSWORD, Constants::USER_ID);

$this->assertFalse(class_exists('WP_Application_Passwords'));
$this->assertIsString($hash);
}
}
5 changes: 4 additions & 1 deletion tests/Unit/UserPasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ public function a_password_is_hashed_using_bcrypt()
->once()
->andReturn(true);

$hash = wp_set_password(Constants::PASSWORD, Constants::USER_ID);

$this->assertIsString($hash);
$this->assertTrue(
password_verify(Constants::PASSWORD, wp_set_password(Constants::PASSWORD, Constants::USER_ID))
password_verify(Constants::PASSWORD, $hash)
);
}

Expand Down
4 changes: 2 additions & 2 deletions wp-password-bcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function wp_hash_password($password)
*
* @param string $password The new user password in plaintext.
* @param int $user_id The user ID.
* @return string
* @return string The new hashed password.
*/
function wp_set_password($password, $user_id)
{
Expand Down Expand Up @@ -117,7 +117,7 @@ function wp_set_password($password, $user_id)
! class_exists('WP_Application_Passwords') ||
empty($passwords = WP_Application_Passwords::get_user_application_passwords($user_id))
) {
return;
return $hash;
}

global $wp_hasher;
Expand Down

0 comments on commit bd26ab9

Please sign in to comment.