Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
64.29% |
27 / 42 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
OAuth2TokenTrait | |
64.29% |
27 / 42 |
|
0.00% |
0 / 1 |
7.64 | |
0.00% |
0 / 1 |
getNewOAuth2Token | |
64.29% |
27 / 42 |
|
0.00% |
0 / 1 |
7.64 |
1 | <?php |
2 | |
3 | /** |
4 | * Trait OAuth2TokenTraitTest |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Moravian Library 2021. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package VuFind\ILS |
25 | * @author Josef Moravec <moravec@mzk.cz> |
26 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
27 | * @license https://opensource.org/licenses/gpl-2.0.php GNU General Public License |
28 | * @link https://knihovny.cz Main Page |
29 | */ |
30 | |
31 | declare(strict_types=1); |
32 | |
33 | namespace VuFind\ILS\Driver; |
34 | |
35 | use VuFind\Auth\AuthToken; |
36 | use VuFind\Exception\AuthToken as AuthTokenException; |
37 | |
38 | /** |
39 | * Trait OAuth2TokenTraitTest |
40 | * |
41 | * @category VuFind |
42 | * @package VuFind\ILS |
43 | * @author Josef Moravec <moravec@mzk.cz> |
44 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
45 | * @license https://opensource.org/licenses/gpl-2.0.php GNU General Public License |
46 | * @link https://knihovny.cz Main Page |
47 | */ |
48 | trait OAuth2TokenTrait |
49 | { |
50 | use \VuFindHttp\HttpServiceAwareTrait; |
51 | use \VuFind\Log\LoggerAwareTrait; |
52 | |
53 | /** |
54 | * Get new authorization token from API using given credentials |
55 | * |
56 | * @param string $tokenEndpoint URL of token endpoint |
57 | * @param string $clientId Client id |
58 | * @param string $clientSecret Client secret |
59 | * @param string $grantType Grant type (usually 'client_credentials') |
60 | * @param bool $useHttpBasic Use HTTP Basic authorization for getting token |
61 | * |
62 | * @return AuthToken |
63 | * @throws AuthTokenException |
64 | */ |
65 | public function getNewOAuth2Token( |
66 | string $tokenEndpoint, |
67 | string $clientId, |
68 | string $clientSecret, |
69 | string $grantType = 'client_credentials', |
70 | bool $useHttpBasic = false |
71 | ): \VuFind\Auth\AuthToken { |
72 | $client = $this->httpService->createClient($tokenEndpoint); |
73 | $client->setMethod('POST'); |
74 | $client->getRequest()->getHeaders()->addHeaderLine( |
75 | 'Content-Type', |
76 | 'application/x-www-form-urlencoded' |
77 | ); |
78 | |
79 | $postFields = ['grant_type' => $grantType]; |
80 | if ($useHttpBasic) { |
81 | $client->setAuth($clientId, $clientSecret); |
82 | } else { |
83 | $postFields['client_id'] = $clientId; |
84 | $postFields['client_secret'] = $clientSecret; |
85 | } |
86 | |
87 | $client->setParameterPost($postFields); |
88 | |
89 | try { |
90 | $response = $client->send(); |
91 | } catch (\Exception $e) { |
92 | $this->logError( |
93 | "POST request for '$tokenEndpoint' failed: " . $e->getMessage() |
94 | ); |
95 | throw new AuthTokenException( |
96 | 'Problem getting authorization token: Request failed' |
97 | ); |
98 | } |
99 | |
100 | if ($response->getStatusCode() != 200) { |
101 | $errorMessage = "Error while getting OAuth2 access token from '$tokenEndpoint' (status code " |
102 | . $response->getStatusCode() . '): ' . $response->getBody(); |
103 | $this->logError($errorMessage); |
104 | throw new AuthTokenException( |
105 | 'Problem getting authorization token: Bad status code returned' |
106 | ); |
107 | } |
108 | $tokenData = json_decode($response->getBody(), true); |
109 | |
110 | if ( |
111 | empty($tokenData['token_type']) |
112 | || empty($tokenData['access_token']) |
113 | ) { |
114 | $this->logError( |
115 | "Did not receive OAuth2 token from '$tokenEndpoint', response: " |
116 | . $response->getBody() |
117 | ); |
118 | throw new AuthTokenException( |
119 | 'Problem getting authorization token: Empty data' |
120 | ); |
121 | } |
122 | return new AuthToken( |
123 | $tokenData['access_token'], |
124 | $tokenData['expires_in'] ?? null, |
125 | $tokenData['token_type'] |
126 | ); |
127 | } |
128 | } |