RSAPrivateKey.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_External\Lib\Auth\PublicKey;
  7. use OCA\Files_External\Lib\Auth\AuthMechanism;
  8. use OCA\Files_External\Lib\DefinitionParameter;
  9. use OCA\Files_External\Lib\StorageConfig;
  10. use OCP\IConfig;
  11. use OCP\IL10N;
  12. use OCP\IUser;
  13. use phpseclib\Crypt\RSA as RSACrypt;
  14. /**
  15. * RSA public key authentication
  16. */
  17. class RSAPrivateKey extends AuthMechanism {
  18. /** @var IConfig */
  19. private $config;
  20. public function __construct(IL10N $l, IConfig $config) {
  21. $this->config = $config;
  22. $this
  23. ->setIdentifier('publickey::rsa_private')
  24. ->setScheme(self::SCHEME_PUBLICKEY)
  25. ->setText($l->t('RSA private key'))
  26. ->addParameters([
  27. new DefinitionParameter('user', $l->t('Login')),
  28. (new DefinitionParameter('password', $l->t('Password')))
  29. ->setFlag(DefinitionParameter::FLAG_OPTIONAL)
  30. ->setType(DefinitionParameter::VALUE_PASSWORD),
  31. new DefinitionParameter('private_key', $l->t('Private key')),
  32. ]);
  33. }
  34. /**
  35. * @return void
  36. */
  37. public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) {
  38. $auth = new RSACrypt();
  39. $auth->setPassword($this->config->getSystemValue('secret', ''));
  40. if (!$auth->loadKey($storage->getBackendOption('private_key'))) {
  41. // Add fallback routine for a time where secret was not enforced to be exists
  42. $auth->setPassword('');
  43. if (!$auth->loadKey($storage->getBackendOption('private_key'))) {
  44. throw new \RuntimeException('unable to load private key');
  45. }
  46. }
  47. $storage->setBackendOption('public_key_auth', $auth);
  48. }
  49. }