Skip to content
Draft
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: 1 addition & 1 deletion apps/settings/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace OCA\Settings\AppInfo;

use OC\AppFramework\Utility\TimeFactory;
use OC\Authentication\Events\AppPasswordCreatedEvent;
use OC\Authentication\Token\IProvider;
use OC\Settings\Manager;
use OCA\Settings\ConfigLexicon;
Expand Down Expand Up @@ -89,6 +88,7 @@
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Authentication\Events\AppPasswordCreatedEvent;
use OCP\Defaults;
use OCP\Group\Events\GroupDeletedEvent;
use OCP\Group\Events\UserAddedEvent;
Expand Down
5 changes: 4 additions & 1 deletion apps/settings/lib/Controller/AuthSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Services\IAppConfig;
use OCP\Authentication\Events\AppPasswordCreatedEvent;
use OCP\Authentication\Exceptions\ExpiredTokenException;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\Exceptions\WipeTokenException;
use OCP\Authentication\Token\IToken;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
Expand All @@ -54,6 +56,7 @@ public function __construct(
private IConfig $serverConfig,
private IL10N $l,
private TokenScopes $tokenScopes,
private IEventDispatcher $eventDispatcher,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -132,7 +135,7 @@ public function create(string $name = '', bool $qrcodeLogin = false): JSONRespon
$tokenData['canDelete'] = true;
$tokenData['canRename'] = true;

$this->publishActivity(Provider::APP_TOKEN_CREATED, $deviceToken->getId(), ['name' => $deviceToken->getName()]);
$this->eventDispatcher->dispatchTyped(new AppPasswordCreatedEvent($deviceToken));

return new JSONResponse([
'token' => $token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
namespace OCA\Settings\Listener;

use BadMethodCallException;
use OC\Authentication\Events\AppPasswordCreatedEvent;
use OCA\Settings\Activity\Provider;
use OCP\Activity\IManager as IActivityManager;
use OCP\Authentication\Events\AppPasswordCreatedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IUserSession;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Services\IAppConfig;
use OCP\Authentication\Events\AppPasswordCreatedEvent;
use OCP\Authentication\Exceptions\WipeTokenException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
Expand All @@ -48,6 +50,7 @@ class AuthSettingsControllerTest extends TestCase {
private RemoteWipe&MockObject $remoteWipe;
private IConfig&MockObject $serverConfig;
private IL10N&MockObject $l;
private IEventDispatcher&MockObject $eventDispatcher;
private string $uid = 'jane';
private AuthSettingsController $controller;

Expand All @@ -66,6 +69,7 @@ protected function setUp(): void {
/** @var LoggerInterface&MockObject $logger */
$logger = $this->createMock(LoggerInterface::class);
$this->l = $this->createMock(IL10N::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);

$this->controller = new AuthSettingsController(
'core',
Expand All @@ -82,6 +86,7 @@ protected function setUp(): void {
$this->serverConfig,
$this->l,
new TokenScopes(),
$this->eventDispatcher,
);
}

Expand Down Expand Up @@ -124,7 +129,9 @@ public function testCreate(): void {
->method('jsonSerialize')
->willReturn(['dummy' => 'dummy', 'canDelete' => true]);

$this->mockActivityManager();
$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with($this->callback(static fn (AppPasswordCreatedEvent $event): bool => $event->getToken() === $deviceToken));

$expected = [
'token' => $newToken,
Expand Down
2 changes: 1 addition & 1 deletion core/Command/User/AuthTokens/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

namespace OC\Core\Command\User\AuthTokens;

use OC\Authentication\Events\AppPasswordCreatedEvent;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OC\User\Manager as UserManager;
use OCP\Authentication\Events\AppPasswordCreatedEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Security\ISecureRandom;
use Symfony\Component\Console\Command\Command;
Expand Down
2 changes: 1 addition & 1 deletion core/Controller/AppPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace OC\Core\Controller;

use OC\Authentication\Events\AppPasswordCreatedEvent;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OC\User\Session;
Expand All @@ -23,6 +22,7 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCSController;
use OCP\Authentication\Events\AppPasswordCreatedEvent;
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\Exceptions\PasswordUnavailableException;
Expand Down
2 changes: 1 addition & 1 deletion core/Controller/ClientFlowLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace OC\Core\Controller;

use OC\Authentication\Events\AppPasswordCreatedEvent;
use OC\Authentication\Exceptions\PasswordlessTokenException;
use OC\Authentication\Token\IProvider;
use OCA\OAuth2\Db\AccessToken;
Expand All @@ -29,6 +28,7 @@
use OCP\AppFramework\Http\StandaloneTemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\Events\AppPasswordCreatedEvent;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\Token\IToken;
use OCP\Defaults;
Expand Down
2 changes: 2 additions & 0 deletions core/Service/LoginFlowV2Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use OC\Core\Exception\LoginFlowV2NotFoundException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\Events\AppPasswordCreatedEvent;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
Expand Down Expand Up @@ -179,6 +180,7 @@ public function flowDone(string $loginToken, string $sessionId, string $server,
$data->setAppPassword($this->encryptPassword($appPassword, $data->getPublicKey()));

$this->mapper->update($data);
$this->eventDispatcher->dispatchTyped(new AppPasswordCreatedEvent($generatedToken));
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
'OCP\\App\\IAppManager' => $baseDir . '/lib/public/App/IAppManager.php',
'OCP\\App\\ManagerEvent' => $baseDir . '/lib/public/App/ManagerEvent.php',
'OCP\\Authentication\\Events\\AnyLoginFailedEvent' => $baseDir . '/lib/public/Authentication/Events/AnyLoginFailedEvent.php',
'OCP\\Authentication\\Events\\AppPasswordCreatedEvent' => $baseDir . '/lib/public/Authentication/Events/AppPasswordCreatedEvent.php',
'OCP\\Authentication\\Events\\LoginFailedEvent' => $baseDir . '/lib/public/Authentication/Events/LoginFailedEvent.php',
'OCP\\Authentication\\Events\\TokenInvalidatedEvent' => $baseDir . '/lib/public/Authentication/Events/TokenInvalidatedEvent.php',
'OCP\\Authentication\\Exceptions\\CredentialsUnavailableException' => $baseDir . '/lib/public/Authentication/Exceptions/CredentialsUnavailableException.php',
Expand Down Expand Up @@ -1300,7 +1301,6 @@
'OC\\Archive\\TAR' => $baseDir . '/lib/private/Archive/TAR.php',
'OC\\Archive\\ZIP' => $baseDir . '/lib/private/Archive/ZIP.php',
'OC\\Authentication\\Events\\ARemoteWipeEvent' => $baseDir . '/lib/private/Authentication/Events/ARemoteWipeEvent.php',
'OC\\Authentication\\Events\\AppPasswordCreatedEvent' => $baseDir . '/lib/private/Authentication/Events/AppPasswordCreatedEvent.php',
'OC\\Authentication\\Events\\LoginFailed' => $baseDir . '/lib/private/Authentication/Events/LoginFailed.php',
'OC\\Authentication\\Events\\RemoteWipeFinished' => $baseDir . '/lib/private/Authentication/Events/RemoteWipeFinished.php',
'OC\\Authentication\\Events\\RemoteWipeStarted' => $baseDir . '/lib/private/Authentication/Events/RemoteWipeStarted.php',
Expand Down
2 changes: 1 addition & 1 deletion lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\App\\IAppManager' => __DIR__ . '/../../..' . '/lib/public/App/IAppManager.php',
'OCP\\App\\ManagerEvent' => __DIR__ . '/../../..' . '/lib/public/App/ManagerEvent.php',
'OCP\\Authentication\\Events\\AnyLoginFailedEvent' => __DIR__ . '/../../..' . '/lib/public/Authentication/Events/AnyLoginFailedEvent.php',
'OCP\\Authentication\\Events\\AppPasswordCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Authentication/Events/AppPasswordCreatedEvent.php',
'OCP\\Authentication\\Events\\LoginFailedEvent' => __DIR__ . '/../../..' . '/lib/public/Authentication/Events/LoginFailedEvent.php',
'OCP\\Authentication\\Events\\TokenInvalidatedEvent' => __DIR__ . '/../../..' . '/lib/public/Authentication/Events/TokenInvalidatedEvent.php',
'OCP\\Authentication\\Exceptions\\CredentialsUnavailableException' => __DIR__ . '/../../..' . '/lib/public/Authentication/Exceptions/CredentialsUnavailableException.php',
Expand Down Expand Up @@ -1341,7 +1342,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Archive\\TAR' => __DIR__ . '/../../..' . '/lib/private/Archive/TAR.php',
'OC\\Archive\\ZIP' => __DIR__ . '/../../..' . '/lib/private/Archive/ZIP.php',
'OC\\Authentication\\Events\\ARemoteWipeEvent' => __DIR__ . '/../../..' . '/lib/private/Authentication/Events/ARemoteWipeEvent.php',
'OC\\Authentication\\Events\\AppPasswordCreatedEvent' => __DIR__ . '/../../..' . '/lib/private/Authentication/Events/AppPasswordCreatedEvent.php',
'OC\\Authentication\\Events\\LoginFailed' => __DIR__ . '/../../..' . '/lib/private/Authentication/Events/LoginFailed.php',
'OC\\Authentication\\Events\\RemoteWipeFinished' => __DIR__ . '/../../..' . '/lib/private/Authentication/Events/RemoteWipeFinished.php',
'OC\\Authentication\\Events\\RemoteWipeStarted' => __DIR__ . '/../../..' . '/lib/private/Authentication/Events/RemoteWipeStarted.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Authentication\Events;
namespace OCP\Authentication\Events;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

globalsiteselector app is listening to that event.
I think we should proactively make it react to both, and still continue with the approach of replacing the event here instead of dispatching both or doing other shenanigans


use OCP\Authentication\Token\IToken;
use OCP\EventDispatcher\Event;

/**
* Dispatched after an app password was created, from any place that creates one
*
* @since 36.0.0
*/
class AppPasswordCreatedEvent extends Event {
/**
* @since 36.0.0
*/
public function __construct(
private IToken $token,
) {
parent::__construct();
}

/**
* @since 36.0.0
*/
public function getToken(): IToken {
return $this->token;
}
Expand Down
15 changes: 12 additions & 3 deletions tests/Core/Service/LoginFlowV2ServiceUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use OC\Core\Service\LoginFlowV2Service;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\Events\AppPasswordCreatedEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\Log\Audit\CriticalActionPerformedEvent;
Expand Down Expand Up @@ -392,10 +393,12 @@ public function testFlowDone(): void {
)
->willReturn($appToken);

$this->eventDispatcher->expects($this->once())
$events = [];
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatchTyped')
->with($this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getOperation() === 'auth.app_password.created'
&& $event->getParameters() === ['tokenId' => 7, 'tokenName' => 'client_name']));
->willReturnCallback(static function (object $event) use (&$events): void {
$events[] = $event;
});

$result = $this->subjectUnderTest->flowDone(
'login_token',
Expand All @@ -405,6 +408,12 @@ public function testFlowDone(): void {
);
$this->assertTrue($result);

$this->assertInstanceOf(CriticalActionPerformedEvent::class, $events[0]);
$this->assertSame('auth.app_password.created', $events[0]->getOperation());
$this->assertSame(['tokenId' => 7, 'tokenName' => 'client_name'], $events[0]->getParameters());
$this->assertInstanceOf(AppPasswordCreatedEvent::class, $events[1]);
$this->assertSame($appToken, $events[1]->getToken());

// app password is encrypted and must look like:
// ZACZOOzxTpKz4+KXL5kZ/gCK0xvkaVi/8yzupAn6Ui6+5qCSKvfPKGgeDRKs0sivvSLzk/XSp811SZCZmH0Y3g==
$this->assertMatchesRegularExpression('/[a-zA-Z\/0-9+=]+/', $loginFlowV2->getAppPassword());
Expand Down
Loading