PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/OAuth2/ResponseType/AccessToken.php

https://github.com/rich-choy/oauth2-server-php
PHP | 129 lines | 59 code | 16 blank | 54 comment | 5 complexity | 6137ae3f7608187b4f9c7a6620fd9138 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. namespace OAuth2\ResponseType;
  3. use OAuth2\Storage\AccessTokenInterface as AccessTokenStorageInterface;
  4. use OAuth2\Storage\RefreshTokenInterface;
  5. /**
  6. *
  7. * @author Brent Shaffer <bshafs at gmail dot com>
  8. */
  9. class AccessToken implements AccessTokenInterface
  10. {
  11. protected $tokenStorage;
  12. protected $refreshStorage;
  13. public function __construct(AccessTokenStorageInterface $tokenStorage, RefreshTokenInterface $refreshStorage = null, array $config = array())
  14. {
  15. $this->tokenStorage = $tokenStorage;
  16. $this->refreshStorage = $refreshStorage;
  17. $this->config = array_merge(array(
  18. 'token_type' => 'bearer',
  19. 'access_lifetime' => 3600,
  20. 'refresh_token_lifetime' => 1209600,
  21. ), $config);
  22. }
  23. public function getAuthorizeResponse($params, $user_id = null)
  24. {
  25. // build the URL to redirect to
  26. $result = array('query' => array());
  27. $params += array('scope' => null, 'state' => null);
  28. /*
  29. * a refresh token MUST NOT be included in the fragment
  30. *
  31. * @see http://tools.ietf.org/html/rfc6749#section-4.2.2
  32. */
  33. $includeRefreshToken = false;
  34. $result["fragment"] = $this->createAccessToken($params['client_id'], $user_id, $params['scope'], $includeRefreshToken);
  35. if (isset($params['state'])) {
  36. $result["fragment"]["state"] = $params['state'];
  37. }
  38. return array($params['redirect_uri'], $result);
  39. }
  40. /**
  41. * Handle the creation of access token, also issue refresh token if supported / desirable.
  42. *
  43. * @param $client_id
  44. * Client identifier related to the access token.
  45. * @param $user_id
  46. * User ID associated with the access token
  47. * @param $scope
  48. * (optional) Scopes to be stored in space-separated string.
  49. * @param bool $includeRefreshToken
  50. * If true, a new refresh_token will be added to the response
  51. *
  52. * @see http://tools.ietf.org/html/rfc6749#section-5
  53. * @ingroup oauth2_section_5
  54. */
  55. public function createAccessToken($client_id, $user_id, $scope = null, $includeRefreshToken = true)
  56. {
  57. $token = array(
  58. "access_token" => $this->generateAccessToken(),
  59. "expires_in" => $this->config['access_lifetime'],
  60. "token_type" => $this->config['token_type'],
  61. "scope" => $scope
  62. );
  63. $this->tokenStorage->setAccessToken($token["access_token"], $client_id, $user_id, $this->config['access_lifetime'] ? time() + $this->config['access_lifetime'] : null, $scope);
  64. /*
  65. * Issue a refresh token also, if we support them
  66. *
  67. * Refresh Tokens are considered supported if an instance of OAuth2_Storage_RefreshTokenInterface
  68. * is supplied in the constructor
  69. */
  70. if ($includeRefreshToken && $this->refreshStorage) {
  71. $token["refresh_token"] = $this->generateRefreshToken();
  72. $this->refreshStorage->setRefreshToken($token['refresh_token'], $client_id, $user_id, time() + $this->config['refresh_token_lifetime'], $scope);
  73. }
  74. return $token;
  75. }
  76. /**
  77. * Generates an unique access token.
  78. *
  79. * Implementing classes may want to override this function to implement
  80. * other access token generation schemes.
  81. *
  82. * @return
  83. * An unique access token.
  84. *
  85. * @ingroup oauth2_section_4
  86. */
  87. protected function generateAccessToken()
  88. {
  89. $tokenLen = 40;
  90. if (file_exists('/dev/urandom')) { // Get 100 bytes of random data
  91. $randomData = file_get_contents('/dev/urandom', false, null, 0, 100) . uniqid(mt_rand(), true);
  92. } else {
  93. $randomData = mt_rand() . mt_rand() . mt_rand() . mt_rand() . microtime(true) . uniqid(mt_rand(), true);
  94. }
  95. return substr(hash('sha512', $randomData), 0, $tokenLen);
  96. }
  97. /**
  98. * Generates an unique refresh token
  99. *
  100. * Implementing classes may want to override this function to implement
  101. * other refresh token generation schemes.
  102. *
  103. * @return
  104. * An unique refresh.
  105. *
  106. * @ingroup oauth2_section_4
  107. * @see OAuth2::generateAccessToken()
  108. */
  109. protected function generateRefreshToken()
  110. {
  111. return $this->generateAccessToken(); // let's reuse the same scheme for token generation
  112. }
  113. }