RefreshToken.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace OAuth2\GrantType;
  3. use OAuth2\Storage\RefreshTokenInterface;
  4. use OAuth2\ResponseType\AccessTokenInterface;
  5. use OAuth2\RequestInterface;
  6. use OAuth2\ResponseInterface;
  7. /**
  8. *
  9. * @author Brent Shaffer <bshafs at gmail dot com>
  10. */
  11. class RefreshToken implements GrantTypeInterface
  12. {
  13. private $refreshToken;
  14. protected $storage;
  15. protected $config;
  16. /**
  17. * @param OAuth2\Storage\RefreshTokenInterface $storage REQUIRED Storage class for retrieving refresh token information
  18. * @param array $config OPTIONAL Configuration options for the server
  19. * <code>
  20. * $config = array(
  21. * 'always_issue_new_refresh_token' => true, // whether to issue a new refresh token upon successful token request
  22. * 'unset_refresh_token_after_use' => true // whether to unset the refresh token after after using
  23. * );
  24. * </code>
  25. */
  26. public function __construct(RefreshTokenInterface $storage, $config = array())
  27. {
  28. $this->config = array_merge(array(
  29. 'always_issue_new_refresh_token' => false,
  30. 'unset_refresh_token_after_use' => true
  31. ), $config);
  32. // to preserve B.C. with v1.6
  33. // @see https://github.com/bshaffer/oauth2-server-php/pull/580
  34. // @todo - remove in v2.0
  35. if (isset($config['always_issue_new_refresh_token']) && !isset($config['unset_refresh_token_after_use'])) {
  36. $this->config['unset_refresh_token_after_use'] = $config['always_issue_new_refresh_token'];
  37. }
  38. $this->storage = $storage;
  39. }
  40. public function getQuerystringIdentifier()
  41. {
  42. return 'refresh_token';
  43. }
  44. public function validateRequest(RequestInterface $request, ResponseInterface $response)
  45. {
  46. if (!$request->request("refresh_token")) {
  47. $response->setError(400, 'invalid_request', 'Missing parameter: "refresh_token" is required');
  48. return null;
  49. }
  50. if (!$refreshToken = $this->storage->getRefreshToken($request->request("refresh_token"))) {
  51. $response->setError(400, 'invalid_grant', 'Invalid refresh token');
  52. return null;
  53. }
  54. if ($refreshToken['expires'] > 0 && $refreshToken["expires"] < time()) {
  55. $response->setError(400, 'invalid_grant', 'Refresh token has expired');
  56. return null;
  57. }
  58. // store the refresh token locally so we can delete it when a new refresh token is generated
  59. $this->refreshToken = $refreshToken;
  60. return true;
  61. }
  62. public function getClientId()
  63. {
  64. return $this->refreshToken['client_id'];
  65. }
  66. public function getUserId()
  67. {
  68. return isset($this->refreshToken['user_id']) ? $this->refreshToken['user_id'] : null;
  69. }
  70. public function getScope()
  71. {
  72. return isset($this->refreshToken['scope']) ? $this->refreshToken['scope'] : null;
  73. }
  74. public function createAccessToken(AccessTokenInterface $accessToken, $client_id, $user_id, $scope)
  75. {
  76. /*
  77. * It is optional to force a new refresh token when a refresh token is used.
  78. * However, if a new refresh token is issued, the old one MUST be expired
  79. * @see http://tools.ietf.org/html/rfc6749#section-6
  80. */
  81. $issueNewRefreshToken = $this->config['always_issue_new_refresh_token'];
  82. $unsetRefreshToken = $this->config['unset_refresh_token_after_use'];
  83. $token = $accessToken->createAccessToken($client_id, $user_id, $scope, $issueNewRefreshToken);
  84. if ($unsetRefreshToken) {
  85. $this->storage->unsetRefreshToken($this->refreshToken['refresh_token']);
  86. }
  87. return $token;
  88. }
  89. }