Browse Source
Migrate personal certificate handling into AppFramework controllers
Migrate personal certificate handling into AppFramework controllers
Also added unit-tests and better error-handlingremotes/origin/handlebars-approach
10 changed files with 298 additions and 100 deletions
-
6apps/files_external/appinfo/routes.php
-
52settings/ajax/addRootCertificate.php
-
28settings/ajax/removeRootCertificate.php
-
12settings/application.php
-
93settings/controller/certificatecontroller.php
-
22settings/js/personal.js
-
2settings/personal.php
-
6settings/routes.php
-
3settings/templates/personal.php
-
174tests/settings/controller/CertificateControllerTest.php
@ -1,52 +0,0 @@ |
|||
<?php |
|||
/** |
|||
* @author Lukas Reschke <lukas@owncloud.com> |
|||
* @author Robin Appelman <icewind@owncloud.com> |
|||
* |
|||
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|||
* @license AGPL-3.0 |
|||
* |
|||
* This code is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License, version 3, |
|||
* as published by the Free Software Foundation. |
|||
* |
|||
* 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, version 3, |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|||
* |
|||
*/ |
|||
OCP\JSON::checkLoggedIn(); |
|||
OCP\JSON::callCheck(); |
|||
|
|||
$l = new OC_L10N('core'); |
|||
|
|||
if (!isset($_FILES['rootcert_import'])) { |
|||
OCP\JSON::error(array('error' => 'No certificate uploaded')); |
|||
exit; |
|||
} |
|||
|
|||
$data = file_get_contents($_FILES['rootcert_import']['tmp_name']); |
|||
$filename = basename($_FILES['rootcert_import']['name']); |
|||
|
|||
$certificateManager = \OC::$server->getCertificateManager(); |
|||
|
|||
try { |
|||
$cert = $certificateManager->addCertificate($data, $filename); |
|||
OCP\JSON::success(array( |
|||
'name' => $cert->getName(), |
|||
'commonName' => $cert->getCommonName(), |
|||
'organization' => $cert->getOrganization(), |
|||
'validFrom' => $cert->getIssueDate()->getTimestamp(), |
|||
'validTill' => $cert->getExpireDate()->getTimestamp(), |
|||
'validFromString' => $l->l('date', $cert->getIssueDate()), |
|||
'validTillString' => $l->l('date', $cert->getExpireDate()), |
|||
'issuer' => $cert->getIssuerName(), |
|||
'issuerOrganization' => $cert->getIssuerOrganization() |
|||
)); |
|||
} catch(\Exception $e) { |
|||
OCP\JSON::error(array('error' => 'Couldn\'t import SSL root certificate, allowed formats: PEM and DER')); |
|||
} |
|||
@ -1,28 +0,0 @@ |
|||
<?php |
|||
/** |
|||
* @author Björn Schießle <schiessle@owncloud.com> |
|||
* @author Lukas Reschke <lukas@owncloud.com> |
|||
* @author Robin Appelman <icewind@owncloud.com> |
|||
* |
|||
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|||
* @license AGPL-3.0 |
|||
* |
|||
* This code is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License, version 3, |
|||
* as published by the Free Software Foundation. |
|||
* |
|||
* 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, version 3, |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|||
* |
|||
*/ |
|||
OCP\JSON::checkLoggedIn(); |
|||
OCP\JSON::callCheck(); |
|||
|
|||
$name = (string)$_POST['cert']; |
|||
$certificateManager = \OC::$server->getCertificateManager(); |
|||
$certificateManager->removeCertificate($name); |
|||
@ -0,0 +1,93 @@ |
|||
<?php |
|||
/** |
|||
* @author Lukas Reschke <lukas@owncloud.com> |
|||
* |
|||
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|||
* @license AGPL-3.0 |
|||
* |
|||
* This code is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License, version 3, |
|||
* as published by the Free Software Foundation. |
|||
* |
|||
* 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, version 3, |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|||
* |
|||
*/ |
|||
|
|||
namespace OC\Settings\Controller; |
|||
|
|||
use OCP\AppFramework\Controller; |
|||
use OCP\AppFramework\Http; |
|||
use OCP\AppFramework\Http\DataResponse; |
|||
use OCP\ICertificateManager; |
|||
use OCP\IL10N; |
|||
use OCP\IRequest; |
|||
|
|||
/** |
|||
* @package OC\Settings\Controller |
|||
*/ |
|||
class CertificateController extends Controller { |
|||
/** @var ICertificateManager */ |
|||
private $certificateManager; |
|||
/** @var IL10N */ |
|||
private $l10n; |
|||
|
|||
/** |
|||
* @param string $appName |
|||
* @param IRequest $request |
|||
* @param ICertificateManager $certificateManager |
|||
* @param IL10N $l10n |
|||
*/ |
|||
public function __construct($appName, |
|||
IRequest $request, |
|||
ICertificateManager $certificateManager, |
|||
IL10N $l10n) { |
|||
parent::__construct($appName, $request); |
|||
$this->certificateManager = $certificateManager; |
|||
$this->l10n = $l10n; |
|||
} |
|||
|
|||
/** |
|||
* Add a new personal root certificate to the users' trust store |
|||
* @return array |
|||
*/ |
|||
public function addPersonalRootCertificate() { |
|||
$file = $this->request->getUploadedFile('rootcert_import'); |
|||
if(empty($file)) { |
|||
return new DataResponse(['message' => 'No file uploaded'], Http::STATUS_UNPROCESSABLE_ENTITY); |
|||
} |
|||
|
|||
try { |
|||
$certificate = $this->certificateManager->addCertificate(file_get_contents($file['tmp_name']), $file['name']); |
|||
return new DataResponse([ |
|||
'name' => $certificate->getName(), |
|||
'commonName' => $certificate->getCommonName(), |
|||
'organization' => $certificate->getOrganization(), |
|||
'validFrom' => $certificate->getIssueDate()->getTimestamp(), |
|||
'validTill' => $certificate->getExpireDate()->getTimestamp(), |
|||
'validFromString' => $this->l10n->l('date', $certificate->getIssueDate()), |
|||
'validTillString' => $this->l10n->l('date', $certificate->getExpireDate()), |
|||
'issuer' => $certificate->getIssuerName(), |
|||
'issuerOrganization' => $certificate->getIssuerOrganization(), |
|||
]); |
|||
} catch (\Exception $e) { |
|||
return new DataResponse('An error occurred.', Http::STATUS_INTERNAL_SERVER_ERROR); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Removes a personal root certificate from the users' trust store |
|||
* @param string $certificateIdentifier |
|||
* @return DataResponse |
|||
*/ |
|||
public function removePersonalRootCertificate($certificateIdentifier) { |
|||
$this->certificateManager->removeCertificate($certificateIdentifier); |
|||
return new DataResponse(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,174 @@ |
|||
<?php |
|||
/** |
|||
* @author Lukas Reschke <lukas@owncloud.com> |
|||
* |
|||
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|||
* @license AGPL-3.0 |
|||
* |
|||
* This code is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License, version 3, |
|||
* as published by the Free Software Foundation. |
|||
* |
|||
* 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, version 3, |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|||
* |
|||
*/ |
|||
|
|||
namespace OC\Settings\Controller; |
|||
|
|||
use OCP\AppFramework\Http; |
|||
use OCP\AppFramework\Http\DataResponse; |
|||
use OCP\IRequest; |
|||
use OCP\IL10N; |
|||
use OCP\ICertificateManager; |
|||
|
|||
/** |
|||
* Class CertificateControllerTest |
|||
* |
|||
* @package OC\Settings\Controller |
|||
*/ |
|||
class CertificateControllerTest extends \Test\TestCase { |
|||
/** @var CertificateController */ |
|||
private $certificateController; |
|||
/** @var IRequest */ |
|||
private $request; |
|||
/** @var ICertificateManager */ |
|||
private $certificateManager; |
|||
/** @var IL10N */ |
|||
private $l10n; |
|||
|
|||
public function setUp() { |
|||
parent::setUp(); |
|||
|
|||
$this->request = $this->getMock('\OCP\IRequest'); |
|||
$this->certificateManager = $this->getMock('\OCP\ICertificateManager'); |
|||
$this->l10n = $this->getMock('\OCP\IL10N'); |
|||
|
|||
$this->certificateController = new CertificateController( |
|||
'settings', |
|||
$this->request, |
|||
$this->certificateManager, |
|||
$this->l10n |
|||
); |
|||
} |
|||
|
|||
public function testAddPersonalRootCertificateWithEmptyFile() { |
|||
$this->request |
|||
->expects($this->once()) |
|||
->method('getUploadedFile') |
|||
->with('rootcert_import') |
|||
->will($this->returnValue(null)); |
|||
|
|||
$expected = new DataResponse(['message' => 'No file uploaded'], Http::STATUS_UNPROCESSABLE_ENTITY); |
|||
$this->assertEquals($expected, $this->certificateController->addPersonalRootCertificate()); |
|||
} |
|||
|
|||
public function testAddPersonalRootCertificateValidCertificate() { |
|||
$uploadedFile = [ |
|||
'tmp_name' => __DIR__ . '/../../data/certificates/goodCertificate.crt', |
|||
'name' => 'goodCertificate.crt', |
|||
]; |
|||
|
|||
$certificate = $this->getMock('\OCP\ICertificate'); |
|||
$certificate |
|||
->expects($this->once()) |
|||
->method('getName') |
|||
->will($this->returnValue('Name')); |
|||
$certificate |
|||
->expects($this->once()) |
|||
->method('getCommonName') |
|||
->will($this->returnValue('CommonName')); |
|||
$certificate |
|||
->expects($this->once()) |
|||
->method('getOrganization') |
|||
->will($this->returnValue('Organization')); |
|||
$certificate |
|||
->expects($this->exactly(2)) |
|||
->method('getIssueDate') |
|||
->will($this->returnValue(new \DateTime('@1429099555'))); |
|||
$certificate |
|||
->expects($this->exactly(2)) |
|||
->method('getExpireDate') |
|||
->will($this->returnValue(new \DateTime('@1529099555'))); |
|||
$certificate |
|||
->expects($this->once()) |
|||
->method('getIssuerName') |
|||
->will($this->returnValue('Issuer')); |
|||
$certificate |
|||
->expects($this->once()) |
|||
->method('getIssuerOrganization') |
|||
->will($this->returnValue('IssuerOrganization')); |
|||
|
|||
$this->request |
|||
->expects($this->once()) |
|||
->method('getUploadedFile') |
|||
->with('rootcert_import') |
|||
->will($this->returnValue($uploadedFile)); |
|||
$this->certificateManager |
|||
->expects($this->once()) |
|||
->method('addCertificate') |
|||
->with(file_get_contents($uploadedFile['tmp_name'], 'goodCertificate.crt')) |
|||
->will($this->returnValue($certificate)); |
|||
|
|||
$this->l10n |
|||
->expects($this->at(0)) |
|||
->method('l') |
|||
->with('date', new \DateTime('@1429099555')) |
|||
->will($this->returnValue('Valid From as String')); |
|||
$this->l10n |
|||
->expects($this->at(1)) |
|||
->method('l') |
|||
->with('date', new \DateTime('@1529099555')) |
|||
->will($this->returnValue('Valid Till as String')); |
|||
|
|||
|
|||
$expected = new DataResponse([ |
|||
'name' => 'Name', |
|||
'commonName' => 'CommonName', |
|||
'organization' => 'Organization', |
|||
'validFrom' => 1429099555, |
|||
'validTill' => 1529099555, |
|||
'validFromString' => 'Valid From as String', |
|||
'validTillString' => 'Valid Till as String', |
|||
'issuer' => 'Issuer', |
|||
'issuerOrganization' => 'IssuerOrganization', |
|||
]); |
|||
$this->assertEquals($expected, $this->certificateController->addPersonalRootCertificate()); |
|||
} |
|||
|
|||
public function testAddPersonalRootCertificateInValidCertificate() { |
|||
$uploadedFile = [ |
|||
'tmp_name' => __DIR__ . '/../../data/certificates/badCertificate.crt', |
|||
'name' => 'badCertificate.crt', |
|||
]; |
|||
|
|||
$this->request |
|||
->expects($this->once()) |
|||
->method('getUploadedFile') |
|||
->with('rootcert_import') |
|||
->will($this->returnValue($uploadedFile)); |
|||
$this->certificateManager |
|||
->expects($this->once()) |
|||
->method('addCertificate') |
|||
->with(file_get_contents($uploadedFile['tmp_name'], 'goodCertificate.crt')) |
|||
->will($this->throwException(new \Exception())); |
|||
|
|||
$expected = new DataResponse('An error occurred.', Http::STATUS_INTERNAL_SERVER_ERROR); |
|||
$this->assertEquals($expected, $this->certificateController->addPersonalRootCertificate()); |
|||
} |
|||
|
|||
public function testRemoveCertificate() { |
|||
$this->certificateManager |
|||
->expects($this->once()) |
|||
->method('removeCertificate') |
|||
->with('CertificateToRemove'); |
|||
|
|||
$this->assertEquals(new DataResponse(), $this->certificateController->removePersonalRootCertificate('CertificateToRemove')); |
|||
} |
|||
|
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue