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() |
||
70 | { |
||
71 | $logger = $this->getLogger(); |
||
72 | $logger->debug('Starting request to create fresh access & refresh tokens'); |
||
73 | |||
74 | try { |
||
75 | $response = $this->httpClient->post('auth/login', [ |
||
76 | 'json' => [ |
||
77 | 'username' => $this->credentials->getUsername(), |
||
78 | 'password' => $this->credentials->getPassword(), |
||
79 | ], |
||
80 | 'headers' => [ |
||
81 | 'accept: application/json', |
||
82 | 'cache-control: no-cache', |
||
83 | 'content-type: application/json', |
||
84 | ], |
||
85 | ]); |
||
86 | |||
87 | $data = \json_decode($response->getBody(), true); |
||
88 | $logger->debug('Successfully retrieved new access & refresh tokens', $data); |
||
89 | |||
90 | return [ |
||
91 | 'accessToken' => new Token( |
||
92 | $data['accessToken'], |
||
93 | $this->tokenExtractor->extract($data['accessToken']), |
||
94 | $data['validForVideoManager'] |
||
95 | ), |
||
96 | 'refreshToken' => new Token( |
||
97 | $data['refreshToken'], |
||
98 | $this->tokenExtractor->extract($data['refreshToken']), |
||
99 | null |
||
100 | ), |
||
101 | ]; |
||
102 | } catch (\Exception $e) { |
||
103 | throw $e; // Just rethrow for now |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Create a new access token for a video manager using a refresh token. |
||
109 | * |
||
110 | * @param Token $refreshToken |
||
111 | * @param int $videoManagerId |
||
112 | * |
||
113 | * @return Token |
||
114 | */ |
||
115 | protected function createAccessTokenFromRefreshToken(Token $refreshToken, $videoManagerId) |
||
144 | |||
145 | /** |
||
146 | * Log information about which tokens we have. |
||
147 | */ |
||
148 | protected function logTokenData() |
||
160 | |||
161 | /** |
||
162 | * Retrieve a valid token. |
||
163 | * |
||
164 | * @param int $videoManagerId Which video manager a token is requested for |
||
165 | * @TODO Refactor token storage to support multiple re-usable tokens, one per video manager |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function getToken($videoManagerId = null) |
||
209 | } |
||
210 |