6
6
7
7
use Illuminate \Http \Exceptions \HttpResponseException ;
8
8
use Illuminate \Http \RedirectResponse ;
9
+ use Illuminate \Support \Facades \Http ;
9
10
use Illuminate \Support \Facades \Session ;
10
11
use Illuminate \Support \Str ;
11
12
use Jumbojett \OpenIDConnectClientException ;
@@ -19,6 +20,15 @@ class OpenIDConnectClient extends \Jumbojett\OpenIDConnectClient
19
20
{
20
21
protected ?JweDecryptInterface $ jweDecrypter ;
21
22
protected ?OpenIDConfiguration $ openIDConfiguration ;
23
+ /**
24
+ * @var int|null Response code from the server
25
+ */
26
+ protected ?int $ responseCode ;
27
+
28
+ /**
29
+ * @var string|null Content type from the server
30
+ */
31
+ private ?string $ responseContentType ;
22
32
23
33
public function __construct (
24
34
?string $ providerUrl = null ,
@@ -95,7 +105,6 @@ protected function handleJweResponse($jwe): string
95
105
* @param string|string[]|bool|null $default optional
96
106
* @throws OpenIDConnectClientException
97
107
* @return string|string[]|bool
98
- * @psalm-suppress ImplementedReturnTypeMismatch
99
108
*/
100
109
protected function getWellKnownConfigValue ($ param , $ default = null ): string |array |bool
101
110
{
@@ -156,4 +165,97 @@ protected function getAuthorizationEndpoint(): string
156
165
157
166
return $ authorizationEndpoint ;
158
167
}
168
+
169
+ /**
170
+ * Override the fetchURL method to use Laravel HTTP client.
171
+ * This uses Guzzle in the background.
172
+ *
173
+ * @param string $url
174
+ * @param string | null $post_body string If this is set the post type will be POST
175
+ * @param array<array-key, mixed> $headers Extra headers to be sent with the request.
176
+ * @return string
177
+ * @throws OpenIDConnectClientException
178
+ */
179
+ protected function fetchURL (string $ url , string $ post_body = null , array $ headers = []): string
180
+ {
181
+ $ pendingRequest = Http::withUserAgent ($ this ->getUserAgent ())
182
+ ->timeout ($ this ->timeOut )
183
+ ->withOptions ([
184
+ 'verify ' => $ this ->getCertPath () ?: $ this ->getVerifyPeer ()
185
+ ]);
186
+
187
+ if (count ($ headers ) > 0 ) {
188
+ $ pendingRequest ->withHeaders ($ this ->reformatHeaders ($ headers ));
189
+ }
190
+
191
+ if ($ post_body === null ) {
192
+ $ request = $ pendingRequest ->get ($ url );
193
+ } else {
194
+ $ isJson = is_object (json_decode ($ post_body , false ));
195
+
196
+ $ request = $ pendingRequest
197
+ ->withBody ($ post_body , $ isJson ? 'application/json ' : 'application/x-www-form-urlencoded ' )
198
+ ->post ($ url );
199
+ }
200
+
201
+ $ this ->responseCode = $ request ->status ();
202
+ $ this ->responseContentType = $ request ->header ('Content-Type ' );
203
+
204
+ if ($ request ->failed ()) {
205
+ throw new OpenIDConnectClientException (
206
+ 'Request failed with status code ' . $ this ->responseCode . ' ' . $ request ->reason ()
207
+ );
208
+ }
209
+
210
+ return $ request ->body ();
211
+ }
212
+
213
+ /**
214
+ * Get the response code from last action/curl request.
215
+ *
216
+ * @return int
217
+ */
218
+ public function getResponseCode (): int
219
+ {
220
+ return $ this ->responseCode ?? 0 ;
221
+ }
222
+
223
+ /**
224
+ * Get the content type from last action/curl request.
225
+ *
226
+ * @return string|null
227
+ */
228
+ public function getResponseContentType (): ?string
229
+ {
230
+ return $ this ->responseContentType ;
231
+ }
232
+
233
+ public function setTlsVerify (bool |string $ tlsVerify ): void
234
+ {
235
+ $ verify = (bool )$ tlsVerify ;
236
+ $ this ->setVerifyHost ($ verify );
237
+ $ this ->setVerifyPeer ($ verify );
238
+
239
+ if (is_string ($ tlsVerify )) {
240
+ $ this ->setCertPath ($ tlsVerify );
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Reformat the headers from string to array for Guzzle.
246
+ *
247
+ * @param array<string> $headers
248
+ * @return array<string, array<int, string>>
249
+ */
250
+ protected function reformatHeaders (array $ headers ): array
251
+ {
252
+ $ result = [];
253
+
254
+ foreach ($ headers as $ header ) {
255
+ [$ key , $ value ] = explode (": " , $ header , 2 );
256
+ $ result [$ key ] = [$ value ];
257
+ }
258
+
259
+ return $ result ;
260
+ }
159
261
}
0 commit comments