jwt = $jwt ?: $this->getJwtService(); $this->keySet = new CachedKeySet( self::FEDERATED_SIGNON_CERT_URL, $http, new HttpFactory(), $cache ); } /** * Verifies an id token and returns the authenticated apiLoginTicket. * Throws an exception if the id token is not valid. * The audience parameter can be used to control which id tokens are * accepted. By default, the id token must have been issued to this OAuth2 client. * * @param string $idToken the ID token in JWT format * @param string $audience Optional. The audience to verify against JWt "aud" * @return array|false the token payload, if successful */ public function verifyIdToken($idToken, $audience = null) { if (empty($idToken)) { throw new LogicException('id_token cannot be null'); } // Check signature try { $payload = ($this->jwt)->decode($idToken, $this->keySet); } catch (ExpiredException | SignatureInvalidException | DomainException) { return false; } if (property_exists($payload, 'aud')) { if ($audience && $payload->aud != $audience) { return false; } } // support HTTP and HTTPS issuers // @see https://developers.google.com/identity/sign-in/web/backend-auth $issuers = [self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS]; if (!isset($payload->iss) || !in_array($payload->iss, $issuers)) { return false; } return (array) $payload; } private function getJwtService() { $jwt = new JWT(); if ($jwt::$leeway < 1) { // Ensures JWT leeway is at least 1 // @see https://github.com/google/google-api-php-client/issues/827 $jwt::$leeway = 1; } return $jwt; } }