Skip to content

Commit 1ee1c80

Browse files
fix(session): Only mark sessions of permanent tokens as app passwords
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent 1a9f52d commit 1ee1c80

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
@@ -883,9 +883,8 @@ public function tryTokenLogin(IRequest $request) {
883883
return true;
884884
}
885885

886-
// Remember me tokens are not app_passwords
887-
if ($dbToken->getRemember() === IToken::DO_NOT_REMEMBER) {
888-
// Set the session variable so we know this is an app password
886+
// Set the session variable so we know this is an app password
887+
if ($dbToken instanceof \OC\Authentication\Token\PublicKeyToken && $dbToken->getType() === IToken::PERMANENT_TOKEN) {
889888
$this->session->set('app_password', $token);
890889
}
891890

tests/lib/User/SessionTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\Lockdown\ILockdownManager;
3333
use OCP\Security\ISecureRandom;
3434
use OCP\User\Events\PostLoginEvent;
35+
use PHPUnit\Framework\ExpectationFailedException;
3536
use PHPUnit\Framework\MockObject\MockObject;
3637
use Psr\Log\LoggerInterface;
3738
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -533,6 +534,45 @@ public function testTryTokenLoginSessionIdTokenNotFound(): void {
533534
self::assertFalse($loginResult);
534535
}
535536

537+
public function testTryTokenLoginNotAnAppPassword(): void {
538+
$request = $this->createMock(IRequest::class);
539+
$this->config->expects(self::once())
540+
->method('getSystemValueString')
541+
->with('instanceid')
542+
->willReturn('abc123');
543+
$request->method('getHeader')->with('Authorization')->willReturn('');
544+
$request->method('getCookie')->with('abc123')->willReturn('abcde12345');
545+
$this->session->expects(self::once())
546+
->method('getId')
547+
->willReturn('abcde12345');
548+
$dbToken = new PublicKeyToken();
549+
$dbToken->setId(42);
550+
$dbToken->setUid('johnny');
551+
$dbToken->setLoginName('johnny');
552+
$dbToken->setLastCheck(0);
553+
$dbToken->setType(IToken::TEMPORARY_TOKEN);
554+
$dbToken->setRemember(IToken::REMEMBER);
555+
$this->tokenProvider->expects(self::any())
556+
->method('getToken')
557+
->with('abcde12345')
558+
->willReturn($dbToken);
559+
$this->session->method('set')
560+
->willReturnCallback(function ($key, $value) {
561+
if ($key === 'app_password') {
562+
throw new ExpectationFailedException('app_password should not be set in session');
563+
}
564+
});
565+
$user = $this->createMock(IUser::class);
566+
$user->method('isEnabled')->willReturn(true);
567+
$this->manager->method('get')
568+
->with('johnny')
569+
->willReturn($user);
570+
571+
$loginResult = $this->userSession->tryTokenLogin($request);
572+
573+
self::assertTrue($loginResult);
574+
}
575+
536576
public function testRememberLoginValidToken() {
537577
$session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
538578
$managerMethods = get_class_methods(Manager::class);

0 commit comments

Comments
 (0)