1
0

JwtAccessToken.php 3.1 KB

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