Skip to content

Commit ba69c16

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 <julien-nc@posteo.net>
1 parent b9ca582 commit ba69c16

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

lib/Service/OIDCService.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use OCA\UserOIDC\Db\Provider;
1313
use OCA\UserOIDC\Helper\HttpClientHelper;
14+
use OCA\UserOIDC\Vendor\Firebase\JWT\JWT;
1415
use OCP\Security\ICrypto;
1516
use Psr\Log\LoggerInterface;
1617
use Throwable;
@@ -37,11 +38,34 @@ public function userinfo(Provider $provider, string $accessToken): array {
3738
'Authorization' => 'Bearer ' . $accessToken,
3839
],
3940
];
41+
4042
try {
41-
return json_decode($this->clientService->get($url, [], $options), true);
43+
$userInfoResponse = $this->clientService->get($url, [], $options);
4244
} catch (Throwable $e) {
45+
$this->logger->error('Request to the userinfo endpoint failed', ['exception' => $e]);
4346
return [];
4447
}
48+
49+
// try to decode it like a JSON string
50+
try {
51+
return json_decode($userInfoResponse, true);
52+
} catch (Throwable) {
53+
$this->logger->debug('The userinfo response is not JSON');
54+
}
55+
56+
// try to decode it like a JWT token
57+
JWT::$leeway = 60;
58+
try {
59+
$jwks = $this->discoveryService->obtainJWK($provider, $userInfoResponse);
60+
$payload = JWT::decode($userInfoResponse, $jwks);
61+
$arrayPayload = json_decode(json_encode($payload), true);
62+
$this->logger->debug('JWT Decoded user info response', ['decoded_userinfo_response' => $arrayPayload]);
63+
return $arrayPayload;
64+
} catch (Throwable $e) {
65+
$this->logger->debug('Treating the userinfo response as a JWT token. Impossible to decode it:' . $e->getMessage());
66+
}
67+
68+
return [];
4569
}
4670

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

0 commit comments

Comments
 (0)