Example #1
0
    def post(self):
        if request.authorization is None:
            return {"errors": {"detail": "Basic auth header missing"}}, 400

        username = request.authorization.get("username")
        password = request.authorization.get("password")
        LOGGER.debug({username: password})

        user = User.query.filter_by(username=username).first()
        if user is not None:
            try:
                if bcrypt.checkpw(password.encode("utf8"), user.pw_hash):
                    payload = {
                        "sub": username,
                        "is_admin": user.is_admin,
                        "is_mod": user.is_mod,
                        "iat": int(time.time()),
                    }
                    token = jwt.encode(payload, SECRET_KEY, algorithm="HS256")
                    LOGGER.debug({"Token": token})
                    return {
                        "data": {
                            "access_token": token.decode("utf-8")
                        }
                    }, 200
            except Exception as e:
                LOGGER.error({"Exception": e})
                return {"errors": {"detail": "server error"}}, 500
        return {"errors": {"detail": "Invalid Credentials"}}, 403
Example #2
0
    def post(self, jwt_payload=None):
        """Create a new Topic."""

        args = post_parser.parse_args(strict=True)
        LOGGER.info({"Args": args})

        topic = Topic.get_topic(args.name)
        if topic is None:
            try:
                record = Topic(name=args.name, descript=args.descript)
                record.save()
                return Success({
                    "message": f"topic {args.name} created"
                }).to_json(), 200
            except Exception as e:
                LOGGER.error({"Exception": e})
                return Error(str(e)).to_json(), 500
        return Fail(f"topic {args.name} exists").to_json(), 400