예제 #1
0
파일: Google.php 프로젝트: BrightFlair/IOU
 protected function startFlow()
 {
     $client = new Client(["clientId" => self::$ID, "clientSecret" => self::$secret, "redirectUri" => "http://localhost:8080/", "scopes" => ["profile", "email"], "hostedDomain" => "localhost:8080"]);
     if (!empty($_GET["error"])) {
         // User probably denied access.
         die("Got an error: {$_GET['error']}");
     } else {
         if (empty($_GET["code"])) {
             // We need to get an authorisation code.
             $authUrl = $client->getAuthorizationUrl();
             $_SESSION["oauth2state"] = $client->state;
             Headers::redirect($authUrl);
             exit;
         } else {
             if (empty($_GET["state"]) || $_GET["state"] !== $_SESSION["oauth2state"]) {
                 // State is invalid - possible CSRF attack.
                 unset($_SESSION["oauth2state"]);
                 die("Invalid state");
             } else {
                 // Try to get an access token using the authorisation grant.
                 try {
                     $token = $client->getAccessToken("authorization_code", ["code" => $_GET["code"]]);
                     $this->details = $client->getUserDetails($token);
                     unset($_SESSION["oauth2state"]);
                 } catch (\Exception $ex) {
                     unset($_SESSION["oauth2state"]);
                     die("Something went wrong! " . $ex->getMessage());
                 }
             }
         }
     }
 }
예제 #2
0
 /**
  * It will return uid, token and information user to save database
  *
  * @return array
  */
 public function authorize()
 {
     $this->view->disable();
     $provider = new Google(['clientId' => $this->clientId, 'clientSecret' => $this->clientSecret, 'redirectUri' => $this->redirectUriAuthorize]);
     $code = $this->request->getQuery('code');
     $state = $this->request->getQuery('state');
     if (!isset($code)) {
         // If we don't have an authorization code then get one
         $authUrl = $provider->getAuthorizationUrl();
         $this->session->set('oauth2state', $provider->state);
         return $this->response->redirect($authUrl);
         // Check given state against previously stored one to mitigate CSRF attack
     } elseif (empty($state) || $state !== $this->session->get('oauth2state')) {
         $this->session->remove('oauth2state');
         exit('Invalid state');
     } else {
         // Try to get an access token (using the authorization code grant)
         $token = $provider->getAccessToken('authorization_code', ['code' => $code]);
         $uid = $provider->getUserUid($token);
         $userDetails = $provider->getUserDetails($token);
         return array($uid, $token, $userDetails);
     }
 }