88
99class OIDC extends AuthenticationParent implements AuthenticationInterface
1010{
11- private $ providerConfig = array ();
11+ //** Cache for providerConfig */
12+ private $ providerConfigCache = null ;
1213
13- private function getEndpoints () {
14- if ( empty ( $ this -> providerConfig )) {
15- global $ sso_url , $ oidc_client_id , $ oidc_client_secret ;
14+ private function getProviderConfig () {
15+ global $ sso_url , $ oidc_client_id , $ oidc_client_secret ;
16+ if ( is_null ( $ this -> providerConfigCache )) {
1617
1718 $ ch = curl_init ();
1819 curl_setopt ($ ch , CURLOPT_URL , 'https:// ' . $ sso_url . '/.well-known/openid-configuration ' );
@@ -27,21 +28,21 @@ private function getEndpoints() {
2728 || !isset ($ newProviderConfig ->authorization_endpoint )
2829 || !isset ($ newProviderConfig ->token_endpoint )) {
2930 error_log ("OIDC Authentication provider replied with invalid JSON body " );
30- return ;
31+ return null ;
3132 }
3233 $ newProviderConfig ->b64ClientCreds = base64_encode (
3334 $ oidc_client_id . ": " . $ oidc_client_secret
3435 );
3536
36- $ this ->providerConfig = $ newProviderConfig ;
37+ $ this ->providerConfigCache = $ newProviderConfig ;
3738 }
39+ return $ this ->providerConfigCache ;
3840 }
3941
4042 private function getUser ($ token )
4143 {
42- $ this ->getEndpoints ();
4344 $ ch = curl_init ();
44- curl_setopt ($ ch , CURLOPT_URL , $ this ->providerConfig ->userinfo_endpoint );
45+ curl_setopt ($ ch , CURLOPT_URL , $ this ->getProviderConfig () ->userinfo_endpoint );
4546 curl_setopt ($ ch , CURLOPT_HEADER , 0 );
4647 curl_setopt ($ ch , CURLOPT_HTTPHEADER , array ('Authorization: Bearer ' . $ token ));
4748 curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
@@ -74,32 +75,30 @@ function check()
7475
7576 function authorise ()
7677 {
77- $ this ->getEndpoints ();
7878 global $ oidc_client_id ;
7979 $ redirect_url = Utils::filterParamFromUrl ($ _SERVER ["HTTP_REFERER " ], "code " );
8080
81- return ( $ this ->providerConfig ->authorization_endpoint .
81+ return ( $ this ->getProviderConfig () ->authorization_endpoint .
8282 '?response_type=code&client_id= ' . $ oidc_client_id .
8383 '&redirect_uri= ' . $ redirect_url
8484 );
8585 }
8686
8787 function authenticateByCode ($ code )
8888 {
89- $ this ->getEndpoints ();
9089 global $ cacert , $ oidc_client_secret , $ oidc_client_id , $ cookie_key ;
9190
9291 $ redirect_url = Utils::filterParamFromUrl ($ _SERVER ["HTTP_REFERER " ], "code " );
9392
9493 $ ch = curl_init ();
95- curl_setopt ($ ch , CURLOPT_URL , $ this ->providerConfig ->token_endpoint .
94+ curl_setopt ($ ch , CURLOPT_URL , $ this ->getProviderConfig () ->token_endpoint .
9695 '?grant_type=authorization_code&redirect_uri= ' .
9796 $ redirect_url .
9897 "&code= " . $ code
9998 );
10099 curl_setopt ($ ch , CURLOPT_HEADER , 0 );
101100 curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
102- curl_setopt ($ ch , CURLOPT_HTTPHEADER , array ('Authorization: Basic ' . $ this ->providerConfig ->b64ClientCreds ));
101+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , array ('Authorization: Basic ' . $ this ->getProviderConfig () ->b64ClientCreds ));
103102 $ response = curl_exec ($ ch );
104103 curl_close ($ ch );
105104
0 commit comments