Skip to content

Try to decode the userinfo response like a JWT #1117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/Service/OIDCService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\UserOIDC\Service;

use OCA\UserOIDC\Db\Provider;
use OCA\UserOIDC\Vendor\Firebase\JWT\JWT;
use OCP\Http\Client\IClientService;
use OCP\Security\ICrypto;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -38,11 +39,34 @@ public function userinfo(Provider $provider, string $accessToken): array {
'Authorization' => 'Bearer ' . $accessToken,
],
];

try {
return json_decode($client->get($url, $options)->getBody(), true);
$userInfoResponse = $client->get($url, $options)->getBody();
} catch (Throwable $e) {
$this->logger->error('Request to the userinfo endpoint failed', ['exception' => $e]);
return [];
}

// try to decode it like a JSON string
try {
return json_decode($userInfoResponse, true);
} catch (Throwable) {
$this->logger->debug('The userinfo response is not JSON');
}

// try to decode it like a JWT token
JWT::$leeway = 60;
try {
$jwks = $this->discoveryService->obtainJWK($provider, $userInfoResponse);
$payload = JWT::decode($userInfoResponse, $jwks);
$arrayPayload = json_decode(json_encode($payload), true);
$this->logger->debug('JWT Decoded user info response', ['decoded_userinfo_response' => $arrayPayload]);
return $arrayPayload;
} catch (Throwable $e) {
$this->logger->debug('Treating the userinfo response as a JWT token. Impossible to decode it:' . $e->getMessage());
}

return [];
}

public function introspection(Provider $provider, string $accessToken): array {
Expand Down
Loading