2016-09-09 23:36:35 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Nextcloud - passman
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Sander Brand <brantje@gmail.com>
|
|
|
|
* @copyright Sander Brand 2016
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Passman\Controller;
|
|
|
|
|
2017-02-20 07:01:01 +08:00
|
|
|
use OCA\Passman\Service\DeleteVaultRequestService;
|
2017-01-02 22:25:41 +08:00
|
|
|
use OCA\Passman\Service\EncryptService;
|
|
|
|
use OCA\Passman\Service\SettingsService;
|
2016-10-07 23:04:26 +08:00
|
|
|
use OCA\Passman\Utility\NotFoundJSONResponse;
|
2016-10-07 18:58:41 +08:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
2016-09-09 23:36:35 +08:00
|
|
|
use OCP\IRequest;
|
2016-09-11 05:30:17 +08:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2016-09-10 00:02:53 +08:00
|
|
|
use OCP\AppFramework\ApiController;
|
2016-09-11 18:33:09 +08:00
|
|
|
use OCA\Passman\Service\VaultService;
|
2016-09-12 02:47:29 +08:00
|
|
|
use OCA\Passman\Service\CredentialService;
|
2016-09-12 01:45:20 +08:00
|
|
|
|
2016-09-15 00:57:38 +08:00
|
|
|
|
2016-09-10 00:02:53 +08:00
|
|
|
class VaultController extends ApiController {
|
2016-09-09 23:36:35 +08:00
|
|
|
private $userId;
|
2016-09-11 18:33:09 +08:00
|
|
|
private $vaultService;
|
2016-09-12 02:47:29 +08:00
|
|
|
private $credentialService;
|
2017-01-02 22:25:41 +08:00
|
|
|
private $settings;
|
2017-02-20 07:01:01 +08:00
|
|
|
private $deleteVaultRequestService;
|
2016-09-09 23:36:35 +08:00
|
|
|
|
2016-09-11 18:33:09 +08:00
|
|
|
public function __construct($AppName,
|
|
|
|
IRequest $request,
|
|
|
|
$UserId,
|
2016-09-12 02:47:29 +08:00
|
|
|
VaultService $vaultService,
|
2017-01-02 22:25:41 +08:00
|
|
|
CredentialService $credentialService,
|
2017-02-20 07:01:01 +08:00
|
|
|
DeleteVaultRequestService $deleteVaultRequestService,
|
2017-01-03 05:06:55 +08:00
|
|
|
SettingsService $settings) {
|
2016-12-23 20:46:43 +08:00
|
|
|
parent::__construct(
|
|
|
|
$AppName,
|
|
|
|
$request,
|
2017-01-19 20:39:01 +08:00
|
|
|
'GET, POST, DELETE, PUT, PATCH, OPTIONS',
|
2016-12-23 20:46:43 +08:00
|
|
|
'Authorization, Content-Type, Accept',
|
|
|
|
86400);
|
2016-09-09 23:36:35 +08:00
|
|
|
$this->userId = $UserId;
|
2016-09-11 18:33:09 +08:00
|
|
|
$this->vaultService = $vaultService;
|
2016-09-12 02:47:29 +08:00
|
|
|
$this->credentialService = $credentialService;
|
2017-02-20 07:01:01 +08:00
|
|
|
$this->deleteVaultRequestService = $deleteVaultRequestService;
|
2017-01-02 22:25:41 +08:00
|
|
|
$this->settings = $settings;
|
2016-09-09 23:36:35 +08:00
|
|
|
}
|
|
|
|
|
2016-09-11 05:30:17 +08:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2016-12-23 20:46:43 +08:00
|
|
|
* @NoCSRFRequired
|
2016-09-11 05:30:17 +08:00
|
|
|
*/
|
|
|
|
public function listVaults() {
|
2016-10-01 02:43:20 +08:00
|
|
|
$result = array();
|
2016-09-11 18:33:09 +08:00
|
|
|
$vaults = $this->vaultService->getByUser($this->userId);
|
2016-10-01 02:43:20 +08:00
|
|
|
|
2016-10-07 19:01:57 +08:00
|
|
|
$protected_credential_fields = array('getDescription', 'getEmail', 'getUsername', 'getPassword');
|
2017-01-12 06:55:28 +08:00
|
|
|
if (isset($vaults)) {
|
2017-01-04 18:55:48 +08:00
|
|
|
foreach ($vaults as $vault) {
|
|
|
|
$credential = $this->credentialService->getRandomCredentialByVaultId($vault->getId(), $this->userId);
|
|
|
|
$secret_field = $protected_credential_fields[array_rand($protected_credential_fields)];
|
2017-01-12 06:55:28 +08:00
|
|
|
if(isset($credential)) {
|
|
|
|
array_push($result, array(
|
|
|
|
'vault_id' => $vault->getId(),
|
|
|
|
'guid' => $vault->getGuid(),
|
|
|
|
'name' => $vault->getName(),
|
|
|
|
'created' => $vault->getCreated(),
|
|
|
|
'public_sharing_key' => $vault->getPublicSharingKey(),
|
|
|
|
'last_access' => $vault->getlastAccess(),
|
|
|
|
'challenge_password' => $credential->{$secret_field}(),
|
2017-02-20 07:01:01 +08:00
|
|
|
'delete_request_pending' => ($this->deleteVaultRequestService->getDeleteRequestForVault($vault->getGuid())) ? true : false
|
2017-01-12 06:55:28 +08:00
|
|
|
));
|
|
|
|
}
|
2017-01-04 18:55:48 +08:00
|
|
|
}
|
2016-10-01 02:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return new JSONResponse($result);
|
2016-09-11 05:30:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-09 23:36:35 +08:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2016-12-23 20:46:43 +08:00
|
|
|
* @NoCSRFRequired
|
2016-09-09 23:36:35 +08:00
|
|
|
*/
|
2016-09-11 18:33:09 +08:00
|
|
|
public function create($vault_name) {
|
|
|
|
$vault = $this->vaultService->createVault($vault_name, $this->userId);
|
|
|
|
return new JSONResponse($vault);
|
2016-09-09 23:36:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2016-12-23 20:46:43 +08:00
|
|
|
* @NoCSRFRequired
|
2016-09-09 23:36:35 +08:00
|
|
|
*/
|
2016-10-07 18:58:41 +08:00
|
|
|
public function get($vault_guid) {
|
|
|
|
$vault = null;
|
2016-10-07 19:01:57 +08:00
|
|
|
try {
|
2016-10-07 18:58:41 +08:00
|
|
|
$vault = $this->vaultService->getByGuid($vault_guid, $this->userId);
|
2016-12-29 07:07:27 +08:00
|
|
|
} catch (\Exception $e) {
|
2017-01-04 18:55:48 +08:00
|
|
|
return new NotFoundJSONResponse();
|
2016-09-29 03:22:49 +08:00
|
|
|
}
|
2016-10-07 18:58:41 +08:00
|
|
|
$result = array();
|
2017-01-12 06:55:28 +08:00
|
|
|
if (isset($vault)) {
|
2016-10-07 18:58:41 +08:00
|
|
|
$credentials = $this->credentialService->getCredentialsByVaultId($vault->getId(), $this->userId);
|
2016-10-07 19:01:57 +08:00
|
|
|
|
|
|
|
$result = array(
|
|
|
|
'vault_id' => $vault->getId(),
|
|
|
|
'guid' => $vault->getGuid(),
|
|
|
|
'name' => $vault->getName(),
|
|
|
|
'created' => $vault->getCreated(),
|
|
|
|
'private_sharing_key' => $vault->getPrivateSharingKey(),
|
|
|
|
'public_sharing_key' => $vault->getPublicSharingKey(),
|
|
|
|
'sharing_keys_generated' => $vault->getSharingKeysGenerated(),
|
|
|
|
'vault_settings' => $vault->getVaultSettings(),
|
2017-02-20 07:01:01 +08:00
|
|
|
'last_access' => $vault->getlastAccess(),
|
|
|
|
'delete_request_pending' => ($this->deleteVaultRequestService->getDeleteRequestForVault($vault->getGuid())) ? true : false
|
2016-10-07 19:01:57 +08:00
|
|
|
);
|
|
|
|
$result['credentials'] = $credentials;
|
|
|
|
|
|
|
|
$this->vaultService->setLastAccess($vault->getId(), $this->userId);
|
2016-10-07 18:58:41 +08:00
|
|
|
}
|
|
|
|
|
2016-10-07 19:01:57 +08:00
|
|
|
|
2016-09-29 02:27:52 +08:00
|
|
|
return new JSONResponse($result);
|
2016-09-09 23:36:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2016-12-23 20:46:43 +08:00
|
|
|
* @NoCSRFRequired
|
2016-09-09 23:36:35 +08:00
|
|
|
*/
|
2016-10-07 18:58:41 +08:00
|
|
|
public function update($vault_guid, $name, $vault_settings) {
|
|
|
|
$vault = $this->vaultService->getByGuid($vault_guid, $this->userId);
|
2017-01-04 18:55:48 +08:00
|
|
|
if ($name && $vault) {
|
2016-09-29 04:05:09 +08:00
|
|
|
$vault->setName($name);
|
|
|
|
}
|
2017-01-04 18:55:48 +08:00
|
|
|
if ($vault_settings && $vault) {
|
2016-09-29 04:05:09 +08:00
|
|
|
$vault->setVaultSettings($vault_settings);
|
|
|
|
}
|
|
|
|
$this->vaultService->updateVault($vault);
|
2016-09-09 23:36:35 +08:00
|
|
|
}
|
|
|
|
|
2016-09-26 00:34:33 +08:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2016-12-23 20:46:43 +08:00
|
|
|
* @NoCSRFRequired
|
2016-09-26 00:34:33 +08:00
|
|
|
*/
|
2016-10-07 18:58:41 +08:00
|
|
|
public function updateSharingKeys($vault_guid, $private_sharing_key, $public_sharing_key) {
|
|
|
|
$vault = null;
|
2016-10-07 19:01:57 +08:00
|
|
|
try {
|
2016-10-07 18:58:41 +08:00
|
|
|
$vault = $this->vaultService->getByGuid($vault_guid, $this->userId);
|
2016-12-29 07:07:27 +08:00
|
|
|
} catch (\Exception $e) {
|
2017-01-04 18:55:48 +08:00
|
|
|
// No need to catch the execption
|
|
|
|
}
|
2016-10-07 18:58:41 +08:00
|
|
|
|
2017-01-04 18:55:48 +08:00
|
|
|
if ($vault) {
|
|
|
|
$this->vaultService->updateSharingKeys($vault->getId(), $private_sharing_key, $public_sharing_key);
|
2016-10-07 18:58:41 +08:00
|
|
|
}
|
|
|
|
|
2016-09-26 00:34:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-09 23:36:35 +08:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2016-12-23 20:46:43 +08:00
|
|
|
* @NoCSRFRequired
|
2016-09-09 23:36:35 +08:00
|
|
|
*/
|
2017-01-19 01:38:28 +08:00
|
|
|
public function delete($vault_guid) {
|
|
|
|
$this->vaultService->deleteVault($vault_guid, $this->userId);
|
2017-01-12 06:55:28 +08:00
|
|
|
return new JSONResponse(array('ok' => true));
|
2016-09-09 23:36:35 +08:00
|
|
|
}
|
|
|
|
}
|