/** * {@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 []; }
/** * {@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; }
/** * @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')); }