Ejemplo n.º 1
0
            break;
        case 'requestApi':
            // calls api endpoint with access token
            echo $service->callApiEndpoint('API_ENDPOINT');
            break;
        case 'revoke':
            // calls revoke access token
            echo $service->revokeAccessToken();
            break;
    }
}
if (isset($_GET['code'])) {
    // retrieve access token from endpoint
    $service->getAccessToken();
}
$token = $dataStore->retrieveAccessToken();
?>
<html>
    <head>
    </head>
    <body>
        Consumer Key: <input type="text" id="consumer-key" value="<?php 
echo $client->getClientKey();
?>
" /><br />
        Consumer Secret: <input type="text" id="consumer-secret" value="<?php 
echo $client->getClientSecret();
?>
" /><br />
        Access Token: <input type="text" id="access-token" value="<?php 
echo $token->getAccessToken();
 public function actionGoogle_oauth2_callback()
 {
     $code = null;
     // = $this->input->get("code");
     //please comment and uncomment accordingly
     //this is the client key for http://localhost/coplat/index.php/site/login?r=Login/google_oauth2_callback
     $client = $this->GetGoogleAuthCallbacK();
     /* new OAuth2\Client( '18539649881-nf47u1hqi68u16719abpqa1c86hhgr3b.apps.googleusercontent.com',
        'eIdiK7XoWCK2GLSB0DbA5KDy',
        $this->getAuthorizationCallbackURL());*/
     //this is the client key for http://cp-dev.cis.fiu.edu/coplat/index.php/site/login?r=Login/google_oauth2_callback
     /*   $client = new OAuth2\Client('265213885628-bvag1ur2vpn9a1asmagjn4rtb624p0l2.apps.googleusercontent.com', 
          'UwkqyyRLy0I_sJXwZ_JqurVh',
           $this->getAuthorizationCallbackURL() );*/
     $configuration = new OAuth2\Service\Configuration('https://accounts.google.com/o/oauth2/auth', 'https://accounts.google.com/o/oauth2/token');
     $dataStore = new OAuth2\DataStore\Session();
     $scope = "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email";
     $service = new OAuth2\Service($client, $configuration, $dataStore, $scope);
     $service->getAccessToken($code);
     $token = $dataStore->retrieveAccessToken();
     $userinfo = $service->callApiEndpoint('https://www.googleapis.com/oauth2/v1/userinfo');
     /* Data format returned by Google
        * '{
         "id": "112343029738132982182",
         "email": "*****@*****.**",
         "verified_email": true,
         "name": "Yaneli Fernandez Sosa",
         "given_name": "Yaneli",
         "family_name": "Fernandez Sosa"
         }
        */
     $matches = array();
     preg_match_all("/\"id\": \"(\\d+)\"/", $userinfo, $matches);
     $id = $matches[1][0];
     $matches = array();
     preg_match_all("/\"email\": \"([a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+)\"/", $userinfo, $matches);
     $email = $matches[1][0];
     $matches = array();
     preg_match_all("/\"given_name\": \"([a-zA-Z\\s]+)\"/", $userinfo, $matches);
     $given_name = $matches[1][0];
     $matches = array();
     preg_match_all("/\"family_name\": \"([a-zA-Z\\s-]+)\"/", $userinfo, $matches);
     $family_name = $matches[1][0];
     $parts = explode('@', $email);
     $model = User::model()->find("email = '" . $email . "'");
     if (!empty($model)) {
         $pw = User::model()->findBySql("select password from user where email = :email", array(":email" => $email));
         $identity = new UserIdentity($parts[0], $pw);
         $identity->authenticate();
         $duration = 3600 * 24 * 30;
         // 30 days
         Yii::app()->user->login($identity, $duration);
         echo "<script> window.location = '../home/userHome';</script>";
     } else {
         echo "<script> window.alert('Please make sure you are registered in SPW and/or contact admin');\n                 window.location = 'login';\n\n             </script>";
     }
 }