-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from roots/application-password-support
Support WP application passwords
- Loading branch information
Showing
6 changed files
with
146 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Roots\PasswordBcrypt\Tests\Unit; | ||
|
||
use Roots\PasswordBcrypt\Tests\TestCase; | ||
use Roots\PasswordBcrypt\Tests\Constants; | ||
use Mockery; | ||
|
||
use function Brain\Monkey\Functions\expect; | ||
use function Brain\Monkey\Filters\expectApplied; | ||
|
||
class ApplicationPasswordTest extends TestCase | ||
{ | ||
|
||
/** @test */ | ||
public function phpass_application_passwords_should_be_verified_and_converted_to_bcrypt() | ||
{ | ||
require_once __DIR__ . '/../WPApplicationPasswords.php'; | ||
|
||
expectApplied('application_password_is_api_request') | ||
->andReturn(true); | ||
|
||
$this | ||
->wpHasher() | ||
->shouldReceive('CheckPassword') | ||
->times(3) | ||
->andReturnValues([true, true, false]); | ||
|
||
expect('update_user_meta') | ||
->once() | ||
->withArgs(function (...$args) { | ||
[$userId, $metaKey, $passwords] = $args; | ||
|
||
if ($userId != Constants::USER_ID) { | ||
return false; | ||
} | ||
|
||
if ($metaKey != \WP_Application_Passwords::USERMETA_KEY_APPLICATION_PASSWORDS) { | ||
return false; | ||
} | ||
|
||
if (count($passwords) != 3) { | ||
return false; | ||
} | ||
|
||
if (!key_exists(0, $passwords)) { | ||
return false; | ||
} | ||
|
||
$passwords = array_map((function ($item) { | ||
return $item['password']; | ||
}), $passwords); | ||
|
||
[$pw1, $pw2, $pw3] = $passwords; | ||
|
||
if (!password_verify(Constants::PASSWORD, $pw1)) { | ||
return false; | ||
} | ||
|
||
if (!password_verify(Constants::PASSWORD, $pw2)) { | ||
return false; | ||
} | ||
|
||
if (password_verify(Constants::PASSWORD, $pw3)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
$hash = wp_set_password(Constants::PASSWORD, Constants::USER_ID); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
use Roots\PasswordBcrypt\Tests\Constants; | ||
|
||
// 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 [ | ||
[ | ||
'password' => Constants::PHPPASS_HASH | ||
], | ||
[ | ||
'password' => Constants::BCRYPT_HASH | ||
], | ||
[ | ||
'password' => Constants::INVALID_HASH | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters