示例#1
0
<?php

spl_autoload_register(function ($class) {
    require str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
});
// configuration of client credentials
$client = new OAuth2\Client('CLIENT_ID', 'CLIENT_SECRET', 'CALLBACK_URL');
// or use without callback_url, use in desktop application
$client = new OAuth2\Client('CLIENT_ID', 'CLIENT_SECRET');
// configuration of service
$configuration = new OAuth2\Service\Configuration('AUTHORIZE_ENDPOINT', 'ACCESS_TOKEN_ENDPOINT', 'REVOKE_ENDPOINT');
// storage class for access token, just implement OAuth2\DataStore interface for
// your own implementation
$dataStore = new OAuth2\DataStore\Session();
$scope = null;
$service = new OAuth2\Service($client, $configuration, $dataStore, $scope);
if (isset($_GET['action'])) {
    switch ($_GET['action']) {
        case 'authorize':
            // redirects to authorize endpoint
            $service->authorize();
            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;
    }
 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>";
     }
 }