From d1c9e221f7ebd72eefe539ccd38cfcc37be46901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 21 Jun 2020 01:31:36 +0200 Subject: [PATCH] MAPG-69 add Google OAuth handler --- src/OAuth/GoogleOAuth.php | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/OAuth/GoogleOAuth.php diff --git a/src/OAuth/GoogleOAuth.php b/src/OAuth/GoogleOAuth.php new file mode 100644 index 0000000..07c2107 --- /dev/null +++ b/src/OAuth/GoogleOAuth.php @@ -0,0 +1,41 @@ + 'code', + 'client_id' => $_ENV['GOOGLE_OAUTH_CLIENT_ID'], + 'scope' => 'openid email', + 'redirect_uri' => $redirectUrl, + 'state' => $state, + 'nonce' => hash('sha256', random_bytes(10) . microtime()), + ]; + + return self::$dialogUrlBase . '?' . http_build_query($oauthParams); + } + + public function getToken(string $code, string $redirectUrl) + { + $tokenParams = [ + 'code' => $code, + 'client_id' => $_ENV['GOOGLE_OAUTH_CLIENT_ID'], + 'client_secret' => $_ENV['GOOGLE_OAUTH_CLIENT_SECRET'], + 'redirect_uri' => $redirectUrl, + 'grant_type' => 'authorization_code', + ]; + + $request = new Request(self::$tokenUrlBase, Request::HTTP_POST); + $request->setQuery($tokenParams); + $response = $request->send(); + + return json_decode($response->getBody(), true); + } +}