Automatic creation of Identity manager

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2017-05-10 09:44:28 +02:00
parent a8bb4a1898
commit ca39940614
No known key found for this signature in database
GPG Key ID: E166FD8976B3BAC8
4 changed files with 19 additions and 14 deletions

View File

@ -30,7 +30,6 @@
namespace OC\Core; namespace OC\Core;
use OC\Security\IdentityProof\Manager;
use OCP\AppFramework\App; use OCP\AppFramework\App;
use OCP\Util; use OCP\Util;
@ -49,11 +48,5 @@ class Application extends App {
$container->registerService('defaultMailAddress', function () { $container->registerService('defaultMailAddress', function () {
return Util::getDefaultEmailAddress('lostpassword-noreply'); return Util::getDefaultEmailAddress('lostpassword-noreply');
}); });
$container->registerService(Manager::class, function () {
return new Manager(
\OC::$server->getAppDataDir('identityproof'),
\OC::$server->getCrypto()
);
});
} }
} }

View File

@ -165,7 +165,7 @@ class DIContainer extends SimpleContainer implements IAppContainer {
$this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) { $this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
return new \OC\Security\IdentityProof\Manager( return new \OC\Security\IdentityProof\Manager(
$this->getServer()->getAppDataDir('identityproof'), $this->getServer()->query(\OC\Files\AppData\Factory::class),
$this->getServer()->getCrypto() $this->getServer()->getCrypto()
); );
}); });

View File

@ -21,6 +21,7 @@
namespace OC\Security\IdentityProof; namespace OC\Security\IdentityProof;
use OC\Files\AppData\Factory;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\IUser; use OCP\IUser;
use OCP\Security\ICrypto; use OCP\Security\ICrypto;
@ -32,12 +33,12 @@ class Manager {
private $crypto; private $crypto;
/** /**
* @param IAppData $appData * @param Factory $appDataFactory
* @param ICrypto $crypto * @param ICrypto $crypto
*/ */
public function __construct(IAppData $appData, public function __construct(Factory $appDataFactory,
ICrypto $crypto) { ICrypto $crypto) {
$this->appData = $appData; $this->appData = $appDataFactory->get('identityproof');
$this->crypto = $crypto; $this->crypto = $crypto;
} }

View File

@ -21,6 +21,7 @@
namespace Test\Security\IdentityProof; namespace Test\Security\IdentityProof;
use OC\Files\AppData\Factory;
use OC\Security\IdentityProof\Key; use OC\Security\IdentityProof\Key;
use OC\Security\IdentityProof\Manager; use OC\Security\IdentityProof\Manager;
use OCP\Files\IAppData; use OCP\Files\IAppData;
@ -31,6 +32,8 @@ use OCP\Security\ICrypto;
use Test\TestCase; use Test\TestCase;
class ManagerTest extends TestCase { class ManagerTest extends TestCase {
/** @var Factory|\PHPUnit_Framework_MockObject_MockObject */
private $factory;
/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */ /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
private $appData; private $appData;
/** @var ICrypto|\PHPUnit_Framework_MockObject_MockObject */ /** @var ICrypto|\PHPUnit_Framework_MockObject_MockObject */
@ -40,11 +43,19 @@ class ManagerTest extends TestCase {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
/** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
$this->factory = $this->createMock(Factory::class);
$this->appData = $this->createMock(IAppData::class); $this->appData = $this->createMock(IAppData::class);
$this->factory->expects($this->any())
->method('get')
->with('identityproof')
->willReturn($this->appData);
$this->crypto = $this->createMock(ICrypto::class); $this->crypto = $this->createMock(ICrypto::class);
$this->manager = $this->getMockBuilder(Manager::class) $this->manager = $this->getMockBuilder(Manager::class)
->setConstructorArgs([ ->setConstructorArgs([
$this->appData, $this->factory,
$this->crypto $this->crypto
]) ])
->setMethods(['generateKeyPair']) ->setMethods(['generateKeyPair'])
@ -151,12 +162,12 @@ class ManagerTest extends TestCase {
public function testGenerateKeyPair() { public function testGenerateKeyPair() {
$manager = new Manager( $manager = new Manager(
$this->appData, $this->factory,
$this->crypto $this->crypto
); );
$data = 'MyTestData'; $data = 'MyTestData';
list($resultPublicKey, $resultPrivateKey) = $this->invokePrivate($manager, 'generateKeyPair'); list($resultPublicKey, $resultPrivateKey) = self::invokePrivate($manager, 'generateKeyPair');
openssl_sign($data, $signature, $resultPrivateKey); openssl_sign($data, $signature, $resultPrivateKey);
$details = openssl_pkey_get_details(openssl_pkey_get_public($resultPublicKey)); $details = openssl_pkey_get_details(openssl_pkey_get_public($resultPublicKey));