session.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Clark Tomlinson <fallen013@gmail.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Encryption;
  23. use \OCP\ISession;
  24. class Session {
  25. /** @var ISession */
  26. protected $session;
  27. const NOT_INITIALIZED = '0';
  28. const INIT_EXECUTED = '1';
  29. const INIT_SUCCESSFUL = '2';
  30. public function __construct(ISession $session) {
  31. $this->session = $session;
  32. }
  33. /**
  34. * Sets status of encryption app
  35. *
  36. * @param string $status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
  37. */
  38. public function setStatus($status) {
  39. $this->session->set('encryptionInitialized', $status);
  40. }
  41. /**
  42. * Gets status if we already tried to initialize the encryption app
  43. *
  44. * @return string init status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
  45. */
  46. public function getStatus() {
  47. $status = $this->session->get('encryptionInitialized');
  48. if (is_null($status)) {
  49. $status = self::NOT_INITIALIZED;
  50. }
  51. return $status;
  52. }
  53. /**
  54. * Gets user or public share private key from session
  55. *
  56. * @return string $privateKey The user's plaintext private key
  57. * @throws Exceptions\PrivateKeyMissingException
  58. */
  59. public function getPrivateKey() {
  60. $key = $this->session->get('privateKey');
  61. if (is_null($key)) {
  62. throw new Exceptions\PrivateKeyMissingException('please try to log-out and log-in again', 0);
  63. }
  64. return $key;
  65. }
  66. /**
  67. * check if private key is set
  68. *
  69. * @return boolean
  70. */
  71. public function isPrivateKeySet() {
  72. $key = $this->session->get('privateKey');
  73. if (is_null($key)) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. /**
  79. * Sets user private key to session
  80. *
  81. * @param string $key users private key
  82. *
  83. * @note this should only be set on login
  84. */
  85. public function setPrivateKey($key) {
  86. $this->session->set('privateKey', $key);
  87. }
  88. /**
  89. * remove keys from session
  90. */
  91. public function clear() {
  92. $this->session->remove('publicSharePrivateKey');
  93. $this->session->remove('privateKey');
  94. $this->session->remove('encryptionInitialized');
  95. }
  96. }