def createsuperuser(username, password): from hyuga.models.user import User click.echo("[CMD] create super user...") User.create(username=username, password=password, identify="admin.hyuga.co", token="", administrator=True) click.echo("[CMD] create super user success...")
def createsuperuser(username, password): """Create superuser. """ click.echo("[CMD] create super user...") User.create(username=username, password=password, identify="admin.hyuga.co", token="", administrator=True) click.echo("[CMD] create super user success...")
def on_get(self, req, resp): if not req.params: raise InvalidParameterError() _filter = None _type = req.params["type"] _token = req.params["token"] if "filter" in req.params.keys(): _filter = req.params["filter"] Record = None _type = req.params["type"] if _type == "dns": Record = DnsRecord elif _type == "http": Record = HttpRecord try: user = User.get(User.token == _token) records = Record.objects.filter(uidentify=user.identify).limit( CONFIG.RECORDS_MAX_NUM) resp_data = records_to_list(records, _filter) self.on_success(resp, resp_data) except User.DoesNotExist: raise UnauthorizedError(f"Token Does Not Exist: {_token}")
def on_post(self, req, resp): req_data = req.context["data"] if not req_data: raise InvalidParameterError(req.context["data"]) username = req_data["username"] password = req_data["password"] try: User.get(User.username == username) self.on_error(resp, ERR_USER_ALREADY_EXISTS) except User.DoesNotExist: kwargs = {"username": username, "password": password} if "nickname" in req_data.keys(): kwargs["nickname"] = req_data["nickname"] create_user(**kwargs) self.on_success(resp)
def on_get(self, req, resp, user_id): if self.current_user is None or \ self.current_user.administrator is False: raise NotSupportedError(method=req.method, url=req.path) try: user = User.get(User.id == user_id) self.on_success(resp, user.model_to_dict()) except User.DoesNotExist: raise UserNotExistsError()
def on_get(self, req, resp): # 管理员权限可以访问 if self.current_user is None or \ self.current_user.administrator is False: raise NotSupportedError(method=req.method, url=req.path) try: resp_data = [user.model_to_dict() for user in User.select()] self.on_success(resp, resp_data) except User.DoesNotExist: self.on_error(resp)
def on_delete(self, req, resp, user_id): if self.current_user is None or \ self.current_user.administrator is False: raise NotSupportedError(method=req.method, url=req.path) try: user = User.get(User.id == user_id) user.delete_instance() self.on_success(resp, {"id": user_id}) except User.DoesNotExist: raise UserNotExistsError() except Exception: self.on_error(resp)
def wrapper(self, req, resp, *args, **kwargs): self.current_user = None jwt_token = req.get_header("JWToken", default=None) if not jwt_token: raise UnauthorizedError('JWToken Not Exists') try: user_id = jwt.decode(jwt_token, CONFIG.SECRET_KEY, algorithms=CONFIG.JWT_ALGORITHM, leeway=CONFIG.JWT_EXPIRE, options={"verify_exp": True})["id"] try: self.current_user = User.get(User.id == user_id) method(self, req, resp, *args, **kwargs) except User.DoesNotExist: raise UserNotExistsError() except jwt.ExpiredSignatureError: raise UnauthorizedError('Expired Signature') except jwt.InvalidSignatureError: raise UnauthorizedError('Invalid Signature') except jwt.DecodeError: raise UnauthorizedError('JWToken Decode Error')
def process_login(self, req, resp): req_data = req.context["data"] if not req_data: raise InvalidParameterError(req.context["data"]) username = req_data["username"] password = req_data["password"] try: user = User.get(User.username == username) _api_logger.debug(f"password: {password}, {user.password}") if not user.password.check_password(password): raise UnauthorizedError() payload = { "id": user.id, "username": user.username, "exp": datetime.utcnow() } jwtoken = jwt.encode( payload, CONFIG.SECRET_KEY, algorithm=CONFIG.JWT_ALGORITHM).decode("utf8") self.on_success(resp, {"username": username, "jwtoken": jwtoken}) except User.DoesNotExist: raise UnauthorizedError()