| 1: | <?php |
| 2: | namespace Worldline\Acquiring\Sdk\Authentication; |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | class DefaultOAuth2TokenCache implements OAuth2TokenCache |
| 10: | { |
| 11: | const OAUTH2_ACCESS_TOKEN_PREFIX = 'acquiring.api.oauth2.access-token'; |
| 12: | const EXPIRATION_TIMESTAMP_PREFIX = 'acquiring.api.oauth2.expiration-timestamp'; |
| 13: | |
| 14: | |
| 15: | private $array; |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | public function __construct(&$array = array()) |
| 21: | { |
| 22: | $this->array = &$array; |
| 23: | } |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | public function getOAuth2AccessToken(string $tokenIdentifier) |
| 30: | { |
| 31: | $accessTokenKey = $this->addPrefix($tokenIdentifier, self::OAUTH2_ACCESS_TOKEN_PREFIX); |
| 32: | $timestampKey = $this->addPrefix($tokenIdentifier, self::EXPIRATION_TIMESTAMP_PREFIX); |
| 33: | if (isset($this->array[$accessTokenKey]) && isset($this->array[$timestampKey])) { |
| 34: | $expirationTimestamp = intval($this->array[$timestampKey]); |
| 35: | if ($expirationTimestamp > time()) { |
| 36: | return $this->array[$accessTokenKey]; |
| 37: | } |
| 38: | } |
| 39: | return null; |
| 40: | } |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | public function storeOAuth2AccessToken($tokenIdentifier, $oauth2AccessToken, $expirationTimestamp) |
| 48: | { |
| 49: | $this->array[$this->addPrefix($tokenIdentifier, self::OAUTH2_ACCESS_TOKEN_PREFIX)] = $oauth2AccessToken; |
| 50: | $this->array[$this->addPrefix($tokenIdentifier, self::EXPIRATION_TIMESTAMP_PREFIX)] = strval($expirationTimestamp); |
| 51: | } |
| 52: | |
| 53: | private function addPrefix($tokenIdentifier, $prefix) |
| 54: | { |
| 55: | if (!$tokenIdentifier) { |
| 56: | return $prefix; |
| 57: | } |
| 58: | return $prefix . '/' . $tokenIdentifier; |
| 59: | } |
| 60: | } |
| 61: | |