AuthSettingsController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@owncloud.com>
  6. * @author Fabrizio Steiner <fabrizio.steiner@gmail.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Marcel Waldvogel <marcel.waldvogel@uni-konstanz.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Settings\Controller;
  28. use OC\AppFramework\Http;
  29. use OC\Authentication\Exceptions\InvalidTokenException;
  30. use OC\Authentication\Exceptions\PasswordlessTokenException;
  31. use OC\Authentication\Token\IProvider;
  32. use OC\Authentication\Token\IToken;
  33. use OCP\AppFramework\Controller;
  34. use OCP\AppFramework\Http\JSONResponse;
  35. use OCP\IRequest;
  36. use OCP\ISession;
  37. use OCP\IUserManager;
  38. use OCP\Security\ISecureRandom;
  39. use OCP\Session\Exceptions\SessionNotAvailableException;
  40. class AuthSettingsController extends Controller {
  41. /** @var IProvider */
  42. private $tokenProvider;
  43. /** @var IUserManager */
  44. private $userManager;
  45. /** @var ISession */
  46. private $session;
  47. /** @var string */
  48. private $uid;
  49. /** @var ISecureRandom */
  50. private $random;
  51. /**
  52. * @param string $appName
  53. * @param IRequest $request
  54. * @param IProvider $tokenProvider
  55. * @param IUserManager $userManager
  56. * @param ISession $session
  57. * @param ISecureRandom $random
  58. * @param string $userId
  59. */
  60. public function __construct($appName, IRequest $request, IProvider $tokenProvider, IUserManager $userManager,
  61. ISession $session, ISecureRandom $random, $userId) {
  62. parent::__construct($appName, $request);
  63. $this->tokenProvider = $tokenProvider;
  64. $this->userManager = $userManager;
  65. $this->uid = $userId;
  66. $this->session = $session;
  67. $this->random = $random;
  68. }
  69. /**
  70. * @NoAdminRequired
  71. * @NoSubadminRequired
  72. *
  73. * @return JSONResponse|array
  74. */
  75. public function index() {
  76. $tokens = $this->tokenProvider->getTokenByUser($this->uid);
  77. try {
  78. $sessionId = $this->session->getId();
  79. } catch (SessionNotAvailableException $ex) {
  80. return $this->getServiceNotAvailableResponse();
  81. }
  82. try {
  83. $sessionToken = $this->tokenProvider->getToken($sessionId);
  84. } catch (InvalidTokenException $ex) {
  85. return $this->getServiceNotAvailableResponse();
  86. }
  87. return array_map(function(IToken $token) use ($sessionToken) {
  88. $data = $token->jsonSerialize();
  89. if ($sessionToken->getId() === $token->getId()) {
  90. $data['canDelete'] = false;
  91. $data['current'] = true;
  92. } else {
  93. $data['canDelete'] = true;
  94. }
  95. return $data;
  96. }, $tokens);
  97. }
  98. /**
  99. * @NoAdminRequired
  100. * @NoSubadminRequired
  101. * @PasswordConfirmationRequired
  102. *
  103. * @param string $name
  104. * @return JSONResponse
  105. */
  106. public function create($name) {
  107. try {
  108. $sessionId = $this->session->getId();
  109. } catch (SessionNotAvailableException $ex) {
  110. return $this->getServiceNotAvailableResponse();
  111. }
  112. try {
  113. $sessionToken = $this->tokenProvider->getToken($sessionId);
  114. $loginName = $sessionToken->getLoginName();
  115. try {
  116. $password = $this->tokenProvider->getPassword($sessionToken, $sessionId);
  117. } catch (PasswordlessTokenException $ex) {
  118. $password = null;
  119. }
  120. } catch (InvalidTokenException $ex) {
  121. return $this->getServiceNotAvailableResponse();
  122. }
  123. $token = $this->generateRandomDeviceToken();
  124. $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN);
  125. $tokenData = $deviceToken->jsonSerialize();
  126. $tokenData['canDelete'] = true;
  127. return new JSONResponse([
  128. 'token' => $token,
  129. 'loginName' => $loginName,
  130. 'deviceToken' => $tokenData,
  131. ]);
  132. }
  133. /**
  134. * @return JSONResponse
  135. */
  136. private function getServiceNotAvailableResponse() {
  137. $resp = new JSONResponse();
  138. $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
  139. return $resp;
  140. }
  141. /**
  142. * Return a 25 digit device password
  143. *
  144. * Example: AbCdE-fGhJk-MnPqR-sTwXy-23456
  145. *
  146. * @return string
  147. */
  148. private function generateRandomDeviceToken() {
  149. $groups = [];
  150. for ($i = 0; $i < 5; $i++) {
  151. $groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE);
  152. }
  153. return implode('-', $groups);
  154. }
  155. /**
  156. * @NoAdminRequired
  157. * @NoSubadminRequired
  158. *
  159. * @return array
  160. */
  161. public function destroy($id) {
  162. $this->tokenProvider->invalidateTokenById($this->uid, $id);
  163. return [];
  164. }
  165. /**
  166. * @NoAdminRequired
  167. * @NoSubadminRequired
  168. *
  169. * @param int $id
  170. * @param array $scope
  171. * @return array|JSONResponse
  172. */
  173. public function update($id, array $scope) {
  174. try {
  175. $token = $this->tokenProvider->getTokenById((string)$id);
  176. if ($token->getUID() !== $this->uid) {
  177. throw new InvalidTokenException('User mismatch');
  178. }
  179. } catch (InvalidTokenException $e) {
  180. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  181. }
  182. $token->setScope([
  183. 'filesystem' => $scope['filesystem']
  184. ]);
  185. $this->tokenProvider->updateToken($token);
  186. return [];
  187. }
  188. }