Skip to content

Commit 8f6386d

Browse files
authored
Merge pull request #51905 from nextcloud/fix/session/permanent-token-app-password
fix(session): Only mark sessions of permanent tokens as app passwords
2 parents 07f4c10 + 5003467 commit 8f6386d

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/private/User/Session.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -834,9 +834,8 @@ public function tryTokenLogin(IRequest $request) {
834834
return true;
835835
}
836836

837-
// Remember me tokens are not app_passwords
838-
if ($dbToken->getRemember() === IToken::DO_NOT_REMEMBER) {
839-
// Set the session variable so we know this is an app password
837+
// Set the session variable so we know this is an app password
838+
if ($dbToken instanceof PublicKeyToken && $dbToken->getType() === IToken::PERMANENT_TOKEN) {
840839
$this->session->set('app_password', $token);
841840
}
842841

tests/lib/User/SessionTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OCP\Security\Bruteforce\IThrottler;
3535
use OCP\Security\ISecureRandom;
3636
use OCP\User\Events\PostLoginEvent;
37+
use PHPUnit\Framework\ExpectationFailedException;
3738
use PHPUnit\Framework\MockObject\MockObject;
3839
use Psr\Log\LoggerInterface;
3940
use function array_diff;
@@ -611,6 +612,45 @@ public function testTryTokenLoginSessionIdTokenNotFound(): void {
611612
self::assertFalse($loginResult);
612613
}
613614

615+
public function testTryTokenLoginNotAnAppPassword(): void {
616+
$request = $this->createMock(IRequest::class);
617+
$this->config->expects(self::once())
618+
->method('getSystemValueString')
619+
->with('instanceid')
620+
->willReturn('abc123');
621+
$request->method('getHeader')->with('Authorization')->willReturn('');
622+
$request->method('getCookie')->with('abc123')->willReturn('abcde12345');
623+
$this->session->expects(self::once())
624+
->method('getId')
625+
->willReturn('abcde12345');
626+
$dbToken = new PublicKeyToken();
627+
$dbToken->setId(42);
628+
$dbToken->setUid('johnny');
629+
$dbToken->setLoginName('johnny');
630+
$dbToken->setLastCheck(0);
631+
$dbToken->setType(IToken::TEMPORARY_TOKEN);
632+
$dbToken->setRemember(IToken::REMEMBER);
633+
$this->tokenProvider->expects(self::any())
634+
->method('getToken')
635+
->with('abcde12345')
636+
->willReturn($dbToken);
637+
$this->session->method('set')
638+
->willReturnCallback(function ($key, $value) {
639+
if ($key === 'app_password') {
640+
throw new ExpectationFailedException('app_password should not be set in session');
641+
}
642+
});
643+
$user = $this->createMock(IUser::class);
644+
$user->method('isEnabled')->willReturn(true);
645+
$this->manager->method('get')
646+
->with('johnny')
647+
->willReturn($user);
648+
649+
$loginResult = $this->userSession->tryTokenLogin($request);
650+
651+
self::assertTrue($loginResult);
652+
}
653+
614654
public function testRememberLoginValidToken(): void {
615655
$session = $this->createMock(Memory::class);
616656
$managerMethods = get_class_methods(Manager::class);

0 commit comments

Comments
 (0)