Example #1
0
 /**
  * @param \nodge\eauth\ServiceBase $service
  * @return User
  * @throws ErrorException
  */
 public static function findByEAuth($service)
 {
     if (!$service->getIsAuthenticated()) {
         throw new ErrorException('EAuth user should be authenticated before creating identity.');
     }
     $service_id = $service->getServiceName() . '-' . $service->getId();
     // find user auth
     $user_auth = AuthRecords::find()->where(["id" => $service_id])->one();
     // make new auth record and create user
     if (!isset($user_auth->user_id)) {
         $attributes = $service->getAttributes();
         $nameFromService = isset($attributes['name']) ? $attributes['name'] : null;
         // add user
         $model = new User();
         $model->username = $nameFromService ? $nameFromService : $service_id;
         $model->auth_key = md5($service_id);
         // set default pass for direct account access
         $password = $model->generatePass();
         $model->password_hash = Yii::$app->security->generatePasswordHash($password);
         $model->save();
         $user_id = $model->id;
         // add auth
         $new_auth = new AuthRecords();
         $new_auth->id = $service_id;
         $new_auth->user_id = $user_id;
         $new_auth->attributes = serialize($service->getAttributes());
         $new_auth->save();
     } else {
         $user_id = $user_auth->user_id;
     }
     return user::findIdentity($user_id);
 }
Example #2
0
 /**
  * @param \nodge\eauth\ServiceBase $service
  * @return User
  * @throws ErrorException
  */
 public static function findByEAuth($service)
 {
     if (!$service->getIsAuthenticated()) {
         throw new ErrorException('EAuth user should be authenticated before creating identity.');
     }
     $id = $service->getServiceName() . '-' . $service->getId();
     $attributes = ['id' => $id, 'steamid' => $service->getAttribute('steamid'), 'username' => $service->getAttribute('username'), 'profile_url' => $service->getAttribute('profile_url'), 'avatar' => $service->getAttribute('avatar'), 'avatar_md' => $service->getAttribute('avatar_md'), 'avatar_lg' => $service->getAttribute('avatar_lg')];
     Yii::$app->getSession()->set('user-' . $id, $attributes);
     return new self($attributes);
 }
Example #3
0
 /**
  * @param \nodge\eauth\ServiceBase $service
  * @return User
  * @throws ErrorException
  */
 public static function findByEAuth($service)
 {
     if (!$service->getIsAuthenticated()) {
         throw new ErrorException('Авторизован.');
     }
     /**
      * @var $social    UserSocial
      * @var $socialOth UserSocial
      */
     $email = $service->getAttribute('email');
     $social = UserSocial::findOne(['service' => $service->getServiceName(), 'service_id' => "{$service->getId()}"]);
     if ($social) {
         return $social->user;
     }
     $socialOth = $email ? UserSocial::findOne(['email' => $email]) : false;
     $user = User::findOne(['email' => $email]);
     if (!$user) {
         if (!$socialOth) {
             $user = new static(['username' => $service->getAttribute('name'), 'email' => $email, 'auth_key' => Yii::$app->security->generateRandomString()]);
             $user->insert(false);
         } else {
             $user = $socialOth->user;
         }
     }
     $social = new UserSocial(['service' => $service->getServiceName(), 'service_id' => $service->getId(), 'user_id' => $user->id, 'email' => $user->email]);
     $social->insert(false);
     return $user;
 }
 /**
  * For OAuth we can check existing access token.
  * Useful for API calls.
  *
  * @return bool
  * @throws ErrorException
  */
 public function getIsAuthenticated()
 {
     if (!$this->authenticated) {
         try {
             $proxy = $this->getProxy();
             $this->authenticated = $proxy->hasValidAccessToken();
         } catch (\OAuth\Common\Exception\Exception $e) {
             throw new ErrorException($e->getMessage(), $e->getCode(), 1, $e->getFile(), $e->getLine(), $e);
         }
     }
     return parent::getIsAuthenticated();
 }
Example #5
0
File: User.php Project: ut8ia/iwet
 /**
  * @param \nodge\eauth\ServiceBase $service
  * @return User
  * @throws ErrorException
  */
 public static function findByEAuth($service)
 {
     if (!$service->getIsAuthenticated()) {
         throw new ErrorException('EAuth user should be authenticated before creating identity.');
     }
     $service_id = $service->getServiceName() . '-' . $service->getId();
     // find user auth
     $user_auth = AuthRecords::find()->where(["id" => $service_id])->one();
     if (!isset($user_auth->user_id)) {
         $attributes = $service->getAttributes();
         $nameFromService = isset($attributes['name']) ? $attributes['name'] : null;
         // add user
         $model = new User();
         $model->username = $nameFromService ? $nameFromService : $service_id;
         $model->auth_key = md5($service_id);
         $model->password_hash = "asd" . rand(0, 1045693);
         //            $model->email = "";
         $model->save();
         $user_id = $model->id;
         // add auth
         $new_auth = new AuthRecords();
         $new_auth->id = $service_id;
         $new_auth->user_id = $user_id;
         $new_auth->attributes = serialize($service->getAttributes());
         $new_auth->save();
     } else {
         $user_id = $user_auth->user_id;
     }
     return user::findIdentity($user_id);
 }
