1111
1212use OCA \UserOIDC \Db \Provider ;
1313use OCA \UserOIDC \Helper \HttpClientHelper ;
14+ use OCA \UserOIDC \Vendor \Firebase \JWT \JWT ;
1415use OCP \Security \ICrypto ;
1516use Psr \Log \LoggerInterface ;
1617use 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