コード例 #1
0
ファイル: Facebook.php プロジェクト: cawaphp/module-oauth
 /**
  * {@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;
 }
コード例 #2
0
ファイル: Twitter.php プロジェクト: cawaphp/module-oauth
 /**
  * {@inheritdoc}
  */
 public function getUser() : User
 {
     $oauthToken = self::request()->getQuery('oauth_token');
     $oauthVerifier = self::request()->getQuery('oauth_verifier');
     if (!$oauthToken || !$oauthVerifier) {
         throw new \LogicException('No code found on oauth route end');
     }
     $token = $this->service->getStorage()->retrieveAccessToken('Twitter');
     // This was a callback request from twitter, get the token
     $this->service->requestAccessToken($oauthToken, $oauthVerifier, $token->getRequestTokenSecret());
     // Send a request now that we have access token
     $result = json_decode($this->service->request('account/verify_credentials.json?include_email=true'), true);
     $name = $this->pop($result, 'name');
     $firstname = $lastname = null;
     if ($name) {
         $explode = explode(' ', $name);
         if (isset($explode[0])) {
             $firstname = array_shift($explode);
             $lastname = implode(' ', $explode);
         }
     }
     $user = new User($this->getType());
     $user->setUid($this->pop($result, 'id_str'))->setEmail($this->pop($result, 'email'))->setUsername($this->pop($result, 'screen_name'))->setVerified($this->pop($result, 'verified'))->setFirstName($firstname)->setLastName($lastname)->setLocale($this->pop($result, 'lang'))->setExtraData($result);
     return $user;
 }
コード例 #3
0
ファイル: Google.php プロジェクト: cawaphp/module-oauth
 /**
  * {@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;
 }
コード例 #4
0
ファイル: Microsoft.php プロジェクト: cawaphp/module-oauth
 /**
  * {@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'), true);
     $day = $this->pop($result, 'birth_day') ?? '00';
     $month = $this->pop($result, 'birth_month') ?? '00';
     $year = $this->pop($result, 'birth_year') ?? '0000';
     $birthday = $year . '-' . $month . '-' . $day;
     if ($birthday !== '0000-00-00') {
         $birthday = new Date($birthday);
     } else {
         $birthday = null;
     }
     $user = new User($this->getType());
     $user->setUid($this->pop($result, 'id'))->setEmail($this->pop($result, ['emails', 'preferred']))->setGender($this->pop($result, 'gender'))->setUsername($this->pop($result, 'name'))->setFirstName($this->pop($result, 'first_name'))->setLastName($this->pop($result, 'last_name'))->setLocale($this->pop($result, 'locale'))->setBirthday($birthday)->setExtraData($result);
     return $user;
 }