Exemple #1
0
 public function updateFromGoogleAccount()
 {
     $client = GoogleSessionController::getClient();
     $oauth2 = new \Google_Auth_OAuth2($client);
     if ($this->google_token && $oauth2->isAccessTokenExpired() == false) {
         $oauth2->refreshToken($this->google_token);
         $client->setAccessToken($oauth2->getAccessToken());
         $oauth2 = new \Google_Service_Oauth2($client);
         $google_user = $oauth2->userinfo->get();
         // update the photo
         GoogleSessionController::saveGoogleProfileImage($google_user, $this);
         // other things later..
         return true;
     }
     return false;
 }
Exemple #2
0
 /**
  * Test that the ID token is properly refreshed.
  */
 public function testRefreshTokenSetsValues()
 {
     $client = new Google_Client();
     $response_data = json_encode(array('access_token' => "ACCESS_TOKEN", 'id_token' => "ID_TOKEN", 'expires_in' => "12345"));
     $response = $this->getMock("Google_Http_Request", array(), array(''));
     $response->expects($this->any())->method('getResponseHttpCode')->will($this->returnValue(200));
     $response->expects($this->any())->method('getResponseBody')->will($this->returnValue($response_data));
     $io = $this->getMock("Google_IO_Stream", array(), array($client));
     $io->expects($this->any())->method('makeRequest')->will($this->returnCallback(function ($request) use(&$token, $response) {
         $elements = $request->getPostBody();
         PHPUnit_Framework_TestCase::assertEquals($elements['grant_type'], "refresh_token");
         PHPUnit_Framework_TestCase::assertEquals($elements['refresh_token'], "REFRESH_TOKEN");
         return $response;
     }));
     $client->setIo($io);
     $oauth = new Google_Auth_OAuth2($client);
     $oauth->refreshToken("REFRESH_TOKEN");
     $token = json_decode($oauth->getAccessToken(), true);
     $this->assertEquals($token['id_token'], "ID_TOKEN");
 }
 public static function getGoogleUser(&$user)
 {
     if ($user->hasToken()) {
         $client = GoogleSessionController::getClient();
         $auth = new Google_Auth_OAuth2($client);
         $auth->refreshToken($user->getToken());
         $token = $auth->getAccessToken();
         $client->setAccessToken($auth->getAccessToken());
         $oauth2 = new Google_Service_Oauth2($client);
         $google_user = $oauth2->userinfo->get();
         // save the latest token
         // $user->google_token = $auth->getRefreshToken();
         // $user->save();
         return $google_user;
     } else {
         $url = GoogleSessionController::generateGoogleLoginURL(['approval_prompt' => 'force', 'state' => 'refresh_token']);
         return Redirect::to($url);
     }
 }