Skip to content

Commit d9c87f2

Browse files
committed
try to decode the userinfo response like a JWT if it's not a raw JSON string
Signed-off-by: Julien Veyssier <[email protected]>
1 parent 5952f42 commit d9c87f2

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lib/Service/OIDCService.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace OCA\UserOIDC\Service;
1111

1212
use OCA\UserOIDC\Db\Provider;
13+
use OCA\UserOIDC\Vendor\Firebase\JWT\JWT;
1314
use OCP\Http\Client\IClientService;
1415
use OCP\Security\ICrypto;
1516
use Psr\Log\LoggerInterface;
@@ -38,11 +39,34 @@ public function userinfo(Provider $provider, string $accessToken): array {
3839
'Authorization' => 'Bearer ' . $accessToken,
3940
],
4041
];
42+
4143
try {
42-
return json_decode($client->get($url, $options)->getBody(), true);
44+
$userInfoResponse = $client->get($url, $options)->getBody();
4345
} catch (Throwable $e) {
46+
$this->logger->error('Request to the userinfo endpoint failed', ['exception' => $e]);
4447
return [];
4548
}
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 [];
4670
}
4771

4872
public function introspection(Provider $provider, string $accessToken): array {

0 commit comments

Comments
 (0)