OauthApiController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\OAuth2\Controller;
  22. use OC\Authentication\Exceptions\InvalidTokenException;
  23. use OC\Authentication\Token\ExpiredTokenException;
  24. use OC\Authentication\Token\IProvider as TokenProvider;
  25. use OCA\OAuth2\Db\AccessTokenMapper;
  26. use OCA\OAuth2\Db\ClientMapper;
  27. use OCA\OAuth2\Exceptions\AccessTokenNotFoundException;
  28. use OCA\OAuth2\Exceptions\ClientNotFoundException;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\JSONResponse;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. use OCP\IRequest;
  34. use OCP\Security\ICrypto;
  35. use OCP\Security\ISecureRandom;
  36. class OauthApiController extends Controller {
  37. /** @var AccessTokenMapper */
  38. private $accessTokenMapper;
  39. /** @var ClientMapper */
  40. private $clientMapper;
  41. /** @var ICrypto */
  42. private $crypto;
  43. /** @var TokenProvider */
  44. private $tokenProvider;
  45. /** @var ISecureRandom */
  46. private $secureRandom;
  47. /** @var ITimeFactory */
  48. private $time;
  49. /**
  50. * @param string $appName
  51. * @param IRequest $request
  52. * @param ICrypto $crypto
  53. * @param AccessTokenMapper $accessTokenMapper
  54. * @param ClientMapper $clientMapper
  55. * @param TokenProvider $tokenProvider
  56. * @param ISecureRandom $secureRandom
  57. * @param ITimeFactory $time
  58. */
  59. public function __construct($appName,
  60. IRequest $request,
  61. ICrypto $crypto,
  62. AccessTokenMapper $accessTokenMapper,
  63. ClientMapper $clientMapper,
  64. TokenProvider $tokenProvider,
  65. ISecureRandom $secureRandom,
  66. ITimeFactory $time) {
  67. parent::__construct($appName, $request);
  68. $this->crypto = $crypto;
  69. $this->accessTokenMapper = $accessTokenMapper;
  70. $this->clientMapper = $clientMapper;
  71. $this->tokenProvider = $tokenProvider;
  72. $this->secureRandom = $secureRandom;
  73. $this->time = $time;
  74. }
  75. /**
  76. * @PublicPage
  77. * @NoCSRFRequired
  78. *
  79. * @param string $grant_type
  80. * @param string $code
  81. * @param string $refresh_token
  82. * @param string $client_id
  83. * @param string $client_secret
  84. * @return JSONResponse
  85. */
  86. public function getToken($grant_type, $code, $refresh_token, $client_id, $client_secret) {
  87. // We only handle two types
  88. if ($grant_type !== 'authorization_code' && $grant_type !== 'refresh_token') {
  89. return new JSONResponse([
  90. 'error' => 'invalid_grant',
  91. ], Http::STATUS_BAD_REQUEST);
  92. }
  93. // We handle the initial and refresh tokens the same way
  94. if ($grant_type === 'refresh_token' ) {
  95. $code = $refresh_token;
  96. }
  97. try {
  98. $accessToken = $this->accessTokenMapper->getByCode($code);
  99. } catch (AccessTokenNotFoundException $e) {
  100. return new JSONResponse([
  101. 'error' => 'invalid_request',
  102. ], Http::STATUS_BAD_REQUEST);
  103. }
  104. try {
  105. $client = $this->clientMapper->getByUid($accessToken->getClientId());
  106. } catch (ClientNotFoundException $e) {
  107. return new JSONResponse([
  108. 'error' => 'invalid_request',
  109. ], Http::STATUS_BAD_REQUEST);
  110. }
  111. // The client id and secret must match. Else we don't provide an access token!
  112. if ($client->getClientIdentifier() !== $client_id || $client->getSecret() !== $client_secret) {
  113. return new JSONResponse([
  114. 'error' => 'invalid_client',
  115. ], Http::STATUS_BAD_REQUEST);
  116. }
  117. $decryptedToken = $this->crypto->decrypt($accessToken->getEncryptedToken(), $code);
  118. // Obtain the appToken assoicated
  119. try {
  120. $appToken = $this->tokenProvider->getTokenById($accessToken->getTokenId());
  121. } catch (ExpiredTokenException $e) {
  122. $appToken = $e->getToken();
  123. } catch (InvalidTokenException $e) {
  124. //We can't do anything...
  125. $this->accessTokenMapper->delete($accessToken);
  126. return new JSONResponse([
  127. 'error' => 'invalid_request',
  128. ], Http::STATUS_BAD_REQUEST);
  129. }
  130. // Rotate the apptoken (so the old one becomes invalid basically)
  131. $newToken = $this->secureRandom->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  132. $appToken = $this->tokenProvider->rotate(
  133. $appToken,
  134. $decryptedToken,
  135. $newToken
  136. );
  137. // Expiration is in 1 hour again
  138. $appToken->setExpires($this->time->getTime() + 3600);
  139. $this->tokenProvider->updateToken($appToken);
  140. // Generate a new refresh token and encrypt the new apptoken in the DB
  141. $newCode = $this->secureRandom->generate(128, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  142. $accessToken->setHashedCode(hash('sha512', $newCode));
  143. $accessToken->setEncryptedToken($this->crypto->encrypt($newToken, $newCode));
  144. $this->accessTokenMapper->update($accessToken);
  145. return new JSONResponse(
  146. [
  147. 'access_token' => $newToken,
  148. 'token_type' => 'Bearer',
  149. 'expires_in' => 3600,
  150. 'refresh_token' => $newCode,
  151. 'user_id' => $appToken->getUID(),
  152. ]
  153. );
  154. }
  155. }