Passed
Pull Request — master (#38)
by Dante
01:49
created

BaseClient::refreshTokens()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 17
rs 9.9666
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2022 Atlas Srl, ChannelWeb Srl, Chialab Srl
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 */
10
11
namespace BEdita\SDK;
12
13
use GuzzleHttp\Psr7\Request;
14
use GuzzleHttp\Psr7\Uri;
15
use Http\Adapter\Guzzle7\Client;
16
use Psr\Http\Message\ResponseInterface;
17
use WoohooLabs\Yang\JsonApi\Client\JsonApiClient;
18
19
class BaseClient
20
{
21
    use LogTrait;
22
23
    /**
24
     * Last response.
25
     *
26
     * @var \Psr\Http\Message\ResponseInterface
27
     */
28
    private $response = null;
29
30
    /**
31
     * BEdita4 API base URL
32
     *
33
     * @var string
34
     */
35
    private $apiBaseUrl = null;
36
37
    /**
38
     * BEdita4 API KEY
39
     *
40
     * @var string
41
     */
42
    private $apiKey = null;
43
44
    /**
45
     * Default headers in request
46
     *
47
     * @var array
48
     */
49
    private $defaultHeaders = [
50
        'Accept' => 'application/vnd.api+json',
51
    ];
52
53
    /**
54
     * Default headers in request
55
     *
56
     * @var array
57
     */
58
    private $defaultContentTypeHeader = [
59
        'Content-Type' => 'application/json',
60
    ];
61
62
    /**
63
     * JWT Auth tokens
64
     *
65
     * @var array
66
     */
67
    private $tokens = [];
68
69
    /**
70
     * JSON API BEdita4 client
71
     *
72
     * @var \WoohooLabs\Yang\JsonApi\Client\JsonApiClient
73
     */
74
    private $jsonApiClient = null;
75
76
    /**
77
     * Setup main client options:
78
     *  - API base URL
79
     *  - API KEY
80
     *  - Auth tokens 'jwt' and 'renew' (optional)
81
     *
82
     * @param string $apiUrl API base URL
83
     * @param string|null $apiKey API key
84
     * @param array $tokens JWT Autorization tokens as associative array ['jwt' => '###', 'renew' => '###']
85
     * @param array $guzzleConfig Additional default configuration for GuzzleHTTP client.
86
     * @return void
87
     */
88
    public function __construct(string $apiUrl, ?string $apiKey = null, array $tokens = [], array $guzzleConfig = [])
89
    {
90
        $this->apiBaseUrl = $apiUrl;
91
        $this->apiKey = $apiKey;
92
93
        $this->defaultHeaders['X-Api-Key'] = $this->apiKey;
94
        $this->setupTokens($tokens);
95
96
        // setup an asynchronous JSON API client
97
        $guzzleClient = Client::createWithConfig($guzzleConfig);
98
        $this->jsonApiClient = new JsonApiClient($guzzleClient);
99
    }
100
101
    /**
102
     * Setup JWT access and refresh tokens.
103
     *
104
     * @param array $tokens JWT tokens as associative array ['jwt' => '###', 'renew' => '###']
105
     * @return void
106
     */
107
    public function setupTokens(array $tokens): void
108
    {
109
        $this->tokens = $tokens;
110
        if (!empty($tokens['jwt'])) {
111
            $this->defaultHeaders['Authorization'] = sprintf('Bearer %s', $tokens['jwt']);
112
        } else {
113
            unset($this->defaultHeaders['Authorization']);
114
        }
115
    }
116
117
    /**
118
     * Get default headers in use on every request
119
     *
120
     * @return array Default headers
121
     */
122
    public function getDefaultHeaders(): array
123
    {
124
        return $this->defaultHeaders;
125
    }
126
127
    /**
128
     * Get API base URL used tokens
129
     *
130
     * @return string API base URL
131
     */
132
    public function getApiBaseUrl(): string
133
    {
134
        return $this->apiBaseUrl;
135
    }
136
137
    /**
138
     * Get current used tokens
139
     *
140
     * @return array Current tokens
141
     */
142
    public function getTokens(): array
143
    {
144
        return $this->tokens;
145
    }
146
147
    /**
148
     * Get last HTTP response
149
     *
150
     * @return ResponseInterface|null Response PSR interface
151
     */
152
    public function getResponse(): ?ResponseInterface
153
    {
154
        return $this->response;
155
    }
156
157
    /**
158
     * Get HTTP response status code
159
     * Return null if no response is available
160
     *
161
     * @return int|null Status code.
162
     */
163
    public function getStatusCode(): ?int
164
    {
165
        return $this->response ? $this->response->getStatusCode() : null;
166
    }
167
168
    /**
169
     * Get HTTP response status message
170
     * Return null if no response is available
171
     *
172
     * @return string|null Message related to status code.
173
     */
174
    public function getStatusMessage(): ?string
175
    {
176
        return $this->response ? $this->response->getReasonPhrase() : null;
177
    }
178
179
    /**
180
     * Get response body serialized into a PHP array
181
     *
182
     * @return array|null Response body as PHP array.
183
     */
184
    public function getResponseBody(): ?array
185
    {
186
        $response = $this->getResponse();
187
        if (empty($response)) {
188
            return null;
189
        }
190
        $responseBody = json_decode((string)$response->getBody(), true);
191
        if (!is_array($responseBody)) {
192
            return null;
193
        }
194
195
        return $responseBody;
196
    }
197
198
    /**
199
     * Refresh JWT access token.
200
     *
201
     * On success `$this->tokens` data will be updated with new access and renew tokens.
202
     *
203
     * @throws \BadMethodCallException Throws an exception if client has no renew token available.
204
     * @throws \Cake\Network\Exception\ServiceUnavailableException Throws an exception if server response doesn't
205
     *      include the expected data.
206
     * @return void
207
     * @throws BEditaClientException Throws an exception if server response code is not 20x.
208
     */
209
    public function refreshTokens(): void
210
    {
211
        if (empty($this->tokens['renew'])) {
212
            throw new \BadMethodCallException('You must be logged in to renew token');
213
        }
214
215
        $headers = [
216
            'Authorization' => sprintf('Bearer %s', $this->tokens['renew']),
217
        ];
218
219
        $this->sendRequest('POST', '/auth', [], $headers);
220
        $body = $this->getResponseBody();
221
        if (empty($body['meta']['jwt'])) {
222
            throw new BEditaClientException('Invalid response from server');
223
        }
224
225
        $this->setupTokens($body['meta']);
226
    }
227
228
    /**
229
     * Send a generic JSON API request with a basic retry policy on expired token exception.
230
     *
231
     * @param string $method HTTP Method.
232
     * @param string $path Endpoint URL path.
233
     * @param array|null $query Query string parameters.
234
     * @param string[]|null $headers Custom request headers.
235
     * @param string|resource|\Psr\Http\Message\StreamInterface|null $body Request body.
236
     * @return \Psr\Http\Message\ResponseInterface
237
     */
238
    protected function sendRequestRetry(string $method, string $path, ?array $query = null, ?array $headers = null, $body = null): ResponseInterface
239
    {
240
        try {
241
            return $this->sendRequest($method, $path, $query, $headers, $body);
242
        } catch (BEditaClientException $e) {
243
            // Handle error.
244
            $attributes = $e->getAttributes();
245
            if ($e->getCode() !== 401 || empty($attributes['code']) || $attributes['code'] !== 'be_token_expired') {
246
                // Not an expired token's fault.
247
                throw $e;
248
            }
249
250
            // Refresh and retry.
251
            $this->refreshTokens();
252
            unset($headers['Authorization']);
253
254
            return $this->sendRequest($method, $path, $query, $headers, $body);
255
        }
256
    }
257
258
    /**
259
     * Send a generic JSON API request and retrieve response $this->response
260
     *
261
     * @param string $method HTTP Method.
262
     * @param string $path Endpoint URL path (with or without starting `/`) or absolute API path
263
     * @param array|null $query Query string parameters.
264
     * @param string[]|null $headers Custom request headers.
265
     * @param string|resource|\Psr\Http\Message\StreamInterface|null $body Request body.
266
     * @return \Psr\Http\Message\ResponseInterface
267
     * @throws BEditaClientException Throws an exception if server response code is not 20x.
268
     */
269
    protected function sendRequest(string $method, string $path, ?array $query = null, ?array $headers = null, $body = null): ResponseInterface
270
    {
271
        $uri = $this->requestUri($path, $query);
272
        $headers = array_merge($this->defaultHeaders, (array)$headers);
273
274
        // set default `Content-Type` if not set and $body not empty
275
        if (!empty($body)) {
276
            $headers = array_merge($this->defaultContentTypeHeader, $headers);
277
        }
278
279
        // Send the request synchronously to retrieve the response.
280
        // Request and response log performed only if configured via `initLogger()`
281
        $request = new Request($method, $uri, $headers, $body);
282
        $this->logRequest($request);
283
        $this->response = $this->jsonApiClient->sendRequest($request);
284
        $this->logResponse($this->response);
285
        if ($this->getStatusCode() >= 400) {
286
            // Something bad just happened.
287
            $response = $this->getResponseBody();
288
            // Message will be 'error` array, if absent use status massage
289
            $message = empty($response['error']) ? $this->getStatusMessage() : $response['error'];
290
            throw new BEditaClientException($message, $this->getStatusCode());
291
        }
292
293
        return $this->response;
294
    }
295
296
    /**
297
     * Create request URI from path.
298
     * If path is absolute, i.e. it starts with 'http://' or 'https://', path is unchanged.
299
     * Otherwise `$this->apiBaseUrl` is prefixed, prepending a `/` if necessary.
300
     *
301
     * @param string $path Endpoint URL path (with or without starting `/`) or absolute API path
302
     * @param array|null $query Query string parameters.
303
     * @return Uri
304
     */
305
    protected function requestUri(string $path, ?array $query = null): Uri
306
    {
307
        if (strpos($path, 'https://') !== 0 && strpos($path, 'http://') !== 0) {
308
            if (substr($path, 0, 1) !== '/') {
309
                $path = '/' . $path;
310
            }
311
            $path = $this->apiBaseUrl . $path;
312
        }
313
        $uri = new Uri($path);
314
315
        // if path contains query strings, remove them from path and add them to query filter
316
        parse_str($uri->getQuery(), $uriQuery);
317
        if ($query) {
318
            $query = array_merge((array)$uriQuery, (array)$query);
319
            $uri = $uri->withQuery(http_build_query($query));
320
        }
321
322
        return $uri;
323
    }
324
325
    /**
326
     * Unset Authorization from defaultHeaders.
327
     *
328
     * @return void
329
     */
330
    protected function unsetAuthorization(): void
331
    {
332
        if (!array_key_exists('Authorization', $this->defaultHeaders)) {
333
            return;
334
        }
335
        unset($this->defaultHeaders['Authorization']);
336
    }
337
}
338