diff --git a/src/Util/GoogleJwtValidator.php b/src/Util/GoogleJwtValidator.php deleted file mode 100644 index 17901a5..0000000 --- a/src/Util/GoogleJwtValidator.php +++ /dev/null @@ -1,102 +0,0 @@ -request = $request; - } - - public function getDialogUrl(string $state, string $redirectUrl, ?string $nonce = null, ?string $loginHint = null): string - { - $oauthParams = [ - 'response_type' => 'code', - 'client_id' => $_ENV['GOOGLE_OAUTH_CLIENT_ID'], - 'scope' => 'openid email', - 'redirect_uri' => $redirectUrl, - 'state' => $state, - ]; - - if ($nonce !== null) { - $oauthParams['nonce'] = $nonce; - } - - if ($loginHint !== null) { - $oauthParams['login_hint'] = $loginHint; - } - - return self::$dialogUrlBase . '?' . http_build_query($oauthParams); - } - - public function getToken(string $code, string $redirectUrl): array - { - $tokenParams = [ - 'code' => $code, - 'client_id' => $_ENV['GOOGLE_OAUTH_CLIENT_ID'], - 'client_secret' => $_ENV['GOOGLE_OAUTH_CLIENT_SECRET'], - 'redirect_uri' => $redirectUrl, - 'grant_type' => 'authorization_code', - ]; - - $this->request->setUrl(self::$tokenUrlBase); - $this->request->setMethod(IRequest::HTTP_POST); - $this->request->setQuery($tokenParams); - $response = $this->request->send(); - - return json_decode($response->getBody(), true); - } - - public function validateJwt($jwt): ?array - { - $request = new Request(self::$certsUrl, IRequest::HTTP_GET); - $response = $request->send(); - $certs = json_decode($response->getBody(), true)['keys']; - - foreach ($certs as $cert) { - $publicKey = $this->getPublicKey($cert); - - try { - return (array) JWT::decode($jwt, new Key($publicKey, 'RS256')); - } catch (ExpiredException $e) { - return null; - } catch (SignatureInvalidException $e) { - //continue - } catch (DomainException $e) { - //continue - } - } - - return null; - } - - private function getPublicKey($cert): string - { - $modulus = new BigInteger($this->base64Decode($cert['n']), 256); - $exponent = new BigInteger($this->base64Decode($cert['e']), 256); - $component = ['n' => $modulus, 'e' => $exponent]; - $rsa = new RSA(); - $rsa->loadKey($component); - return $rsa->getPublicKey(); - } - - private function base64Decode($input): string - { - $input = str_replace(['_', '-'], ['/', '+'], $input); - return base64_decode($input); - } -}