205 lines
6.5 KiB
PHP
205 lines
6.5 KiB
PHP
<?php
|
|
/**
|
|
* @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
|
|
*
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
*
|
|
* @license GNU AGPL version 3 or any later version
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
namespace Test\Security\IdentityProof;
|
|
|
|
use OC\Security\IdentityProof\Key;
|
|
use OC\Security\IdentityProof\Manager;
|
|
use OC\Security\IdentityProof\Signer;
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
use OCP\IUser;
|
|
use OCP\IUserManager;
|
|
use Test\TestCase;
|
|
|
|
class SignerTest extends TestCase {
|
|
|
|
/** @var string */
|
|
private $private = '-----BEGIN PRIVATE KEY-----
|
|
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDImc6QvHBjBIoo
|
|
w9nnywiPNESleUuFJ2yQ3Gd/3w2BkblojlUAJAsUd/bQokjOH9d+7nqyzgSiXjRl
|
|
iwKagY6NjcNEEq0X5KOMNwx6uEbtq3+7pA7H2JefNrnAuD+Fhp3Hyo3h1cse6hAq
|
|
6Zr8haCiSdFBfelLnx/X3gPgCzgl6GnvSmiqPEPFGng822dlW2RW+IIUv4y2LoIH
|
|
2PKZpxottxtFGIpcKSSHGUfNWya7Ih4E6RBgOrpyu4hrkikl4Xdh4RVgAf/GH54F
|
|
gQi/AFeRS6llXJhep3l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
|
|
pNjJ9j7BWYNF2cngq1+NSOlzc51fVyjCAhqX5cDXkXGVjPJRDWAIh0clBvcOTwnN
|
|
tAKgJhP9AS3rdvHR1szGEA2VnocWwMqfu//AowhdAoGACYwuBjfUWc21jcT5yeLZ
|
|
ahLp+ImQsKDE0swhmk4uesbLLPRfyvpLca98XbBMuS1iLrVUY3mEfIV7ltaBajE0
|
|
l3eB7suqch3WUzH1RMWzwpuUMWV/A8qjPbIrd2QYUFYxJsU88lBqRg92rPnri6Ec
|
|
kC6HCb+CXsMRD7yp8KrrYnw=
|
|
-----END PRIVATE KEY-----';
|
|
|
|
/** @var string */
|
|
private $public = '-----BEGIN PUBLIC KEY-----
|
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyJnOkLxwYwSKKMPZ58sI
|
|
jzREpXlLhSdskNxnf98NgZG5aI5VACQLFHf20KJIzh/Xfu56ss4Eol40ZYsCmoGO
|
|
jY3DRBKtF+SjjDcMerhG7at/u6QOx9iXnza5wLg/hYadx8qN4dXLHuoQKuma/IWg
|
|
oknRQX3pS58f194D4As4Jehp70poqjxDxRp4PNtnZVtkVviCFL+Mti6CB9jymaca
|
|
LbcbRRiKXCkkhxlHzVsmuyIeBOkQYDq6cruIa5IpJeF3YeEVYAH/xh+eBYEIvwBX
|
|
kUupZVyYXqd5WTrS5xXWDTyhc9Yv1LwaMlL/Ja7LzWu+hyIEjJhkCjjeIkDtD/zm
|
|
gQIDAQAB
|
|
-----END PUBLIC KEY-----';
|
|
|
|
/** @var Key */
|
|
private $key;
|
|
|
|
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
|
|
private $keyManager;
|
|
|
|
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
|
|
private $timeFactory;
|
|
|
|
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
|
|
private $userManager;
|
|
|
|
/** @var Signer */
|
|
private $signer;
|
|
|
|
public function setUp() {
|
|
parent::setUp();
|
|
|
|
$this->key = new Key($this->public, $this->private);
|
|
|
|
$this->keyManager = $this->createMock(Manager::class);
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
|
$this->userManager = $this->createMock(IUserManager::class);
|
|
|
|
$this->signer = new Signer(
|
|
$this->keyManager,
|
|
$this->timeFactory,
|
|
$this->userManager
|
|
);
|
|
}
|
|
|
|
public function testSign() {
|
|
$user = $this->createMock(IUser::class);
|
|
$user->method('getCloudId')
|
|
->willReturn('foo@example.com');
|
|
|
|
$this->timeFactory->method('getTime')
|
|
->willReturn(42);
|
|
|
|
$this->keyManager->method('getKey')
|
|
->with($this->equalTo($user))
|
|
->willReturn($this->key);
|
|
|
|
$data = [
|
|
'foo' => 'bar',
|
|
'abc' => 'def',
|
|
'xyz' => 123,
|
|
];
|
|
|
|
$expects = [
|
|
'message' => [
|
|
'data' => $data,
|
|
'type' => 'myType',
|
|
'signer' => 'foo@example.com',
|
|
'timestamp' => 42,
|
|
],
|
|
'signature' => 'E1fNdoWMX1QmSyKv+S3FtOgLe9niYGQFWOKGaMLxc2h7s3V++EIqJvw/NCLBfrUowzWkTzhkjfbHaf88Hz34WAn4sAwXYAO8cnboQs6SClKRzQ/nvbtLgd2wm9RQ8UTOM7wR6C7HpIn4qqJ4BTQ1bAwYAiJ2GoK+W8wC0o0Gpub2906j3JJ4cbc9lufd5ohWKCev8Ubem/EEKaRIZA7qHh+Q1MKXTaJQJlCjTMe5PyGy0fsmtVxsPls3/Fkd9sVeHEHSYHzOiF6ttlxWou4TdRbq3WSEVpt1DOOvkKI9w2+zBJ7IPH8CnVpXcdIzWDctUygZKzNMUQnweDOOziEdUw=='
|
|
];
|
|
|
|
$result = $this->signer->sign('myType', $data, $user);
|
|
|
|
$this->assertEquals($expects, $result);
|
|
}
|
|
|
|
public function testVerifyValid() {
|
|
$data = [
|
|
'message' => [
|
|
'data' => [
|
|
'foo' => 'bar',
|
|
'abc' => 'def',
|
|
'xyz' => 123,
|
|
],
|
|
'type' => 'myType',
|
|
'signer' => 'foo@example.com',
|
|
'timestamp' => 42,
|
|
],
|
|
'signature' => 'E1fNdoWMX1QmSyKv+S3FtOgLe9niYGQFWOKGaMLxc2h7s3V++EIqJvw/NCLBfrUowzWkTzhkjfbHaf88Hz34WAn4sAwXYAO8cnboQs6SClKRzQ/nvbtLgd2wm9RQ8UTOM7wR6C7HpIn4qqJ4BTQ1bAwYAiJ2GoK+W8wC0o0Gpub2906j3JJ4cbc9lufd5ohWKCev8Ubem/EEKaRIZA7qHh+Q1MKXTaJQJlCjTMe5PyGy0fsmtVxsPls3/Fkd9sVeHEHSYHzOiF6ttlxWou4TdRbq3WSEVpt1DOOvkKI9w2+zBJ7IPH8CnVpXcdIzWDctUygZKzNMUQnweDOOziEdUw=='
|
|
];
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$this->keyManager->method('getKey')
|
|
->with($this->equalTo($user))
|
|
->willReturn($this->key);
|
|
|
|
$this->userManager->method('get')
|
|
->with('foo')
|
|
->willReturn($user);
|
|
|
|
$this->assertTrue($this->signer->verify($data));
|
|
}
|
|
|
|
public function testVerifyInvalid() {
|
|
$data = [
|
|
'message' => [
|
|
'data' => [
|
|
'foo' => 'bar',
|
|
'abc' => 'def',
|
|
'xyz' => 123,
|
|
],
|
|
'type' => 'myType',
|
|
'signer' => 'foo@example.com',
|
|
'timestamp' => 42,
|
|
],
|
|
'signature' => 'Invalid sig'
|
|
];
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$this->keyManager->method('getKey')
|
|
->with($this->equalTo($user))
|
|
->willReturn($this->key);
|
|
|
|
$this->userManager->method('get')
|
|
->with('foo')
|
|
->willReturn($user);
|
|
|
|
$this->assertFalse($this->signer->verify($data));
|
|
}
|
|
|
|
public function testVerifyInvalidData() {
|
|
$data = [
|
|
];
|
|
|
|
$this->assertFalse($this->signer->verify($data));
|
|
}
|
|
|
|
|
|
}
|