暫無描述

Verify.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /*
  3. * Copyright 2008 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. use Firebase\JWT\ExpiredException as ExpiredExceptionV3;
  18. use Firebase\JWT\SignatureInvalidException;
  19. use GuzzleHttp\Client;
  20. use GuzzleHttp\ClientInterface;
  21. use Psr\Cache\CacheItemPoolInterface;
  22. use Google\Auth\Cache\MemoryCacheItemPool;
  23. use Stash\Driver\FileSystem;
  24. use Stash\Pool;
  25. /**
  26. * Wrapper around Google Access Tokens which provides convenience functions
  27. *
  28. */
  29. class Google_AccessToken_Verify
  30. {
  31. const FEDERATED_SIGNON_CERT_URL = 'https://www.googleapis.com/oauth2/v3/certs';
  32. const OAUTH2_ISSUER = 'accounts.google.com';
  33. const OAUTH2_ISSUER_HTTPS = 'https://accounts.google.com';
  34. /**
  35. * @var GuzzleHttp\ClientInterface The http client
  36. */
  37. private $http;
  38. /**
  39. * @var Psr\Cache\CacheItemPoolInterface cache class
  40. */
  41. private $cache;
  42. /**
  43. * Instantiates the class, but does not initiate the login flow, leaving it
  44. * to the discretion of the caller.
  45. */
  46. public function __construct(
  47. ClientInterface $http = null,
  48. CacheItemPoolInterface $cache = null,
  49. $jwt = null
  50. ) {
  51. if (null === $http) {
  52. $http = new Client();
  53. }
  54. if (null === $cache) {
  55. $cache = new MemoryCacheItemPool;
  56. }
  57. $this->http = $http;
  58. $this->cache = $cache;
  59. $this->jwt = $jwt ?: $this->getJwtService();
  60. }
  61. /**
  62. * Verifies an id token and returns the authenticated apiLoginTicket.
  63. * Throws an exception if the id token is not valid.
  64. * The audience parameter can be used to control which id tokens are
  65. * accepted. By default, the id token must have been issued to this OAuth2 client.
  66. *
  67. * @param $audience
  68. * @return array the token payload, if successful
  69. */
  70. public function verifyIdToken($idToken, $audience = null)
  71. {
  72. if (empty($idToken)) {
  73. throw new LogicException('id_token cannot be null');
  74. }
  75. // set phpseclib constants if applicable
  76. $this->setPhpsecConstants();
  77. // Check signature
  78. $certs = $this->getFederatedSignOnCerts();
  79. foreach ($certs as $cert) {
  80. $bigIntClass = $this->getBigIntClass();
  81. $rsaClass = $this->getRsaClass();
  82. $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256);
  83. $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256);
  84. $rsa = new $rsaClass();
  85. $rsa->loadKey(array('n' => $modulus, 'e' => $exponent));
  86. try {
  87. $payload = $this->jwt->decode(
  88. $idToken,
  89. $rsa->getPublicKey(),
  90. array('RS256')
  91. );
  92. if (property_exists($payload, 'aud')) {
  93. if ($audience && $payload->aud != $audience) {
  94. return false;
  95. }
  96. }
  97. // support HTTP and HTTPS issuers
  98. // @see https://developers.google.com/identity/sign-in/web/backend-auth
  99. $issuers = array(self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS);
  100. if (!isset($payload->iss) || !in_array($payload->iss, $issuers)) {
  101. return false;
  102. }
  103. return (array) $payload;
  104. } catch (ExpiredException $e) {
  105. return false;
  106. } catch (ExpiredExceptionV3 $e) {
  107. return false;
  108. } catch (SignatureInvalidException $e) {
  109. // continue
  110. } catch (DomainException $e) {
  111. // continue
  112. }
  113. }
  114. return false;
  115. }
  116. private function getCache()
  117. {
  118. return $this->cache;
  119. }
  120. /**
  121. * Retrieve and cache a certificates file.
  122. *
  123. * @param $url string location
  124. * @throws Google_Exception
  125. * @return array certificates
  126. */
  127. private function retrieveCertsFromLocation($url)
  128. {
  129. // If we're retrieving a local file, just grab it.
  130. if (0 !== strpos($url, 'http')) {
  131. if (!$file = file_get_contents($url)) {
  132. throw new Google_Exception(
  133. "Failed to retrieve verification certificates: '" .
  134. $url . "'."
  135. );
  136. }
  137. return json_decode($file, true);
  138. }
  139. $response = $this->http->get($url);
  140. if ($response->getStatusCode() == 200) {
  141. return json_decode((string) $response->getBody(), true);
  142. }
  143. throw new Google_Exception(
  144. sprintf(
  145. 'Failed to retrieve verification certificates: "%s".',
  146. $response->getBody()->getContents()
  147. ),
  148. $response->getStatusCode()
  149. );
  150. }
  151. // Gets federated sign-on certificates to use for verifying identity tokens.
  152. // Returns certs as array structure, where keys are key ids, and values
  153. // are PEM encoded certificates.
  154. private function getFederatedSignOnCerts()
  155. {
  156. $certs = null;
  157. if ($cache = $this->getCache()) {
  158. $cacheItem = $cache->getItem('federated_signon_certs_v3');
  159. $certs = $cacheItem->get();
  160. }
  161. if (!$certs) {
  162. $certs = $this->retrieveCertsFromLocation(
  163. self::FEDERATED_SIGNON_CERT_URL
  164. );
  165. if ($cache) {
  166. $cacheItem->expiresAt(new DateTime('+1 hour'));
  167. $cacheItem->set($certs);
  168. $cache->save($cacheItem);
  169. }
  170. }
  171. if (!isset($certs['keys'])) {
  172. throw new InvalidArgumentException(
  173. 'federated sign-on certs expects "keys" to be set'
  174. );
  175. }
  176. return $certs['keys'];
  177. }
  178. private function getJwtService()
  179. {
  180. $jwtClass = 'JWT';
  181. if (class_exists('\Firebase\JWT\JWT')) {
  182. $jwtClass = 'Firebase\JWT\JWT';
  183. }
  184. if (property_exists($jwtClass, 'leeway') && $jwtClass::$leeway < 1) {
  185. // Ensures JWT leeway is at least 1
  186. // @see https://github.com/google/google-api-php-client/issues/827
  187. $jwtClass::$leeway = 1;
  188. }
  189. return new $jwtClass;
  190. }
  191. private function getRsaClass()
  192. {
  193. if (class_exists('phpseclib\Crypt\RSA')) {
  194. return 'phpseclib\Crypt\RSA';
  195. }
  196. return 'Crypt_RSA';
  197. }
  198. private function getBigIntClass()
  199. {
  200. if (class_exists('phpseclib\Math\BigInteger')) {
  201. return 'phpseclib\Math\BigInteger';
  202. }
  203. return 'Math_BigInteger';
  204. }
  205. private function getOpenSslConstant()
  206. {
  207. if (class_exists('phpseclib\Crypt\RSA')) {
  208. return 'phpseclib\Crypt\RSA::MODE_OPENSSL';
  209. }
  210. if (class_exists('Crypt_RSA')) {
  211. return 'CRYPT_RSA_MODE_OPENSSL';
  212. }
  213. throw new \Exception('Cannot find RSA class');
  214. }
  215. /**
  216. * phpseclib calls "phpinfo" by default, which requires special
  217. * whitelisting in the AppEngine VM environment. This function
  218. * sets constants to bypass the need for phpseclib to check phpinfo
  219. *
  220. * @see phpseclib/Math/BigInteger
  221. * @see https://github.com/GoogleCloudPlatform/getting-started-php/issues/85
  222. */
  223. private function setPhpsecConstants()
  224. {
  225. if (filter_var(getenv('GAE_VM'), FILTER_VALIDATE_BOOLEAN)) {
  226. if (!defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) {
  227. define('MATH_BIGINTEGER_OPENSSL_ENABLED', true);
  228. }
  229. if (!defined('CRYPT_RSA_MODE')) {
  230. define('CRYPT_RSA_MODE', constant($this->getOpenSslConstant()));
  231. }
  232. }
  233. }
  234. }