Skip to content

Commit ac5f291

Browse files
authored
Merge pull request #22641 from nextcloud/fix/credentials-store-upgrade-property-undefined
Fix undefined class property access after upgrade from 19 to 20
2 parents 292c86f + adf100a commit ac5f291

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

lib/private/Authentication/LoginCredentials/Store.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,13 @@ public function getLoginCredentials(): ICredentials {
112112
}
113113

114114
if ($trySession && $this->session->exists('login_credentials')) {
115-
$creds = json_decode($this->session->get('login_credentials'));
116-
return new Credentials($creds->uid, $creds->loginName, $creds->password);
115+
/** @var array $creds */
116+
$creds = json_decode($this->session->get('login_credentials'), true);
117+
return new Credentials(
118+
$creds['uid'],
119+
$creds['loginName'] ?? $this->session->get('loginname') ?? $creds['uid'], // Pre 20 didn't have a loginName property, hence fall back to the session value and then to the UID
120+
$creds['password']
121+
);
117122
}
118123

119124
// If we reach this line, an exception was thrown.

tests/lib/Authentication/LoginCredentials/StoreTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OCP\ISession;
3636
use OCP\Session\Exceptions\SessionNotAvailableException;
3737
use Test\TestCase;
38+
use function json_encode;
3839

3940
class StoreTest extends TestCase {
4041

@@ -140,6 +141,81 @@ public function testGetLoginCredentialsInvalidToken() {
140141
$this->store->getLoginCredentials();
141142
}
142143

144+
public function testGetLoginCredentialsPartialCredentialsAndSessionName() {
145+
$uid = 'id987';
146+
$user = 'user987';
147+
$password = '7389374';
148+
149+
$this->session->expects($this->once())
150+
->method('getId')
151+
->willReturn('sess2233');
152+
$this->tokenProvider->expects($this->once())
153+
->method('getToken')
154+
->with('sess2233')
155+
->will($this->throwException(new InvalidTokenException()));
156+
$this->session->expects($this->once())
157+
->method('exists')
158+
->with($this->equalTo('login_credentials'))
159+
->willReturn(true);
160+
$this->session->expects($this->exactly(2))
161+
->method('get')
162+
->willReturnMap([
163+
[
164+
'login_credentials',
165+
json_encode([
166+
'uid' => $uid,
167+
'password' => $password,
168+
])
169+
],
170+
[
171+
'loginname',
172+
$user,
173+
],
174+
]);
175+
$expected = new Credentials($uid, $user, $password);
176+
177+
$actual = $this->store->getLoginCredentials();
178+
179+
$this->assertEquals($expected, $actual);
180+
}
181+
182+
public function testGetLoginCredentialsPartialCredentials() {
183+
$uid = 'id987';
184+
$password = '7389374';
185+
186+
$this->session->expects($this->once())
187+
->method('getId')
188+
->willReturn('sess2233');
189+
$this->tokenProvider->expects($this->once())
190+
->method('getToken')
191+
->with('sess2233')
192+
->will($this->throwException(new InvalidTokenException()));
193+
$this->session->expects($this->once())
194+
->method('exists')
195+
->with($this->equalTo('login_credentials'))
196+
->willReturn(true);
197+
$this->session->expects($this->exactly(2))
198+
->method('get')
199+
->willReturnMap([
200+
[
201+
'login_credentials',
202+
json_encode([
203+
'uid' => $uid,
204+
'password' => $password,
205+
])
206+
],
207+
[
208+
'loginname',
209+
null,
210+
],
211+
]);
212+
$expected = new Credentials($uid, $uid, $password);
213+
214+
$actual = $this->store->getLoginCredentials();
215+
216+
$this->assertEquals($expected, $actual);
217+
}
218+
143219
public function testGetLoginCredentialsInvalidTokenLoginCredentials() {
144220
$uid = 'id987';
145221
$user = 'user987';

0 commit comments

Comments
 (0)