/** * {@inheritdoc} */ public function getUser() : User { $code = self::request()->getQuery('code'); $state = self::request()->getQuery('state'); if (!$code) { throw new \LogicException('No code found on oauth route end'); } // This was a callback request from facebook, get the token $token = $this->service->requestAccessToken($code, $state); // Send a request with it $result = json_decode($this->service->request('/me?fields=id,name,birthday,verified,first_name,last_name,email,locale'), true); $gender = $this->pop($result, 'gender'); $birthday = $this->pop($result, 'birthday'); if ($birthday) { $explode = explode('/', $birthday); if (sizeof($explode) == 3) { $birthday = new Date($explode[2] . '-' . $explode[0] . '-' . $explode[1]); } elseif (sizeof($explode) == 2) { $birthday = new Date('0000-' . $explode[0] . '-' . $explode[1]); } else { $birthday = new Date($explode[0] . '-00-00'); } } $user = new User($this->getType()); $user->setUid($this->pop($result, 'id'))->setEmail($this->pop($result, 'email'))->setVerified($this->pop($result, 'verified'))->setFirstName($this->pop($result, 'first_name'))->setLastName($this->pop($result, 'last_name'))->setLocale(substr($this->pop($result, 'locale'), 0, 2))->setGender($gender == 'male' ? User::GENDER_MALE : ($gender == 'female' ? User::GENDER_FEMALE : null))->setBirthday($birthday)->setExtraData($result); return $user; }
/** * {@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 []; }
/** * @covers OAuth\OAuth2\Service\Facebook::__construct * @covers OAuth\OAuth2\Service\Facebook::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=bar&refresh_token=baz')); $service = new Facebook($this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), $client, $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')); $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); }