|
10 | 10 | namespace OCA\UserOIDC\Service; |
11 | 11 |
|
12 | 12 | use OCA\UserOIDC\Db\Provider; |
| 13 | +use OCA\UserOIDC\Vendor\Firebase\JWT\JWT; |
13 | 14 | use OCP\Http\Client\IClientService; |
14 | 15 | use OCP\Security\ICrypto; |
15 | 16 | use Psr\Log\LoggerInterface; |
@@ -38,11 +39,34 @@ public function userinfo(Provider $provider, string $accessToken): array { |
38 | 39 | 'Authorization' => 'Bearer ' . $accessToken, |
39 | 40 | ], |
40 | 41 | ]; |
| 42 | + |
41 | 43 | try { |
42 | | - return json_decode($client->get($url, $options)->getBody(), true); |
| 44 | + $userInfoResponse = $client->get($url, $options)->getBody(); |
43 | 45 | } catch (Throwable $e) { |
| 46 | + $this->logger->error('Request to the userinfo endpoint failed', ['exception' => $e]); |
44 | 47 | return []; |
45 | 48 | } |
| 49 | + |
| 50 | + // try to decode it like a JSON string |
| 51 | + try { |
| 52 | + return json_decode($userInfoResponse, true); |
| 53 | + } catch (Throwable) { |
| 54 | + $this->logger->debug('The userinfo response is not JSON'); |
| 55 | + } |
| 56 | + |
| 57 | + // try to decode it like a JWT token |
| 58 | + JWT::$leeway = 60; |
| 59 | + try { |
| 60 | + $jwks = $this->discoveryService->obtainJWK($provider, $userInfoResponse); |
| 61 | + $payload = JWT::decode($userInfoResponse, $jwks); |
| 62 | + $arrayPayload = json_decode(json_encode($payload), true); |
| 63 | + $this->logger->debug('JWT Decoded user info response', ['decoded_userinfo_response' => $arrayPayload]); |
| 64 | + return $arrayPayload; |
| 65 | + } catch (Throwable $e) { |
| 66 | + $this->logger->debug('Treating the userinfo response as a JWT token. Impossible to decode it:' . $e->getMessage()); |
| 67 | + } |
| 68 | + |
| 69 | + return []; |
46 | 70 | } |
47 | 71 |
|
48 | 72 | public function introspection(Provider $provider, string $accessToken): array { |
|
0 commit comments