1 | <?php |
||
17 | class TokenManager implements LoggerAwareInterface |
||
18 | { |
||
19 | use LoggerAwareTrait; |
||
20 | |||
21 | /** |
||
22 | * @var ClientInterface |
||
23 | */ |
||
24 | private $httpClient; |
||
25 | |||
26 | /** |
||
27 | * @var ApiCredentials |
||
28 | */ |
||
29 | protected $credentials; |
||
30 | |||
31 | /** |
||
32 | * @var TokenExtractor |
||
33 | */ |
||
34 | private $tokenExtractor; |
||
35 | |||
36 | /** |
||
37 | * @var Token |
||
38 | */ |
||
39 | private $accessToken; |
||
40 | |||
41 | /** |
||
42 | * @var Token |
||
43 | */ |
||
44 | private $refreshToken; |
||
45 | |||
46 | /** |
||
47 | * TokenManager constructor. |
||
48 | * |
||
49 | * @param ClientInterface $httpClient |
||
50 | * @param ApiCredentials $credentials |
||
51 | */ |
||
52 | 2 | public function __construct( |
|
61 | |||
62 | /** |
||
63 | * Create completely fresh Access + Refresh tokens. |
||
64 | * |
||
65 | * @TODO Implement proper error handling |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | protected function createNewTokens() |
||
102 | |||
103 | /** |
||
104 | * Create a new access token for a video manager using a refresh token. |
||
105 | * |
||
106 | * @param Token $refreshToken |
||
107 | * @param int $videoManagerId |
||
108 | * |
||
109 | * @return Token |
||
110 | */ |
||
111 | protected function createAccessTokenFromRefreshToken(Token $refreshToken, $videoManagerId) |
||
112 | { |
||
113 | $logger = $this->getLogger(); |
||
114 | $logger->debug('Starting request to create fresh access token from refresh token'); |
||
115 | |||
116 | $response = $this->httpClient->post(sprintf('auth/refresh/%d', $videoManagerId), [ |
||
117 | 'json' => [ |
||
118 | 'refreshToken' => $refreshToken->getTokenString(), |
||
119 | ], |
||
120 | 'headers' => [ |
||
121 | 'accept: application/json', |
||
122 | 'cache-control: no-cache', |
||
123 | 'content-type: application/json', |
||
124 | ], |
||
125 | ]); |
||
126 | |||
127 | $data = \json_decode($response->getBody(), true); |
||
128 | $logger->debug('Successfully retrieved new access token', $data); |
||
129 | |||
130 | return new Token( |
||
131 | $data['accessToken'], |
||
132 | $this->tokenExtractor->extract($data['accessToken']), |
||
133 | $videoManagerId |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Log information about which tokens we have. |
||
139 | */ |
||
140 | protected function logTokenData() |
||
141 | { |
||
142 | $this->getLogger()->debug('Token information', [ |
||
143 | 'accessTokenExists' => isset($this->accessToken), |
||
144 | 'accessTokenExpiration' => isset($this->accessToken) ? $this->accessToken->getTokenData()['exp'] : null, |
||
145 | 'accessTokenHasExpired' => isset($this->accessToken) ? $this->accessToken->expired() : null, |
||
146 | 'refreshTokenExists' => isset($this->refreshToken), |
||
147 | 'refreshTokenExpiration' => isset($this->refreshToken) ? $this->refreshToken->getTokenData()['exp'] : null, |
||
148 | 'refreshTokenHasExpired' => isset($this->refreshToken) ? $this->refreshToken->expired() : null, |
||
149 | 'localTime' => time(), |
||
150 | ]); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Retrieve a valid token. |
||
155 | * |
||
156 | * @param int $videoManagerId Which video manager a token is requested for |
||
157 | * @TODO Refactor token storage to support multiple re-usable tokens, one per video manager |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function getToken($videoManagerId = null) |
||
201 | } |
||
202 |