else: raise UnauthorizedException() def validate_credentials(self, username: str, password: str) -> Dict: if username in DATABASE and DATABASE[username].get( "password") == password: return DATABASE[username] else: raise UnauthorizedException() # Create a strategy basic_strategy = BasicAuthentication() # Create a blueprint as a safe zone secure = customs.safe_zone(Blueprint("secure", __name__, url_prefix="/secure"), strategies=[basic_strategy]) # ----------------------- # # Define some open routes # # ----------------------- # @app.route("/") def index(): return "Success" # ------------------------------ # # Define some (protected) routes # # ------------------------------ #
else: raise UnauthorizedException() def validate_credentials(self, username: str, password: str) -> Dict: if username in DATABASE and DATABASE[username].get( "password") == password: return DATABASE[username] else: raise UnauthorizedException() # Create a strategy basic_strategy = BasicAuthentication() # Declare the entire app a safe zone, all routes will be protected in the same way customs.safe_zone(app, strategies=[basic_strategy]) # ------------------ # # Define some routes # # ------------------ # @app.route("/") def index(): return "Success" @app.route("/user_info") def user_info(user: Dict): user.pop("password") return jsonify(user)
class JWTAuthentication(JWTStrategy): def get_or_create_user(self, user: Dict) -> Dict: if user.get("username") in DATABASE: return DATABASE[user["username"]] else: raise UnauthorizedException() # Create a strategies basic_strategy = BasicAuthentication() jwt_strategy = JWTAuthentication(key="9E30771F-6957-4C49-A8A0-55C292025349") # Create a blueprint as a safe zone, protected using the JWT token strategy api = customs.safe_zone( Blueprint("api", __name__, url_prefix="/api"), strategies=[jwt_strategy] ) # ----------------------- # # Define some open routes # # ----------------------- # # Open to everyone @app.route("/") def index(): return "Success" # Use basic authentication to authenticate the user, create a token for subsequent calls @app.route("/login", methods=["POST"])