Files
Ghassen kefi d052c4f17d feat(core): log app passwords, password reset requests and finished remote wipes
App passwords created through login flow v1 and v2 and the app
password API, and deleting an app password through the API, are now
written to the audit log with the same operations the settings app
uses (auth.app_password.created and auth.app_password.deleted).

A password reset request is logged with the resolved user ID once the
reset token has been created, and a finished remote wipe is logged
when the device confirms it.

The token and the password are never logged.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ghassen kefi <ghassen.kefi.dev@gmail.com>
2026-10-15 12:24:12 +02:00

352 lines
10 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Tests\Core\Controller;
use OC\Authentication\Events\AppPasswordCreatedEvent;
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OC\Core\Controller\AppPasswordController;
use OC\User\Session;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
use OCP\Authentication\Exceptions\PasswordUnavailableException;
use OCP\Authentication\LoginCredentials\ICredentials;
use OCP\Authentication\LoginCredentials\IStore;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUserManager;
use OCP\Log\Audit\CriticalActionPerformedEvent;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Security\ISecureRandom;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class AppPasswordControllerTest extends TestCase {
/** @var ISession|MockObject */
private $session;
/** @var ISecureRandom|MockObject */
private $random;
/** @var IProvider|MockObject */
private $tokenProvider;
/** @var IStore|MockObject */
private $credentialStore;
/** @var IRequest|MockObject */
private $request;
/** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
private $eventDispatcher;
/** @var Session|MockObject */
private $userSession;
/** @var IUserManager|MockObject */
private $userManager;
/** @var IThrottler|MockObject */
private $throttler;
private IConfig&MockObject $serverConfig;
/** @var AppPasswordController */
private $controller;
#[\Override]
protected function setUp(): void {
parent::setUp();
$this->session = $this->createMock(ISession::class);
$this->random = $this->createMock(ISecureRandom::class);
$this->tokenProvider = $this->createMock(IProvider::class);
$this->credentialStore = $this->createMock(IStore::class);
$this->request = $this->createMock(IRequest::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->userSession = $this->createMock(Session::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->throttler = $this->createMock(IThrottler::class);
$this->serverConfig = $this->createMock(IConfig::class);
$this->controller = new AppPasswordController(
'core',
$this->request,
$this->session,
$this->random,
$this->tokenProvider,
$this->credentialStore,
$this->eventDispatcher,
$this->userSession,
$this->userManager,
$this->throttler,
$this->serverConfig,
);
}
public function testGetAppPasswordWithAppPassword(): void {
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(true);
$this->session->method('exists')
->with('app_password')
->willReturn(true);
$this->tokenProvider->expects($this->never())
->method('generateToken');
$this->eventDispatcher->expects($this->never())
->method('dispatchTyped');
$this->expectException(OCSForbiddenException::class);
$this->controller->getAppPassword();
}
public function testGetAppPasswordNoLoginCreds(): void {
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(true);
$this->session->method('exists')
->with('app_password')
->willReturn(false);
$this->credentialStore->method('getLoginCredentials')
->willThrowException(new CredentialsUnavailableException());
$this->tokenProvider->expects($this->never())
->method('generateToken');
$this->eventDispatcher->expects($this->never())
->method('dispatchTyped');
$this->expectException(OCSForbiddenException::class);
$this->controller->getAppPassword();
}
public function testGetAppPassword(): void {
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(true);
$credentials = $this->createMock(ICredentials::class);
$this->session->method('exists')
->with('app_password')
->willReturn(false);
$this->credentialStore->method('getLoginCredentials')
->willReturn($credentials);
$credentials->method('getUid')
->willReturn('myUID');
$credentials->method('getPassword')
->willReturn('myPassword');
$credentials->method('getLoginName')
->willReturn('myLoginName');
$this->request->method('getHeader')
->with('user-agent')
->willReturn('myUA');
$this->random->method('generate')
->with(
72,
ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS
)->willReturn('myToken');
$this->tokenProvider->expects($this->once())
->method('generateToken')
->with(
'myToken',
'myUID',
'myLoginName',
'myPassword',
'myUA',
IToken::PERMANENT_TOKEN,
IToken::DO_NOT_REMEMBER
);
$this->expectAppPasswordCreatedEvents();
$this->controller->getAppPassword();
}
public function testGetAppPasswordNoPassword(): void {
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(true);
$credentials = $this->createMock(ICredentials::class);
$this->session->method('exists')
->with('app_password')
->willReturn(false);
$this->credentialStore->method('getLoginCredentials')
->willReturn($credentials);
$credentials->method('getUid')
->willReturn('myUID');
$credentials->method('getPassword')
->willThrowException(new PasswordUnavailableException());
$credentials->method('getLoginName')
->willReturn('myLoginName');
$this->request->method('getHeader')
->with('user-agent')
->willReturn('myUA');
$this->random->method('generate')
->with(
72,
ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS
)->willReturn('myToken');
$this->tokenProvider->expects($this->once())
->method('generateToken')
->with(
'myToken',
'myUID',
'myLoginName',
null,
'myUA',
IToken::PERMANENT_TOKEN,
IToken::DO_NOT_REMEMBER
);
$this->expectAppPasswordCreatedEvents();
$this->controller->getAppPassword();
}
public function testGetAppPasswordDisabledBySystemConfig(): void {
$this->serverConfig->method('getSystemValueBool')
->with('auth_can_create_app_token', true)
->willReturn(false);
$this->tokenProvider->expects($this->never())
->method('generateToken');
$this->eventDispatcher->expects($this->never())
->method('dispatchTyped');
$this->expectException(OCSForbiddenException::class);
$this->controller->getAppPassword();
}
public function testDeleteAppPasswordNoAppPassword(): void {
$this->session->method('exists')
->with('app_password')
->willReturn(false);
$this->expectException(OCSForbiddenException::class);
$this->controller->deleteAppPassword();
}
public function testDeleteAppPasswordFails(): void {
$this->session->method('exists')
->with('app_password')
->willReturn(true);
$this->session->method('get')
->with('app_password')
->willReturn('myAppPassword');
$this->tokenProvider->method('getToken')
->with('myAppPassword')
->willThrowException(new InvalidTokenException());
$this->expectException(OCSForbiddenException::class);
$this->controller->deleteAppPassword();
}
public function testDeleteAppPasswordSuccess(): void {
$this->session->method('exists')
->with('app_password')
->willReturn(true);
$this->session->method('get')
->with('app_password')
->willReturn('myAppPassword');
$token = $this->createMock(IToken::class);
$this->tokenProvider->method('getToken')
->with('myAppPassword')
->willReturn($token);
$token->method('getUID')
->willReturn('myUID');
$token->method('getId')
->willReturn(42);
$this->tokenProvider->expects($this->once())
->method('invalidateTokenById')
->with(
'myUID',
42
);
$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with($this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getOperation() === 'auth.app_password.deleted'
&& $event->getParameters() === ['tokenId' => 42]
&& $event->getLogMessage() === 'App password with ID "%s" was deleted'));
$result = $this->controller->deleteAppPassword();
$this->assertEquals(new DataResponse(), $result);
}
public function testGetAppPasswordWithOneTimePassword(): void {
$credentials = $this->createMock(ICredentials::class);
$this->session->method('exists')
->with('one_time_token')
->willReturn(true);
$this->credentialStore->method('getLoginCredentials')
->willReturn($credentials);
$credentials->method('getUid')
->willReturn('myUID');
$credentials->method('getPassword')
->willReturn('myPassword');
$credentials->method('getLoginName')
->willReturn('myLoginName');
$this->request->method('getHeader')
->with('user-agent')
->willReturn('myUA');
$this->random->method('generate')
->willReturn('myToken');
$this->tokenProvider->expects($this->once())
->method('generateToken')
->with('myToken', 'myUID', 'myLoginName', 'myPassword', 'myUA', IToken::PERMANENT_TOKEN);
$this->expectAppPasswordCreatedEvents();
$this->assertEquals(new DataResponse(['apppassword' => 'myToken']), $this->controller->getAppPasswordWithOneTimePassword());
}
/**
* Expects the AppPasswordCreatedEvent followed by its audit log entry.
*/
private function expectAppPasswordCreatedEvents(): void {
$calls = 0;
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatchTyped')
->willReturnCallback(function (Event $event) use (&$calls): void {
if (++$calls === 1) {
$this->assertInstanceOf(AppPasswordCreatedEvent::class, $event);
return;
}
$this->assertInstanceOf(CriticalActionPerformedEvent::class, $event);
$this->assertSame('auth.app_password.created', $event->getOperation());
$this->assertSame('App password with ID "%s" and name "%s" was created', $event->getLogMessage());
$this->assertSame(['tokenId', 'tokenName'], array_keys($event->getParameters()));
});
}
}