diff --git a/apps/files_external/appinfo/application.php b/apps/files_external/appinfo/application.php index 38b9e9b7c36..d9c877a54d1 100644 --- a/apps/files_external/appinfo/application.php +++ b/apps/files_external/appinfo/application.php @@ -75,6 +75,10 @@ class Application extends App { // AuthMechanism::SCHEME_BUILTIN mechanism $container->query('OCA\Files_External\Lib\Auth\Builtin'), + + // AuthMechanism::SCHEME_PASSWORD mechanisms + $container->query('OCA\Files_External\Lib\Auth\Password\Password'), + $container->query('OCA\Files_External\Lib\Auth\Password\SessionCredentials'), ]); } diff --git a/apps/files_external/lib/auth/password/password.php b/apps/files_external/lib/auth/password/password.php new file mode 100644 index 00000000000..96ad4b496d4 --- /dev/null +++ b/apps/files_external/lib/auth/password/password.php @@ -0,0 +1,45 @@ + + * + * @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 + * + */ + +namespace OCA\Files_External\Lib\Auth\Password; + +use \OCP\IL10N; +use \OCA\Files_External\Lib\DefinitionParameter; +use \OCA\Files_External\Lib\Auth\AuthMechanism; + +/** + * Basic password authentication mechanism + */ +class Password extends AuthMechanism { + + public function __construct(IL10N $l) { + $this + ->setIdentifier('password::password') + ->setScheme(self::SCHEME_PASSWORD) + ->setText($l->t('Username and password')) + ->addParameters([ + (new DefinitionParameter('user', $l->t('Username'))), + (new DefinitionParameter('password', $l->t('Password'))) + ->setType(DefinitionParameter::VALUE_PASSWORD), + ]); + } + +} diff --git a/apps/files_external/lib/auth/password/sessioncredentials.php b/apps/files_external/lib/auth/password/sessioncredentials.php new file mode 100644 index 00000000000..37cfd97a176 --- /dev/null +++ b/apps/files_external/lib/auth/password/sessioncredentials.php @@ -0,0 +1,84 @@ + + * + * @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 + * + */ + +namespace OCA\Files_External\Lib\Auth\Password; + +use \OCP\IL10N; +use \OCA\Files_External\Lib\DefinitionParameter; +use \OCA\Files_External\Lib\Auth\AuthMechanism; +use \OCA\Files_External\Lib\StorageConfig; +use \OCP\ISession; +use \OCP\Security\ICrypto; +use \OCP\Files\Storage; +use \OCA\Files_External\Lib\SessionStorageWrapper; +use \OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException; + +/** + * Username and password from login credentials, saved in session + */ +class SessionCredentials extends AuthMechanism { + + /** @var ISession */ + protected $session; + + /** @var ICrypto */ + protected $crypto; + + public function __construct(IL10N $l, ISession $session, ICrypto $crypto) { + $this->session = $session; + $this->crypto = $crypto; + + $this + ->setIdentifier('password::sessioncredentials') + ->setScheme(self::SCHEME_PASSWORD) + ->setText($l->t('Session credentials')) + ->addParameters([ + ]) + ; + + \OCP\Util::connectHook('OC_User', 'post_login', $this, 'authenticate'); + } + + /** + * Hook listener on post login + * + * @param array $params + */ + public function authenticate(array $params) { + $this->session->set('password::sessioncredentials/credentials', $this->crypto->encrypt(json_encode($params))); + } + + public function manipulateStorageConfig(StorageConfig &$storage) { + $encrypted = $this->session->get('password::sessioncredentials/credentials'); + if (!isset($encrypted)) { + throw new InsufficientDataForMeaningfulAnswerException('No session credentials saved'); + } + + $credentials = json_decode($this->crypto->decrypt($encrypted), true); + $storage->setBackendOption('user', $this->session->get('loginname')); + $storage->setBackendOption('password', $credentials['password']); + } + + public function wrapStorage(Storage $storage) { + return new SessionStorageWrapper(['storage' => $storage]); + } + +} diff --git a/apps/files_external/lib/sessionstoragewrapper.php b/apps/files_external/lib/sessionstoragewrapper.php new file mode 100644 index 00000000000..91be7eb1903 --- /dev/null +++ b/apps/files_external/lib/sessionstoragewrapper.php @@ -0,0 +1,43 @@ + + * + * @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 + * + */ + +namespace OCA\Files_External\Lib; + +use \OCP\Files\Storage; +use \OC\Files\Storage\Wrapper\PermissionsMask; +use \OCP\Constants; + +/** + * Wrap Storage in PermissionsMask for session ephemeral use + */ +class SessionStorageWrapper extends PermissionsMask { + + /** + * @param array $arguments ['storage' => $storage] + */ + public function __construct(array $arguments) { + // disable sharing permission + $arguments['mask'] = Constants::PERMISSION_ALL & ~Constants::PERMISSION_SHARE; + parent::__construct($arguments); + } + +} + diff --git a/lib/private/files/storage/wrapper/permissionsmask.php b/lib/private/files/storage/wrapper/permissionsmask.php index 993936321d0..50c3f2a6268 100644 --- a/lib/private/files/storage/wrapper/permissionsmask.php +++ b/lib/private/files/storage/wrapper/permissionsmask.php @@ -65,6 +65,10 @@ class PermissionsMask extends Wrapper { return $this->checkMask(Constants::PERMISSION_DELETE) and parent::isDeletable($path); } + public function isSharable($path) { + return $this->checkMask(Constants::PERMISSION_SHARE) and parent::isSharable($parm); + } + public function getPermissions($path) { return $this->storage->getPermissions($path) & $this->mask; }