| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright 2008 Google Inc. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
namespace Google\AccessToken; |
| 20 |
|
| 21 |
use DomainException; |
| 22 |
use Exception; |
| 23 |
use Firebase\JWT\CachedKeySet; |
| 24 |
use Firebase\JWT\ExpiredException; |
| 25 |
use Firebase\JWT\JWT; |
| 26 |
use Firebase\JWT\SignatureInvalidException; |
| 27 |
use Google\Auth\Cache\MemoryCacheItemPool; |
| 28 |
use GuzzleHttp\Client; |
| 29 |
use GuzzleHttp\ClientInterface as GuzzleClientInterface; |
| 30 |
use GuzzleHttp\Psr7\HttpFactory; |
| 31 |
use InvalidArgumentException; |
| 32 |
use LogicException; |
| 33 |
use Psr\Cache\CacheItemPoolInterface; |
| 34 |
use Psr\Http\Client\ClientInterface; |
| 35 |
|
| 36 |
/** |
| 37 |
* Wrapper around Google Access Tokens which provides convenience functions |
| 38 |
* |
| 39 |
*/ |
| 40 |
class Verify |
| 41 |
{ |
| 42 |
const FEDERATED_SIGNON_CERT_URL = 'https://www.googleapis.com/oauth2/v3/certs'; |
| 43 |
const OAUTH2_ISSUER = 'accounts.google.com'; |
| 44 |
const OAUTH2_ISSUER_HTTPS = 'https://accounts.google.com'; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var \Firebase\JWT\JWT |
| 48 |
*/ |
| 49 |
public JWT $jwt; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var \Firebase\JWT\CachedKeySet |
| 53 |
*/ |
| 54 |
private CachedKeySet $keySet; |
| 55 |
|
| 56 |
/** |
| 57 |
* Instantiates the class, but does not initiate the login flow, leaving it |
| 58 |
* to the discretion of the caller. |
| 59 |
*/ |
| 60 |
public function __construct( |
| 61 |
?GuzzleClientInterface $http = null, |
| 62 |
?CacheItemPoolInterface $cache = null, |
| 63 |
?JWT $jwt = null |
| 64 |
) { |
| 65 |
if (null === $http) { |
| 66 |
$http = new Client(); |
| 67 |
} |
| 68 |
|
| 69 |
if (null === $cache) { |
| 70 |
$cache = new MemoryCacheItemPool(); |
| 71 |
} |
| 72 |
|
| 73 |
if (!$http instanceof ClientInterface) { |
| 74 |
throw new InvalidArgumentException('http client must implement ' . ClientInterface::class); |
| 75 |
} |
| 76 |
|
| 77 |
$this->jwt = $jwt ?: $this->getJwtService(); |
| 78 |
$this->keySet = new CachedKeySet( |
| 79 |
self::FEDERATED_SIGNON_CERT_URL, |
| 80 |
$http, |
| 81 |
new HttpFactory(), |
| 82 |
$cache |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Verifies an id token and returns the authenticated apiLoginTicket. |
| 88 |
* Throws an exception if the id token is not valid. |
| 89 |
* The audience parameter can be used to control which id tokens are |
| 90 |
* accepted. By default, the id token must have been issued to this OAuth2 client. |
| 91 |
* |
| 92 |
* @param string $idToken the ID token in JWT format |
| 93 |
* @param string $audience Optional. The audience to verify against JWt "aud" |
| 94 |
* @return array|false the token payload, if successful |
| 95 |
*/ |
| 96 |
public function verifyIdToken($idToken, $audience = null) |
| 97 |
{ |
| 98 |
if (empty($idToken)) { |
| 99 |
throw new LogicException('id_token cannot be null'); |
| 100 |
} |
| 101 |
|
| 102 |
// Check signature |
| 103 |
try { |
| 104 |
$payload = ($this->jwt)->decode($idToken, $this->keySet); |
| 105 |
} catch (ExpiredException |SignatureInvalidException |DomainException) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
if (property_exists($payload, 'aud')) { |
| 110 |
if ($audience && $payload->aud != $audience) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// support HTTP and HTTPS issuers |
| 116 |
// @see https://developers.google.com/identity/sign-in/web/backend-auth |
| 117 |
$issuers = [self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS]; |
| 118 |
if (!isset($payload->iss) || !in_array($payload->iss, $issuers)) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
return (array) $payload; |
| 123 |
} |
| 124 |
|
| 125 |
private function getJwtService() |
| 126 |
{ |
| 127 |
$jwt = new JWT(); |
| 128 |
if ($jwt::$leeway < 1) { |
| 129 |
// Ensures JWT leeway is at least 1 |
| 130 |
// @see https://github.com/google/google-api-php-client/issues/827 |
| 131 |
$jwt::$leeway = 1; |
| 132 |
} |
| 133 |
|
| 134 |
return $jwt; |
| 135 |
} |
| 136 |
} |
| 137 |
|