Beispiel #1
0
 private function simple($require)
 {
     $params = $this->controller->getParams($require);
     $application = $this->controller->getApplication($params['app_id']);
     if (!$application) {
         throw new NotFoundApiException('ApplicationNotFound');
     }
     $form = new SimpleRegisterForm('default', $params);
     if (!$form->validate()) {
         throw new ValidationFailedApiException();
     }
     if (!$form->save()) {
         throw new ApiException($form->errors, 500);
     }
     $form->user->refresh();
     $this->controller->identity = new UserIdentity($params['email'], $params['password']);
     /**
      * @var UserApiToken $token
      */
     $token = UserApiToken::model()->findByPk($this->controller->identity->getId() . $params['app_id']);
     //        if ($token) {
     //            $token->delete();
     //        }
     if (!$token) {
         $token = UserApiToken::model()->create($form->user, $params['app_id']);
     }
     $data = array('user' => $form->user, 'token' => $token);
     $this->controller->data = $data;
 }
Beispiel #2
0
 public function authenticate()
 {
     /**
      * @var \EMongoDocument|UserApiToken $token
      */
     $token = UserApiToken::model()->byToken($this->access_token)->byAppId($this->appId)->find();
     if (!$token) {
         throw new AuthFailedApiException('InvalidToken');
     }
     $this->user_id = $token->user_id;
     $this->handleUser();
     return true;
 }
Beispiel #3
0
 public function logout()
 {
     throw new DeprecatedApiException();
     /**
      * @var UserApiToken $token
      */
     $appId = $this->controller->requestHeaders['HTTP_APP_ID'];
     $token = UserApiToken::model()->byAppId($appId)->findByPk($this->controller->identity->getId() . $appId);
     if ($token) {
         $token->expire = time();
         if (!$token->save()) {
             throw new ApiException($token->getErrors(), 500);
         }
         $this->controller->setMessage('Success');
     } else {
         //why?
         throw new NotFoundApiException('TokenNotFound');
     }
 }
Beispiel #4
0
 public function check()
 {
     throw new DeprecatedApiException();
     $require = array('HTTP_APP_ID' => true, 'HTTP_ACCESS_TOKEN' => true);
     $headers = $this->controller->getHeaders($require, true);
     $application = $this->controller->getApplication($headers['HTTP_APP_ID']);
     if (!$application) {
         throw new NotFoundApiException('ApplicationNotFound');
     }
     /**
      * @var UserApiToken $token
      */
     $token = UserApiToken::model()->byToken($headers['HTTP_ACCESS_TOKEN'])->byAppId($headers['HTTP_APP_ID'])->find();
     if (!$token) {
         throw new AuthFailedApiException('InvalidToken');
     }
     if (!$token->isAlive()) {
         throw new AuthFailedApiException('DeadToken');
     }
     $this->controller->setMessage('Success');
 }
Beispiel #5
0
 /**
  * Fill data field with array of user and token keys
  * In user key instance of user
  * In token key just created api token
  *
  * @param \User $user
  */
 public function toAuthArray($user)
 {
     if ($user instanceof \User) {
         /**
          * @var UserApiToken $token
          */
         $token = UserApiToken::model()->byAppId($this->getApplication()->_id->{'$id'})->findByPk($user->id . $this->getApplication()->_id);
         if (!$token) {
             //support old tokens where app_id is ObjectId
             $token = UserApiToken::model()->byAppId($this->getApplication()->_id)->findByPk($user->id . $this->getApplication()->_id);
             if (!$token) {
                 $token = UserApiToken::model()->create($user, $this->getApplication()->_id->{'$id'});
             }
         }
         Formatter::format($token);
         $user->refresh();
         $this->data = array('user' => $user, 'token' => $token);
     } else {
         $this->setError('WrongAuthArrayParam', 500);
     }
 }