RSAPrivateKey.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Files_External\Lib\Auth\PublicKey;
  24. use OCA\Files_External\Lib\Auth\AuthMechanism;
  25. use OCA\Files_External\Lib\DefinitionParameter;
  26. use OCA\Files_External\Lib\StorageConfig;
  27. use OCP\IConfig;
  28. use OCP\IL10N;
  29. use OCP\IUser;
  30. use phpseclib\Crypt\RSA as RSACrypt;
  31. /**
  32. * RSA public key authentication
  33. */
  34. class RSAPrivateKey extends AuthMechanism {
  35. /** @var IConfig */
  36. private $config;
  37. public function __construct(IL10N $l, IConfig $config) {
  38. $this->config = $config;
  39. $this
  40. ->setIdentifier('publickey::rsa_private')
  41. ->setScheme(self::SCHEME_PUBLICKEY)
  42. ->setText($l->t('RSA private key'))
  43. ->addParameters([
  44. new DefinitionParameter('user', $l->t('Username')),
  45. (new DefinitionParameter('password', $l->t('Password')))
  46. ->setFlag(DefinitionParameter::FLAG_OPTIONAL)
  47. ->setType(DefinitionParameter::VALUE_PASSWORD),
  48. new DefinitionParameter('private_key', $l->t('Private key')),
  49. ]);
  50. }
  51. public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
  52. $auth = new RSACrypt();
  53. $auth->setPassword($this->config->getSystemValue('secret', ''));
  54. if (!$auth->loadKey($storage->getBackendOption('private_key'))) {
  55. // Add fallback routine for a time where secret was not enforced to be exists
  56. $auth->setPassword('');
  57. if (!$auth->loadKey($storage->getBackendOption('private_key'))) {
  58. throw new \RuntimeException('unable to load private key');
  59. }
  60. }
  61. $storage->setBackendOption('public_key_auth', $auth);
  62. }
  63. }