Example #6
0
 /**
  * @param \nodge\eauth\ServiceBase $service
  * @return User
  * @throws ErrorException
  */
 public static function findByEAuth($service)
 {
     if (!$service->getIsAuthenticated()) {
         throw new ErrorException('EAuth user should be authenticated before creating identity.');
     }
     $id = $service->getServiceName() . '-' . $service->getId();
     $attributes = array('id' => $id, 'username' => $service->getAttribute('name'), 'authKey' => md5($id), 'profile' => $service->getAttributes());
     $attributes['profile']['service'] = $service->getServiceName();
     Yii::$app->getSession()->set('user-' . $id, $attributes);
     return new self($attributes);
 }
Example #7
0
 /**
  * Initialize the component.
  */
 public function init()
 {
     parent::init();
     $host = null;
     if (array_key_exists('openid_return_to', $_GET)) {
         $url = parse_url($_GET['openid_return_to']);
         $host = $url['host'];
     }
     if (empty($host)) {
         $host = Yii::$app->getRequest()->getHostInfo();
     }
     $this->auth = new LightOpenID($host);
 }
Example #8
0
 /**
  * Returns the public resource.
  *
  * @param string $url url to request.
  * @param array $options HTTP request options. Keys: query, data, headers.
  * @param boolean $parseResponse Whether to parse response.
  * @return mixed the response.
  * @throws ErrorException
  */
 public function makeRequest($url, $options = [], $parseResponse = true)
 {
     $headers = isset($options['headers']) ? $options['headers'] : [];
     $options['headers'] = array_merge($this->getExtraApiHeaders(), $headers);
     return parent::makeRequest($url, $options, $parseResponse);
 }
Example #9
0
 /**
  * Initialize the component.
  */
 public function init()
 {
     parent::init();
     $this->auth = new LightOpenID(Yii::$app->getRequest()->getHostInfo());
 }
Example #10
0
 /**
  * @param \nodge\eauth\ServiceBase $service
  * @return User
  * @throws ErrorException
  */
 public static function findByEAuth($service)
 {
     if (!$service->getIsAuthenticated()) {
         throw new ErrorException('EAuth user should be authenticated before creating identity.');
     }
     $user = self::find()->where(['eauth_service' => $service->getServiceName(), 'eauth_id' => $service->getId()])->one();
     if ($user == null) {
         // print_r($service->attributes);
         // exit;
         $username = $service->getAttribute('username');
         $username = $username ? $username : "******" . $service->getId();
         $user = new User(['username' => $username, 'password' => Yii::$app->security->generateRandomString(6), 'status' => self::STATUS_ACTIVE, 'name' => $service->getAttribute('name') ? $service->getAttribute('name') : $service->getAttribute('username'), 'email' => $service->getAttribute('email'), 'eauth_service' => (string) $service->getServiceName(), 'eauth_id' => (string) $service->getId()]);
         $user->generateAuthKey();
         $user->save();
     }
     return $user;
 }
Example #11
0
File: User.php Project: h11Nox/slug
 /**
  * @param \nodge\eauth\ServiceBase $service
  * @return User
  * @throws ErrorException
  */
 public static function findByEAuth($service)
 {
     if (!$service->getIsAuthenticated()) {
         throw new ErrorException('EAuth user should be authenticated before creating identity.');
     }
     $user = static::find()->where('service = :service AND service_id = :ID', [':service' => $service->getServiceName(), ':ID' => $service->getAttribute('id')])->one();
     if (!$user) {
         $photo = $service->getServiceName() === 'vkontakte' ? $service->getAttribute('photo_big') : "https://graph.facebook.com/{$service->getAttribute('id')}/picture?type=large";
         $user = new static(['username' => $service->getAttribute('name'), 'service' => $service->getServiceName(), 'service_id' => $service->getAttribute('id'), 'img' => $photo]);
         $user->save();
     }
     $id = $service->getServiceName() . '-' . $service->getId();
     $attributes = array_merge($user->getAttributes(['id', 'username', 'img', 'rating']), ['id' => $id, 'auth_key' => md5($id), 'cid' => $user->id]);
     Yii::$app->getSession()->set('user-' . $id, $attributes);
     return new self($attributes);
 }
 /**
  * Finds user by \nodge\eauth\ServiceBase eauth service.
  * 
  * @param \nodge\eauth\ServiceBase $service
  * 
  * @return object ServiceUser
  * 
  * @throws ErrorException
  */
 public static function findByEAuth($service)
 {
     if (!$service->getIsAuthenticated()) {
         throw new ErrorException('EAuth user should be authenticated before creating identity.');
     }
     $identity = static::findIdentity(['service_name' => $service->getServiceName(), 'service_user_id' => $service->getId()]);
     if ($identity instanceof \yii\web\IdentityInterface) {
         $identity->username = $service->getAttribute('username');
         $identity->access_token = $service->getAttribute('access_token');
         $identity->avatar_url = $service->getAttribute('avatar_url');
         $identity->save();
     }
     return $identity;
 }