Publish activity for app token created by ocs api

Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
This commit is contained in:
Daniel Kesselberg 2019-02-17 22:45:05 +01:00
parent cb3379e97d
commit 2ade2bef8c
No known key found for this signature in database
GPG Key ID: 36E3664E099D0614
2 changed files with 55 additions and 3 deletions

View File

@ -24,13 +24,17 @@ declare(strict_types=1);
namespace OC\Core\Controller;
use BadMethodCallException;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OC\Settings\Activity\Provider;
use OCP\Activity\IManager as IActivityManager;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
use OCP\Authentication\Exceptions\PasswordUnavailableException;
use OCP\Authentication\LoginCredentials\IStore;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ISession;
use OCP\Security\ISecureRandom;
@ -46,6 +50,12 @@ class AppPasswordController extends \OCP\AppFramework\OCSController {
/** @var IProvider */
private $tokenProvider;
/** @var IActivityManager */
private $activityManager;
/** @var ILogger */
private $logger;
/** @var IStore */
private $credentialStore;
@ -54,13 +64,17 @@ class AppPasswordController extends \OCP\AppFramework\OCSController {
ISession $session,
ISecureRandom $random,
IProvider $tokenProvider,
IStore $credentialStore) {
IStore $credentialStore,
IActivityManager $activityManager,
ILogger $logger) {
parent::__construct($appName, $request);
$this->session = $session;
$this->random = $random;
$this->tokenProvider = $tokenProvider;
$this->credentialStore = $credentialStore;
$this->activityManager = $activityManager;
$this->logger = $logger;
}
/**
@ -91,7 +105,7 @@ class AppPasswordController extends \OCP\AppFramework\OCSController {
$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
$this->tokenProvider->generateToken(
$generatedToken = $this->tokenProvider->generateToken(
$token,
$credentials->getUID(),
$credentials->getLoginName(),
@ -101,6 +115,21 @@ class AppPasswordController extends \OCP\AppFramework\OCSController {
IToken::DO_NOT_REMEMBER
);
$event = $this->activityManager->generateEvent();
$event->setApp('settings')
->setType('security')
->setAffectedUser($credentials->getUID())
->setAuthor($credentials->getUID())
->setSubject(Provider::APP_TOKEN_CREATED, ['name' => $generatedToken->getName()])
->setObject('app_token', $generatedToken->getId(), 'App Password');
try {
$this->activityManager->publish($event);
} catch (BadMethodCallException $e) {
$this->logger->warning('could not publish activity');
$this->logger->logException($e);
}
return new DataResponse([
'apppassword' => $token
]);

View File

@ -27,11 +27,14 @@ namespace Tests\Core\Controller;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OC\Core\Controller\AppPasswordController;
use OCP\Activity\IEvent;
use OCP\Activity\IManager as IActivityManager;
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\ILogger;
use OCP\IRequest;
use OCP\ISession;
use OCP\Security\ISecureRandom;
@ -55,6 +58,9 @@ class AppPasswordControllerTest extends TestCase {
/** @var IRequest|MockObject */
private $request;
/** @var IActivityManager|\PHPUnit_Framework_MockObject_MockObject */
private $activityManager;
/** @var AppPasswordController */
private $controller;
@ -66,6 +72,9 @@ class AppPasswordControllerTest extends TestCase {
$this->tokenProvider = $this->createMock(IProvider::class);
$this->credentialStore = $this->createMock(IStore::class);
$this->request = $this->createMock(IRequest::class);
$this->activityManager = $this->createMock(IActivityManager::class);
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject $logger */
$logger = $this->createMock(ILogger::class);
$this->controller = new AppPasswordController(
'core',
@ -73,7 +82,9 @@ class AppPasswordControllerTest extends TestCase {
$this->session,
$this->random,
$this->tokenProvider,
$this->credentialStore
$this->credentialStore,
$this->activityManager,
$logger
);
}
@ -134,6 +145,12 @@ class AppPasswordControllerTest extends TestCase {
IToken::DO_NOT_REMEMBER
);
$this->activityManager->expects($this->once())
->method('generateEvent')
->willReturn($this->createMock(IEvent::class));
$this->activityManager->expects($this->once())
->method('publish');
$this->controller->getAppPassword();
}
@ -172,6 +189,12 @@ class AppPasswordControllerTest extends TestCase {
IToken::DO_NOT_REMEMBER
);
$this->activityManager->expects($this->once())
->method('generateEvent')
->willReturn($this->createMock(IEvent::class));
$this->activityManager->expects($this->once())
->method('publish');
$this->controller->getAppPassword();
}