Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Do not build encrypted password if there is none #51130

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/private/Authentication/LoginCredentials/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function __construct(
* @param array $params
*/
public function authenticate(array $params) {
$params['password'] = $this->crypto->encrypt((string)$params['password']);
if ($params['password'] !== null) {
$params['password'] = $this->crypto->encrypt((string)$params['password']);
}
$this->session->set('login_credentials', json_encode($params));
}

Expand Down Expand Up @@ -97,10 +99,12 @@ public function getLoginCredentials(): ICredentials {
if ($trySession && $this->session->exists('login_credentials')) {
/** @var array $creds */
$creds = json_decode($this->session->get('login_credentials'), true);
try {
$creds['password'] = $this->crypto->decrypt($creds['password']);
} catch (Exception $e) {
//decryption failed, continue with old password as it is
if ($creds['password'] !== null) {
try {
$creds['password'] = $this->crypto->decrypt($creds['password']);
} catch (Exception $e) {
//decryption failed, continue with old password as it is
}
}
return new Credentials(
$creds['uid'],
Expand Down
40 changes: 40 additions & 0 deletions tests/lib/Authentication/LoginCredentials/StoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,44 @@ public function testGetLoginCredentialsPasswordlessToken(): void {

$this->store->getLoginCredentials();
}

public function testAuthenticatePasswordlessToken(): void {
$user = 'user987';
$password = null;

$params = [
'run' => true,
'loginName' => $user,
'uid' => $user,
'password' => $password,
];

$this->session->expects($this->once())
->method('set')
->with($this->equalTo('login_credentials'), $this->equalTo(json_encode($params)));


$this->session->expects($this->once())
->method('getId')
->willReturn('sess2233');
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('sess2233')
->will($this->throwException(new PasswordlessTokenException()));

$this->session->expects($this->once())
->method('exists')
->with($this->equalTo('login_credentials'))
->willReturn(true);
$this->session->expects($this->once())
->method('get')
->with($this->equalTo('login_credentials'))
->willReturn(json_encode($params));

$this->store->authenticate($params);
$actual = $this->store->getLoginCredentials();

$expected = new Credentials($user, $user, $password);
$this->assertEquals($expected, $actual);
}
}
Loading