Skip to content
Merged
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
2 changes: 2 additions & 0 deletions lib/private/Session/Internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use OC\Authentication\Token\IProvider;
use OC\Diagnostics\TLogSlowOperation;
use OC\User\Session as UserSession;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Server;
use OCP\Session\Exceptions\SessionNotAvailableException;
Expand Down Expand Up @@ -149,6 +150,7 @@ public function regenerateId(bool $deleteOldSession = true, bool $updateToken =

try {
$tokenProvider->renewSessionToken($oldId, $newId);
Server::get(UserSession::class)->renewMagicSessionId($oldId);
} catch (InvalidTokenException $e) {
// Just ignore
}
Expand Down
15 changes: 15 additions & 0 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,21 @@ public function setMagicInCookie($username, $token) {
}
}

/**
* Point the remember-me cookie at the regenerated session id, so cookie
* login can still find the token that was renewed along with it.
*/
public function renewMagicSessionId(string $oldSessionId): void {
$request = Server::get(IRequest::class);
$username = $request->getCookie('nc_username');
$token = $request->getCookie('nc_token');
$sessionId = $request->getCookie('nc_session_id');
if ($username === null || $token === null || $sessionId !== $oldSessionId) {
return;
}
$this->setMagicInCookie($username, $token);
}

/**
* Remove cookie for "remember username"
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/private/legacy/OC_User.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static function loginWithApache(IApacheBackend $backend): bool {

$dispatcher->dispatchTyped(new BeforeUserLoggedInEvent($uid, $password, $backend));

$userSession->createSessionToken($request, $uid, $uid, $password);
$userSession->createSessionToken($request, $uid, $uid, $password, IToken::REMEMBER);
$userSession->completeLogin(
$user,
['loginName' => $uid, 'password' => $password ?? ''],
Expand Down
21 changes: 21 additions & 0 deletions tests/lib/User/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,27 @@ public function testCreateRememberMeToken(): void {
$this->userSession->createRememberMeToken($user);
}

public static function renewMagicSessionIdData(): array {
return [
'cookie holds the old session id' => [['nc_username' => 'u', 'nc_token' => 't', 'nc_session_id' => 'old'], true],
'cookie holds another session id' => [['nc_username' => 'u', 'nc_token' => 't', 'nc_session_id' => 'other'], false],
'no remember-me cookies' => [[], false],
];
}

#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'renewMagicSessionIdData')]
public function testRenewMagicSessionId(array $cookies, bool $expectRenewal): void {
$this->userSession->expects($expectRenewal ? $this->once() : $this->never())
->method('setMagicInCookie')
->with('u', 't');

$request = $this->createMock(IRequest::class);
$request->method('getCookie')->willReturnCallback(fn (string $key) => $cookies[$key] ?? null);
$this->overwriteService(IRequest::class, $request);

$this->userSession->renewMagicSessionId('old');
}

public function testTryBasicAuthLoginValid(): void {
$request = $this->createMock(Request::class);
$request->method('__get')
Expand Down
Loading