示例#1
0
文件: Google.php 项目: romeoz/rock
 /**
  * {@inheritdoc}
  */
 public function getAttributes($code = null)
 {
     if (!isset($code)) {
         $code = Request::get('code');
     }
     if (empty($code)) {
         return [];
     }
     // This was a callback request from google, get the token
     $this->service->requestAccessToken($code);
     // Send a request with it
     try {
         return Json::decode($this->service->request($this->apiUrl));
     } catch (JsonException $e) {
         if (class_exists('\\rock\\log\\Log')) {
             Log::err(BaseException::convertExceptionToString($e));
         }
     }
     return [];
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function getUser()
 {
     $code = self::request()->getQuery('code');
     $state = self::request()->getQuery('state');
     $error = self::request()->getQuery('error');
     if ($error == 'access_denied') {
         return new Denied($this->getType(), $error, sprintf("Error Code '%s'", $error));
     } elseif ($error) {
         throw new \Exception(sprintf("Failed with error '%s'", $error));
     }
     if (!$code) {
         throw new \LogicException('No code found on oauth route end');
     }
     // This was a callback request from google, get the token
     $token = $this->service->requestAccessToken($code, $state);
     // Send a request with it
     $result = json_decode($this->service->request('userinfo'), true);
     $gender = $this->pop($result, 'gender');
     $user = new User($this->getType());
     $user->setUid($this->pop($result, 'id'))->setEmail($this->pop($result, 'email'))->setVerified($this->pop($result, 'verified_email'))->setFirstName($this->pop($result, 'given_name'))->setLastName($this->pop($result, 'family_name'))->setGender($gender == 'male' ? User::GENDER_MALE : ($gender == 'female' ? User::GENDER_FEMALE : null))->setLocale($this->pop($result, 'locale'))->setExtraData($result);
     return $user;
 }
示例#3
0
 /**
  * @param CredentialsInterface  $credentials
  * @param ClientInterface       $httpClient
  * @param TokenStorageInterface $storage
  * @param array                 $scopes
  * @param UriInterface|null     $baseApiUri
  * @param bool                  $stateParameterInAutUrl
  * @param string                $apiVersion
  *
  * @throws InvalidScopeException
  */
 public function __construct(CredentialsInterface $credentials, ClientInterface $httpClient, TokenStorageInterface $storage, $scopes = [], UriInterface $baseApiUri = null, $stateParameterInAutUrl = false, $apiVersion = '')
 {
     parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, $stateParameterInAutUrl, $apiVersion);
     $consumerSecret = $this->credentials->getConsumerSecret();
     if (is_file($consumerSecret) === true) {
         $extension = substr($consumerSecret, strrpos($consumerSecret, '.') + 1);
         $privateyKey = file_get_contents($consumerSecret);
         switch ($extension) {
             case 'p12':
                 $this->privateKey = $this->getPrivateKeyFromPKCS12($privateyKey, 'notasecret');
                 break;
             case 'json':
                 $this->privateKey = json_decode($privateyKey, true)['private_key'];
                 break;
         }
     } else {
         $this->privateKey = $consumerSecret;
     }
 }
 /**
  * Request access token from Google and return a LoginRequest object for logging into our app
  *
  * @param  Oauth2Service\Google $google
  * @param  TokenInterface       $token
  * @return LoginRequest
  */
 protected function google(Oauth2Service\Google $google, TokenInterface $token)
 {
     $user = json_decode($google->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
     $loginRequest = new LoginRequest('google', $user['id'], $token->getAccessToken(), $token->getEndOfLife() > 0 ? $token->getEndOfLife() : 0, $token->getRefreshToken(), [$user['email']]);
     return $loginRequest;
 }
示例#5
0
 /**
  * @covers OAuth\OAuth2\Service\Google::__construct
  * @covers OAuth\OAuth2\Service\Google::parseAccessTokenResponse
  */
 public function testParseAccessTokenResponseValidWithRefreshToken()
 {
     $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
     $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}'));
     $service = new Google($this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), $client, $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'));
     $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
 }
示例#6
0
 /**
  * @covers OAuth\OAuth2\Service\Google::__construct
  * @covers OAuth\OAuth2\Service\Google::getAccessTokenEndpoint
  */
 public function testGetAccessTokenEndpoint()
 {
     $service = new Google($this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), $this->getMock('\\Buzz\\Browser'), $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'));
     $this->assertSame('https://accounts.google.com/o/oauth2/token', (string) $service->getAccessTokenEndpoint());
 }