| @@ 13-93 (lines=81) @@ | ||
| 10 | /** |
|
| 11 | * Access token class. |
|
| 12 | */ |
|
| 13 | class AccessToken implements AccessTokenInterface |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * @var string |
|
| 17 | */ |
|
| 18 | protected $hash; |
|
| 19 | ||
| 20 | /** |
|
| 21 | * @var int |
|
| 22 | */ |
|
| 23 | protected $expireIn; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * @var AccountInterface |
|
| 27 | */ |
|
| 28 | protected $account; |
|
| 29 | ||
| 30 | /** |
|
| 31 | * @var ApplicationInterface |
|
| 32 | */ |
|
| 33 | protected $application; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @see AccessTokenInterface::__construct() |
|
| 37 | */ |
|
| 38 | public function __construct( |
|
| 39 | ApplicationInterface $application, |
|
| 40 | AccountInterface $account = null, |
|
| 41 | $expireIn = AccessTokenInterface::DEFAULT_TTL, |
|
| 42 | $hash = null |
|
| 43 | ) { |
|
| 44 | $this->application = $application; |
|
| 45 | $this->account = $account; |
|
| 46 | $this->expireIn = $expireIn; |
|
| 47 | ||
| 48 | $this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword( |
|
| 49 | sprintf('[%s\o/%s]', $application->getSecret(), $account->getPassword() ?: time()), |
|
| 50 | uniqid(mt_rand(), true) |
|
| 51 | ); |
|
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * @see AccessTokenInterface::getHash() |
|
| 56 | */ |
|
| 57 | public function getHash() |
|
| 58 | { |
|
| 59 | return $this->hash; |
|
| 60 | } |
|
| 61 | ||
| 62 | /** |
|
| 63 | * @see AccessTokenInterface::getExpireIn() |
|
| 64 | */ |
|
| 65 | public function getExpireIn() |
|
| 66 | { |
|
| 67 | return $this->expireIn; |
|
| 68 | } |
|
| 69 | ||
| 70 | /** |
|
| 71 | * @see AccessTokenInterface::getAccount() |
|
| 72 | */ |
|
| 73 | public function getAccount() |
|
| 74 | { |
|
| 75 | return $this->account; |
|
| 76 | } |
|
| 77 | ||
| 78 | /** |
|
| 79 | * @see AccessTokenInterface::getApplication() |
|
| 80 | */ |
|
| 81 | public function getApplication() |
|
| 82 | { |
|
| 83 | return $this->application; |
|
| 84 | } |
|
| 85 | ||
| 86 | /** |
|
| 87 | * @see AccessTokenInterface::getRoles() |
|
| 88 | */ |
|
| 89 | public function getRoles() |
|
| 90 | { |
|
| 91 | return array_intersect($this->account->getRoles(), $this->application->getRoles()); |
|
| 92 | } |
|
| 93 | } |
|
| 94 | ||
| @@ 15-95 (lines=81) @@ | ||
| 12 | * |
|
| 13 | * @author Raphael De Freitas <[email protected]> |
|
| 14 | */ |
|
| 15 | class RefreshToken implements RefreshTokenInterface |
|
| 16 | { |
|
| 17 | /** |
|
| 18 | * @var string |
|
| 19 | */ |
|
| 20 | protected $hash; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * @var int |
|
| 24 | */ |
|
| 25 | protected $expireIn; |
|
| 26 | ||
| 27 | /** |
|
| 28 | * @var AccountInterface |
|
| 29 | */ |
|
| 30 | protected $account; |
|
| 31 | ||
| 32 | /** |
|
| 33 | * @var ApplicationInterface |
|
| 34 | */ |
|
| 35 | protected $application; |
|
| 36 | ||
| 37 | /** |
|
| 38 | * @see RefreshTokenInterface::__construct() |
|
| 39 | */ |
|
| 40 | public function __construct( |
|
| 41 | ApplicationInterface $application, |
|
| 42 | AccountInterface $account = null, |
|
| 43 | $expireIn = RefreshTokenInterface::DEFAULT_TTL, |
|
| 44 | $hash = null |
|
| 45 | ) { |
|
| 46 | $this->application = $application; |
|
| 47 | $this->account = $account; |
|
| 48 | $this->expireIn = $expireIn; |
|
| 49 | ||
| 50 | $this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword( |
|
| 51 | sprintf('[%s\o/%s]', $application->getSecret(), $account->getPassword() ?: time()), |
|
| 52 | uniqid(mt_rand(), true) |
|
| 53 | ); |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * @see RefreshTokenInterface::getHash() |
|
| 58 | */ |
|
| 59 | public function getHash() |
|
| 60 | { |
|
| 61 | return $this->hash; |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * @see RefreshTokenInterface::getExpireIn() |
|
| 66 | */ |
|
| 67 | public function getExpireIn() |
|
| 68 | { |
|
| 69 | return $this->expireIn; |
|
| 70 | } |
|
| 71 | ||
| 72 | /** |
|
| 73 | * @see RefreshTokenInterface::getAccount() |
|
| 74 | */ |
|
| 75 | public function getAccount() |
|
| 76 | { |
|
| 77 | return $this->account; |
|
| 78 | } |
|
| 79 | ||
| 80 | /** |
|
| 81 | * @see RefreshTokenInterface::getApplication() |
|
| 82 | */ |
|
| 83 | public function getApplication() |
|
| 84 | { |
|
| 85 | return $this->application; |
|
| 86 | } |
|
| 87 | ||
| 88 | /** |
|
| 89 | * @see RefreshTokenInterface::getRoles() |
|
| 90 | */ |
|
| 91 | public function getRoles() |
|
| 92 | { |
|
| 93 | return array_intersect($this->account->getRoles(), $this->application->getRoles()); |
|
| 94 | } |
|
| 95 | } |
|
| 96 | ||