JwtAccessToken.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace OAuth2\Storage;
  3. use OAuth2\Encryption\EncryptionInterface;
  4. use OAuth2\Encryption\Jwt;
  5. /**
  6. *
  7. * @author Brent Shaffer <bshafs at gmail dot com>
  8. */
  9. class JwtAccessToken implements JwtAccessTokenInterface
  10. {
  11. protected $publicKeyStorage;
  12. protected $tokenStorage;
  13. protected $encryptionUtil;
  14. /**
  15. * @param OAuth2\Encryption\PublicKeyInterface $publicKeyStorage the public key encryption to use
  16. * @param OAuth2\Storage\AccessTokenInterface $tokenStorage OPTIONAL persist the access token to another storage. This is useful if
  17. * you want to retain access token grant information somewhere, but
  18. * is not necessary when using this grant type.
  19. * @param OAuth2\Encryption\EncryptionInterface $encryptionUtil OPTIONAL class to use for "encode" and "decode" functions.
  20. */
  21. public function __construct(PublicKeyInterface $publicKeyStorage, AccessTokenInterface $tokenStorage = null, EncryptionInterface $encryptionUtil = null)
  22. {
  23. $this->publicKeyStorage = $publicKeyStorage;
  24. $this->tokenStorage = $tokenStorage;
  25. if (is_null($encryptionUtil)) {
  26. $encryptionUtil = new Jwt;
  27. }
  28. $this->encryptionUtil = $encryptionUtil;
  29. }
  30. public function getAccessToken($oauth_token)
  31. {
  32. // just decode the token, don't verify
  33. if (!$tokenData = $this->encryptionUtil->decode($oauth_token, null, false)) {
  34. return false;
  35. }
  36. $client_id = isset($tokenData['aud']) ? $tokenData['aud'] : null;
  37. $public_key = $this->publicKeyStorage->getPublicKey($client_id);
  38. $algorithm = $this->publicKeyStorage->getEncryptionAlgorithm($client_id);
  39. // now that we have the client_id, verify the token
  40. if (false === $this->encryptionUtil->decode($oauth_token, $public_key, array($algorithm))) {
  41. return false;
  42. }
  43. // normalize the JWT claims to the format expected by other components in this library
  44. return $this->convertJwtToOAuth2($tokenData);
  45. }
  46. public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = null)
  47. {
  48. if ($this->tokenStorage) {
  49. return $this->tokenStorage->setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope);
  50. }
  51. }
  52. public function unsetAccessToken($access_token)
  53. {
  54. if ($this->tokenStorage) {
  55. return $this->tokenStorage->unsetAccessToken($access_token);
  56. }
  57. }
  58. // converts a JWT access token into an OAuth2-friendly format
  59. protected function convertJwtToOAuth2($tokenData)
  60. {
  61. $keyMapping = array(
  62. 'aud' => 'client_id',
  63. 'exp' => 'expires',
  64. 'sub' => 'user_id'
  65. );
  66. foreach ($keyMapping as $jwtKey => $oauth2Key) {
  67. if (isset($tokenData[$jwtKey])) {
  68. $tokenData[$oauth2Key] = $tokenData[$jwtKey];
  69. unset($tokenData[$jwtKey]);
  70. }
  71. }
  72. return $tokenData;
  73. }
  74. }