Skip to content
Open
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
51 changes: 41 additions & 10 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,15 @@ public function logClientIn($user,
}

try {
$isTokenPassword = $this->isTokenPassword($password);
} catch (ExpiredTokenException $e) {
$dbToken = $this->getTokenFromPassword($password);
$isTokenPassword = $dbToken !== null;
if (($dbToken instanceof PublicKeyToken)
&& ($dbToken->getType() !== IToken::PERMANENT_TOKEN)
) {
// Refuse session tokens here, only app tokens are handled
return false;
}
} catch (ExpiredTokenException) {
// Just return on an expired token no need to check further or record a failed login
return false;
}
Expand All @@ -410,7 +417,6 @@ public function logClientIn($user,
}

if ($isTokenPassword) {
$dbToken = $this->tokenProvider->getToken($password);
$userFromToken = $this->manager->get($dbToken->getUID());
$isValidEmailLogin = $userFromToken->getEMailAddress() === $user
&& $this->validateTokenLoginName($userFromToken->getEMailAddress(), $dbToken);
Expand Down Expand Up @@ -500,6 +506,24 @@ public function isTokenPassword($password) {
}
}

/**
* Check if the given 'password' is actually a device token
*
* @throws ExpiredTokenException
*/
private function getTokenFromPassword(string $password): ?\OCP\Authentication\Token\IToken {
try {
return $this->tokenProvider->getToken($password);
} catch (ExpiredTokenException $e) {
throw $e;
} catch (InvalidTokenException $ex) {
$this->logger->debug('Token is not valid: ' . $ex->getMessage(), [
'exception' => $ex,
]);
return null;
}
}

protected function prepareUserLogin($firstTimeLogin, $refreshCsrfToken = true) {
if ($refreshCsrfToken) {
// TODO: mock/inject/use non-static
Expand Down Expand Up @@ -806,32 +830,39 @@ private function validateTokenLoginName(?string $loginName, IToken $token): bool
*/
public function tryTokenLogin(IRequest $request) {
$authHeader = $request->getHeader('Authorization');
$tokenFromCookie = false;
if (str_starts_with($authHeader, 'Bearer ')) {
$token = substr($authHeader, 7);
} elseif ($request->getCookie($this->config->getSystemValueString('instanceid')) !== null) {
// No auth header, let's try session id, but only if this is an existing
// session and the request has a session cookie
try {
$token = $this->session->getId();
$tokenFromCookie = true;
} catch (SessionNotAvailableException $ex) {
return false;
}
} else {
return false;
}

if (!$this->loginWithToken($token)) {
try {
$dbToken = $this->tokenProvider->getToken($token);
} catch (InvalidTokenException $e) {
// Can't really happen but better safe than sorry
return false;
}
if (!$this->validateToken($token)) {

if ($dbToken instanceof PublicKeyToken && $dbToken->getType() === IToken::TEMPORARY_TOKEN && !$tokenFromCookie) {
// Session token but from Bearer header, not allowed
return false;
}

try {
$dbToken = $this->tokenProvider->getToken($token);
} catch (InvalidTokenException $e) {
// Can't really happen but better save than sorry
return true;
if (!$this->loginWithToken($token)) {
return false;
}
if (!$this->validateToken($token)) {
return false;
}

// Set the session variable so we know this is an app password
Expand Down
23 changes: 14 additions & 9 deletions tests/lib/User/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,16 +482,18 @@ public function testLogClientInWithTokenPassword(): void {
$manager = $this->createMock(Manager::class);
$session = $this->createMock(ISession::class);
$request = $this->createMock(IRequest::class);
$token = $this->createMock(IToken::class);

/** @var Session $userSession */
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();

$userSession->expects($this->once())
->method('isTokenPassword')
->willReturn(true);
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('I-AM-AN-APP-PASSWORD')
->willReturn($token);
$userSession->expects($this->once())
->method('login')
->with('john', 'I-AM-AN-APP-PASSWORD')
Expand Down Expand Up @@ -1231,16 +1233,18 @@ public function testLogClientInThrottlerUsername(): void {
$manager = $this->createMock(Manager::class);
$session = $this->createMock(ISession::class);
$request = $this->createMock(IRequest::class);
$token = $this->createMock(IToken::class);

/** @var Session $userSession */
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();

$userSession->expects($this->once())
->method('isTokenPassword')
->willReturn(true);
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('I-AM-AN-PASSWORD')
->willReturn($token);
$userSession->expects($this->once())
->method('login')
->with('john', 'I-AM-AN-PASSWORD')
Expand Down Expand Up @@ -1284,9 +1288,10 @@ public function testLogClientInThrottlerEmail(): void {
->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();

$userSession->expects($this->once())
->method('isTokenPassword')
->willReturn(false);
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('I-AM-AN-PASSWORD')
->willThrowException(new InvalidTokenException());
$userSession->expects($this->once())
->method('login')
->with('john@foo.bar', 'I-AM-AN-PASSWORD')
Expand Down