You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.0 KiB

10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@owncloud.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  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, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Settings\Controller;
  24. use OC\AppFramework\Http;
  25. use OC\Authentication\Exceptions\InvalidTokenException;
  26. use OC\Authentication\Exceptions\PasswordlessTokenException;
  27. use OC\Authentication\Token\IProvider;
  28. use OC\Authentication\Token\IToken;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http\JSONResponse;
  31. use OCP\IRequest;
  32. use OCP\ISession;
  33. use OCP\IUserManager;
  34. use OCP\Security\ISecureRandom;
  35. use OCP\Session\Exceptions\SessionNotAvailableException;
  36. class AuthSettingsController extends Controller {
  37. /** @var IProvider */
  38. private $tokenProvider;
  39. /** @var IUserManager */
  40. private $userManager;
  41. /** @var ISession */
  42. private $session;
  43. /** @var string */
  44. private $uid;
  45. /** @var ISecureRandom */
  46. private $random;
  47. /**
  48. * @param string $appName
  49. * @param IRequest $request
  50. * @param IProvider $tokenProvider
  51. * @param IUserManager $userManager
  52. * @param ISession $session
  53. * @param ISecureRandom $random
  54. * @param string $userId
  55. */
  56. public function __construct($appName, IRequest $request, IProvider $tokenProvider, IUserManager $userManager,
  57. ISession $session, ISecureRandom $random, $userId) {
  58. parent::__construct($appName, $request);
  59. $this->tokenProvider = $tokenProvider;
  60. $this->userManager = $userManager;
  61. $this->uid = $userId;
  62. $this->session = $session;
  63. $this->random = $random;
  64. }
  65. /**
  66. * @NoAdminRequired
  67. * @NoSubadminRequired
  68. *
  69. * @return JSONResponse
  70. */
  71. public function index() {
  72. $user = $this->userManager->get($this->uid);
  73. if (is_null($user)) {
  74. return [];
  75. }
  76. $tokens = $this->tokenProvider->getTokenByUser($user);
  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. *
  102. * @return JSONResponse
  103. */
  104. public function create($name) {
  105. try {
  106. $sessionId = $this->session->getId();
  107. } catch (SessionNotAvailableException $ex) {
  108. return $this->getServiceNotAvailableResponse();
  109. }
  110. try {
  111. $sessionToken = $this->tokenProvider->getToken($sessionId);
  112. $loginName = $sessionToken->getLoginName();
  113. try {
  114. $password = $this->tokenProvider->getPassword($sessionToken, $sessionId);
  115. } catch (PasswordlessTokenException $ex) {
  116. $password = null;
  117. }
  118. } catch (InvalidTokenException $ex) {
  119. return $this->getServiceNotAvailableResponse();
  120. }
  121. $token = $this->generateRandomDeviceToken();
  122. $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN);
  123. $tokenData = $deviceToken->jsonSerialize();
  124. $tokenData['canDelete'] = true;
  125. return [
  126. 'token' => $token,
  127. 'loginName' => $loginName,
  128. 'deviceToken' => $tokenData
  129. ];
  130. }
  131. private function getServiceNotAvailableResponse() {
  132. $resp = new JSONResponse();
  133. $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
  134. return $resp;
  135. }
  136. /**
  137. * Return a 20 digit device password
  138. *
  139. * Example: ABCDE-FGHIJ-KLMNO-PQRST
  140. *
  141. * @return string
  142. */
  143. private function generateRandomDeviceToken() {
  144. $groups = [];
  145. for ($i = 0; $i < 4; $i++) {
  146. $groups[] = $this->random->generate(5, implode('', range('A', 'Z')));
  147. }
  148. return implode('-', $groups);
  149. }
  150. /**
  151. * @NoAdminRequired
  152. * @NoSubadminRequired
  153. *
  154. * @return JSONResponse
  155. */
  156. public function destroy($id) {
  157. $user = $this->userManager->get($this->uid);
  158. if (is_null($user)) {
  159. return [];
  160. }
  161. $this->tokenProvider->invalidateTokenById($user, $id);
  162. return [];
  163. }
  164. /**
  165. * @NoAdminRequired
  166. * @NoSubadminRequired
  167. *
  168. * @param int $id
  169. * @param array $scope
  170. */
  171. public function update($id, array $scope) {
  172. $token = $this->tokenProvider->getTokenById($id);
  173. $token->setScope([
  174. 'filesystem' => $scope['filesystem'],
  175. 'app' => array_values($scope['apps'])
  176. ]);
  177. $this->tokenProvider->updateToken($token);
  178. return [];
  179. }
  180. }