Cheetah
JwtAccessToken.php
Go to the documentation of this file.
1 <?php
2 
3 namespace OAuth2\Storage;
4 
7 
13 {
14  protected $publicKeyStorage;
15  protected $tokenStorage;
16  protected $encryptionUtil;
17 
26  {
27  $this->publicKeyStorage = $publicKeyStorage;
28  $this->tokenStorage = $tokenStorage;
29  if (is_null($encryptionUtil)) {
30  $encryptionUtil = new Jwt;
31  }
32  $this->encryptionUtil = $encryptionUtil;
33  }
34 
35  public function getAccessToken($oauth_token)
36  {
37  // just decode the token, don't verify
38  if (!$tokenData = $this->encryptionUtil->decode($oauth_token, null, false)) {
39  return false;
40  }
41 
42  $client_id = isset($tokenData['aud']) ? $tokenData['aud'] : null;
43  $public_key = $this->publicKeyStorage->getPublicKey($client_id);
44  $algorithm = $this->publicKeyStorage->getEncryptionAlgorithm($client_id);
45 
46  // now that we have the client_id, verify the token
47  if (false === $this->encryptionUtil->decode($oauth_token, $public_key, array($algorithm))) {
48  return false;
49  }
50 
51  // normalize the JWT claims to the format expected by other components in this library
52  return $this->convertJwtToOAuth2($tokenData);
53  }
54 
55  public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = null)
56  {
57  if ($this->tokenStorage) {
58  return $this->tokenStorage->setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope);
59  }
60  }
61 
62  public function unsetAccessToken($access_token)
63  {
64  if ($this->tokenStorage) {
65  return $this->tokenStorage->unsetAccessToken($access_token);
66  }
67  }
68 
69 
70  // converts a JWT access token into an OAuth2-friendly format
71  protected function convertJwtToOAuth2($tokenData)
72  {
73  $keyMapping = array(
74  'aud' => 'client_id',
75  'exp' => 'expires',
76  'sub' => 'user_id'
77  );
78 
79  foreach ($keyMapping as $jwtKey => $oauth2Key) {
80  if (isset($tokenData[$jwtKey])) {
81  $tokenData[$oauth2Key] = $tokenData[$jwtKey];
82  unset($tokenData[$jwtKey]);
83  }
84  }
85 
86  return $tokenData;
87  }
88 }
OAuth2\Storage\JwtAccessToken\setAccessToken
setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope=null)
Definition: JwtAccessToken.php:55
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
OAuth2\Storage\PublicKeyInterface
Definition: PublicKeyInterface.php:12
OAuth2\Storage\JwtAccessToken\$tokenStorage
$tokenStorage
Definition: JwtAccessToken.php:15
php
OAuth2\Encryption\Jwt
Definition: Jwt.php:10
OAuth2\Storage\JwtAccessToken\unsetAccessToken
unsetAccessToken($access_token)
Definition: JwtAccessToken.php:62
OAuth2\Storage\JwtAccessToken\getAccessToken
getAccessToken($oauth_token)
Definition: JwtAccessToken.php:35
OAuth2\Storage\JwtAccessTokenInterface
Definition: JwtAccessTokenInterface.php:12
OAuth2\Storage\JwtAccessToken\__construct
__construct(PublicKeyInterface $publicKeyStorage, AccessTokenInterface $tokenStorage=null, EncryptionInterface $encryptionUtil=null)
Definition: JwtAccessToken.php:25
OAuth2\Storage\JwtAccessToken\$publicKeyStorage
$publicKeyStorage
Definition: JwtAccessToken.php:14
OAuth2\Storage
Definition: AccessTokenInterface.php:3
OAuth2\Storage\JwtAccessToken\$encryptionUtil
$encryptionUtil
Definition: JwtAccessToken.php:16
OAuth2\Storage\JwtAccessToken\convertJwtToOAuth2
convertJwtToOAuth2($tokenData)
Definition: JwtAccessToken.php:71
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
OAuth2\Storage\JwtAccessToken
Definition: JwtAccessToken.php:13
OAuth2\Encryption\EncryptionInterface
Definition: EncryptionInterface.php:6
OAuth2\Storage\AccessTokenInterface
Definition: AccessTokenInterface.php:12