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
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
namespace OC\Authentication\Login;

use OC\User\Session;
use OCP\IConfig;

class FinishRememberedLoginCommand extends ALoginCommand {
public function __construct(
private Session $userSession,
private IConfig $config,
) {
}

#[\Override]
public function process(LoginData $loginData): LoginResult {
if ($loginData->isRememberLogin() && !$this->config->getSystemValueBool('auto_logout', false)) {
if ($loginData->isRememberLogin()) {
$this->userSession->createRememberMeToken($loginData->getUser());
}

Expand Down
4 changes: 4 additions & 0 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,10 @@ public function loginWithCookie($uid, $currentToken, $oldSessionId) {
* @param IUser $user
*/
public function createRememberMeToken(IUser $user) {
if ($this->config->getSystemValueBool('auto_logout', false)) {
return;
}

$token = $this->random->generate(32);
$this->config->setUserValue($user->getUID(), 'login_token', $token, (string)$this->timeFactory->getTime());
$this->setMagicInCookie($user->getUID(), $token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,20 @@

use OC\Authentication\Login\FinishRememberedLoginCommand;
use OC\User\Session;
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;

class FinishRememberedLoginCommandTest extends ALoginTestCommand {
/** @var Session|MockObject */
private $userSession;
/** @var IConfig|MockObject */
private $config;

#[\Override]
protected function setUp(): void {
parent::setUp();

$this->userSession = $this->createMock(Session::class);
$this->config = $this->createMock(IConfig::class);

$this->cmd = new FinishRememberedLoginCommand(
$this->userSession,
$this->config
);
}

Expand All @@ -46,10 +41,6 @@ public function testProcessNotRememberedLogin(): void {

public function testProcess(): void {
$data = $this->getLoggedInLoginData();
$this->config->expects($this->once())
->method('getSystemValueBool')
->with('auto_logout', false)
->willReturn(false);
$this->userSession->expects($this->once())
->method('createRememberMeToken')
->with($this->user);
Expand All @@ -58,18 +49,4 @@ public function testProcess(): void {

$this->assertTrue($result->isSuccess());
}

public function testProcessNotRemeberedLoginWithAutologout(): void {
$data = $this->getLoggedInLoginData();
$this->config->expects($this->once())
->method('getSystemValueBool')
->with('auto_logout', false)
->willReturn(true);
$this->userSession->expects($this->never())
->method('createRememberMeToken');

$result = $this->cmd->process($data);

$this->assertTrue($result->isSuccess());
}
}
20 changes: 20 additions & 0 deletions tests/lib/User/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,26 @@ public function testCreateRememberMeToken(): void {
$this->userSession->createRememberMeToken($user);
}

public function testCreateRememberMeTokenWithAutoLogout(): void {
$user = $this->createMock(IUser::class);
$this->config
->expects($this->once())
->method('getSystemValueBool')
->with('auto_logout', false)
->willReturn(true);
$this->random
->expects($this->never())
->method('generate');
$this->config
->expects($this->never())
->method('setUserValue');
$this->userSession
->expects($this->never())
->method('setMagicInCookie');

$this->userSession->createRememberMeToken($user);
}

public static function renewMagicSessionIdData(): array {
return [
'cookie holds the old session id' => ['cookies' => ['nc_username' => 'u', 'nc_token' => 't', 'nc_session_id' => 'old'], 'expectRenewal' => true],
Expand Down
Loading