Skip to content

Commit 381017c

Browse files
committed
fix: avoid changing the IUserSession interface
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
1 parent 5d15fc0 commit 381017c

6 files changed

Lines changed: 1 addition & 179 deletions

File tree

apps/dav/lib/Connector/Sabre/BearerAuth.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ public function validateBearerToken($bearerToken) {
5151
$this->userSession->tryTokenLogin($this->request);
5252
$loggedIn = $this->userSession->isLoggedIn();
5353
}
54-
if (!$loggedIn) {
55-
$this->userSession->doTryTokenLogin($bearerToken);
56-
$loggedIn = $this->userSession->isLoggedIn();
57-
}
5854
if ($loggedIn) {
5955
return $this->setupUserFs($this->userSession->getUser()->getUID());
6056
}

apps/dav/tests/unit/Connector/Sabre/BearerAuthTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,6 @@ public function testValidateBearerToken(): void {
6969
$this->assertSame('principals/users/admin', $this->bearerAuth->validateBearerToken('Token'));
7070
}
7171

72-
public function testValidateBearerTokenFallbackToDoTryTokenLogin(): void {
73-
// First two isLoggedIn() calls return false (tryTokenLogin did not log in),
74-
// then doTryTokenLogin succeeds and the third call returns true.
75-
$this->userSession
76-
->expects($this->exactly(3))
77-
->method('isLoggedIn')
78-
->willReturnOnConsecutiveCalls(false, false, true);
79-
80-
$this->userSession
81-
->expects($this->once())
82-
->method('tryTokenLogin')
83-
->with($this->request);
84-
85-
$this->userSession
86-
->expects($this->once())
87-
->method('doTryTokenLogin')
88-
->with('BearerToken');
89-
90-
$user = $this->createMock(IUser::class);
91-
$user->expects($this->once())
92-
->method('getUID')
93-
->willReturn('admin');
94-
$this->userSession
95-
->expects($this->once())
96-
->method('getUser')
97-
->willReturn($user);
98-
99-
$this->assertSame('principals/users/admin', $this->bearerAuth->validateBearerToken('BearerToken'));
100-
}
101-
10272
public function testChallenge(): void {
10373
/** @var RequestInterface&MockObject $request */
10474
$request = $this->createMock(RequestInterface::class);

apps/files_external/lib/Migration/DummyUserSession.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,4 @@ public function getImpersonatingUserID() : ?string {
5454
public function setImpersonatingUserID(bool $useCurrentUser = true): void {
5555
//no OP
5656
}
57-
58-
public function doTryTokenLogin(string $token): bool {
59-
return false;
60-
}
6157
}

lib/private/User/Session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ public function tryTokenLogin(IRequest $request) {
854854
return $this->doTryTokenLogin($token);
855855
}
856856

857-
public function doTryTokenLogin(string $token): bool {
857+
private function doTryTokenLogin(string $token): bool {
858858
if (!$this->loginWithToken($token)) {
859859
return false;
860860
}

lib/public/IUserSession.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,4 @@ public function getImpersonatingUserID(): ?string;
9292
* @since 18.0.0
9393
*/
9494
public function setImpersonatingUserID(bool $useCurrentUser = true): void;
95-
96-
/**
97-
* Try to login with the given token
98-
*
99-
* @param string $token
100-
* @return bool true if successful
101-
* @since 32.0.0
102-
*/
103-
public function doTryTokenLogin(string $token): bool;
10495
}

tests/lib/User/SessionTest.php

Lines changed: 0 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,135 +1337,4 @@ public function testLogClientInThrottlerEmail(): void {
13371337
$this->assertFalse($userSession->logClientIn('john@foo.bar', 'I-AM-A-PASSWORD', $request, $this->throttler));
13381338
}
13391339

1340-
public function testDoTryTokenLoginSuccess(): void {
1341-
$manager = $this->createMock(Manager::class);
1342-
$session = $this->createMock(ISession::class);
1343-
1344-
$user = $this->createMock(IUser::class);
1345-
$user->method('getUID')->willReturn('testuser');
1346-
$user->method('isEnabled')->willReturn(true);
1347-
1348-
$manager->method('get')
1349-
->with('testuser')
1350-
->willReturn($user);
1351-
1352-
$token = $this->createMock(PublicKeyToken::class);
1353-
$token->method('getUID')->willReturn('testuser');
1354-
$token->method('getLoginName')->willReturn('testuser');
1355-
$token->method('getType')->willReturn(\OCP\Authentication\Token\IToken::PERMANENT_TOKEN);
1356-
$token->method('getLastCheck')->willReturn($this->timeFactory->getTime());
1357-
1358-
$this->tokenProvider->method('getToken')
1359-
->with('valid-token')
1360-
->willReturn($token);
1361-
1362-
$appPasswordSet = false;
1363-
$session->expects($this->atLeastOnce())
1364-
->method('set')
1365-
->willReturnCallback(function ($key, $value) use (&$appPasswordSet) {
1366-
// We expect app_password to be set for permanent tokens
1367-
if ($key === 'app_password') {
1368-
$appPasswordSet = true;
1369-
$this->assertEquals('valid-token', $value);
1370-
}
1371-
return true;
1372-
});
1373-
1374-
/** @var Session $userSession */
1375-
$userSession = $this->getMockBuilder(Session::class)
1376-
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
1377-
->onlyMethods(['setMagicInCookie'])
1378-
->getMock();
1379-
1380-
$this->assertTrue($userSession->doTryTokenLogin('valid-token'));
1381-
$this->assertTrue($appPasswordSet, 'app_password should be set for permanent tokens');
1382-
}
1383-
1384-
public function testDoTryTokenLoginInvalidToken(): void {
1385-
$manager = $this->createMock(Manager::class);
1386-
$session = $this->createMock(ISession::class);
1387-
1388-
$this->tokenProvider->method('getToken')
1389-
->with('invalid-token')
1390-
->willThrowException(new InvalidTokenException());
1391-
1392-
/** @var Session $userSession */
1393-
$userSession = $this->getMockBuilder(Session::class)
1394-
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
1395-
->onlyMethods(['setMagicInCookie'])
1396-
->getMock();
1397-
1398-
$this->assertFalse($userSession->doTryTokenLogin('invalid-token'));
1399-
}
1400-
1401-
public function testDoTryTokenLoginTemporaryToken(): void {
1402-
$manager = $this->createMock(Manager::class);
1403-
$session = $this->createMock(ISession::class);
1404-
1405-
$user = $this->createMock(IUser::class);
1406-
$user->method('getUID')->willReturn('testuser');
1407-
$user->method('isEnabled')->willReturn(true);
1408-
1409-
$manager->method('get')
1410-
->with('testuser')
1411-
->willReturn($user);
1412-
1413-
$token = $this->createMock(PublicKeyToken::class);
1414-
$token->method('getUID')->willReturn('testuser');
1415-
$token->method('getLoginName')->willReturn('testuser');
1416-
$token->method('getType')->willReturn(\OCP\Authentication\Token\IToken::TEMPORARY_TOKEN);
1417-
$token->method('getLastCheck')->willReturn($this->timeFactory->getTime());
1418-
1419-
$this->tokenProvider->method('getToken')
1420-
->with('temp-token')
1421-
->willReturn($token);
1422-
1423-
// app_password should NOT be set for temporary tokens
1424-
$session->expects($this->atLeastOnce())
1425-
->method('set')
1426-
->willReturnCallback(function ($key, $value) {
1427-
$this->assertNotEquals('app_password', $key, 'app_password should not be set for temporary tokens');
1428-
return true;
1429-
});
1430-
1431-
/** @var Session $userSession */
1432-
$userSession = $this->getMockBuilder(Session::class)
1433-
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
1434-
->onlyMethods(['setMagicInCookie'])
1435-
->getMock();
1436-
1437-
$this->assertTrue($userSession->doTryTokenLogin('temp-token'));
1438-
}
1439-
1440-
public function testDoTryTokenLoginDisabledUser(): void {
1441-
$manager = $this->createMock(Manager::class);
1442-
$session = $this->createMock(ISession::class);
1443-
1444-
$user = $this->createMock(IUser::class);
1445-
$user->method('getUID')->willReturn('testuser');
1446-
$user->method('isEnabled')->willReturn(false);
1447-
1448-
$manager->method('get')
1449-
->with('testuser')
1450-
->willReturn($user);
1451-
1452-
$token = $this->createMock(PublicKeyToken::class);
1453-
$token->method('getUID')->willReturn('testuser');
1454-
$token->method('getLoginName')->willReturn('testuser');
1455-
$token->method('getType')->willReturn(\OCP\Authentication\Token\IToken::PERMANENT_TOKEN);
1456-
$token->method('getLastCheck')->willReturn($this->timeFactory->getTime());
1457-
1458-
$this->tokenProvider->method('getToken')
1459-
->with('valid-token')
1460-
->willReturn($token);
1461-
1462-
/** @var Session $userSession */
1463-
$userSession = $this->getMockBuilder(Session::class)
1464-
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
1465-
->onlyMethods(['setMagicInCookie'])
1466-
->getMock();
1467-
1468-
$this->expectException(LoginException::class);
1469-
$userSession->doTryTokenLogin('valid-token');
1470-
}
14711340
}

0 commit comments

Comments
 (0)