33 *
44 * Copyright MITRE 2020
55 *
6- * OpenIDConnectClient for PHP5
6+ * OpenIDConnectClient for PHP7+
77 * Author: Michael Jett <[email protected] > 88 *
99 * Licensed under the Apache License, Version 2.0 (the "License"); you may
2525
2626use Error ;
2727use Exception ;
28- use phpseclib3 \Crypt \PublicKeyLoader ;
2928use phpseclib3 \Crypt \RSA ;
3029use phpseclib3 \Math \BigInteger ;
3130use stdClass ;
@@ -145,12 +144,12 @@ class OpenIDConnectClient
145144 /**
146145 * @var int|null Response code from the server
147146 */
148- private $ responseCode ;
147+ protected $ responseCode ;
149148
150149 /**
151150 * @var string|null Content type from the server
152151 */
153- private $ responseContentType ;
152+ protected $ responseContentType ;
154153
155154 /**
156155 * @var array holds response types
@@ -380,7 +379,7 @@ public function authenticate(): bool
380379 $ accessToken = $ _REQUEST ['access_token ' ] ?? null ;
381380
382381 // Do an OpenID Connect session check
383- if (!isset ($ _REQUEST ['state ' ]) || ($ _REQUEST ['state ' ] !== $ this ->getState ())) {
382+ if (!isset ($ _REQUEST ['state ' ]) || ($ _REQUEST ['state ' ] !== $ this ->getState ())) {
384383 throw new OpenIDConnectClientException ('Unable to determine state ' );
385384 }
386385
@@ -691,6 +690,7 @@ public function getRedirectURL(): string
691690 if (isset ($ _SERVER ['HTTP_X_FORWARDED_PORT ' ])) {
692691 $ port = (int )$ _SERVER ['HTTP_X_FORWARDED_PORT ' ];
693692 } elseif (isset ($ _SERVER ['SERVER_PORT ' ])) {
693+ # keep this case - even if some tool claim it is unnecessary
694694 $ port = (int )$ _SERVER ['SERVER_PORT ' ];
695695 } elseif ($ protocol === 'https ' ) {
696696 $ port = 443 ;
@@ -1212,8 +1212,10 @@ protected function verifyJWTClaims($claims, string $accessToken = null): bool
12121212 $ len = ((int )$ bit )/16 ;
12131213 $ expected_at_hash = $ this ->urlEncode (substr (hash ('sha ' .$ bit , $ accessToken , true ), 0 , $ len ));
12141214 }
1215+ $ auds = $ claims ->aud ;
1216+ $ auds = is_array ( $ auds ) ? $ auds : [ $ auds ];
12151217 return (($ this ->validateIssuer ($ claims ->iss ))
1216- && (( $ claims -> aud === $ this -> clientID ) || in_array ($ this ->clientID , $ claims -> aud , true ))
1218+ && (in_array ($ this ->clientID , $ auds , true ))
12171219 && ($ claims ->sub === $ this ->getIdTokenPayload ()->sub )
12181220 && (!isset ($ claims ->nonce ) || $ claims ->nonce === $ this ->getNonce ())
12191221 && ( !isset ($ claims ->exp ) || ((is_int ($ claims ->exp )) && ($ claims ->exp >= time () - $ this ->leeway )))
@@ -1232,12 +1234,11 @@ protected function urlEncode(string $str): string
12321234 /**
12331235 * @param string $jwt encoded JWT
12341236 * @param int $section the section we would like to decode
1235- * @return object
1237+ * @return object|string|null
12361238 */
1237- protected function decodeJWT (string $ jwt , int $ section = 0 ): stdClass {
1238-
1239+ protected function decodeJWT (string $ jwt , int $ section = 0 ) {
12391240 $ parts = explode ('. ' , $ jwt );
1240- return json_decode (base64url_decode ($ parts [$ section ]), false );
1241+ return json_decode (base64url_decode ($ parts [$ section ] ?? '' ), false );
12411242 }
12421243
12431244 /**
@@ -1699,7 +1700,10 @@ public function revokeToken(string $token, string $token_type_hint = '', string
16991700 return json_decode ($ this ->fetchURL ($ revocation_endpoint , $ post_params , $ headers ), false );
17001701 }
17011702
1702- public function getClientName (): string
1703+ /**
1704+ * @return string|null
1705+ */
1706+ public function getClientName ()
17031707 {
17041708 return $ this ->clientName ;
17051709 }
@@ -1709,14 +1713,14 @@ public function setClientName(string $clientName) {
17091713 }
17101714
17111715 /**
1712- * @return string
1716+ * @return string|null
17131717 */
17141718 public function getClientID () {
17151719 return $ this ->clientID ;
17161720 }
17171721
17181722 /**
1719- * @return string
1723+ * @return string|null
17201724 */
17211725 public function getClientSecret () {
17221726 return $ this ->clientSecret ;
@@ -1731,17 +1735,30 @@ public function setAccessToken(string $accessToken) {
17311735 $ this ->accessToken = $ accessToken ;
17321736 }
17331737
1734- public function getAccessToken (): string
1738+ /**
1739+ * @return string|null
1740+ */
1741+ public function getAccessToken ()
17351742 {
17361743 return $ this ->accessToken ;
17371744 }
17381745
1739- public function getRefreshToken (): string
1746+ /**
1747+ * @return string|null
1748+ */
1749+ public function getRefreshToken ()
17401750 {
17411751 return $ this ->refreshToken ;
17421752 }
17431753
1744- public function getIdToken (): string
1754+ public function setIdToken (string $ idToken ) {
1755+ $ this ->idToken = $ idToken ;
1756+ }
1757+
1758+ /**
1759+ * @return string|null
1760+ */
1761+ public function getIdToken ()
17451762 {
17461763 return $ this ->idToken ;
17471764 }
@@ -1754,21 +1771,21 @@ public function getAccessTokenHeader() {
17541771 }
17551772
17561773 /**
1757- * @return object
1774+ * @return object|string|null
17581775 */
17591776 public function getAccessTokenPayload () {
17601777 return $ this ->decodeJWT ($ this ->accessToken , 1 );
17611778 }
17621779
17631780 /**
1764- * @return object
1781+ * @return object|string|null
17651782 */
17661783 public function getIdTokenHeader () {
17671784 return $ this ->decodeJWT ($ this ->idToken );
17681785 }
17691786
17701787 /**
1771- * @return object
1788+ * @return object|string|null
17721789 */
17731790 public function getIdTokenPayload () {
17741791 return $ this ->decodeJWT ($ this ->idToken , 1 );
0 commit comments