Skip to content

Commit

Permalink
Add Auth example
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbaldwin44 committed Jan 9, 2024
1 parent 7df8b9b commit a8f1ef3
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions examples/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import os
import json
from locust import HttpUser, events
from flask import url_for, Blueprint, request, make_response, redirect, session
from flask_login import UserMixin, login_user


class LocustHttpUser(HttpUser):
pass


class User(UserMixin):
def __init__(self, username):
self.username = username

def get_id(self):
return self.username


auth_blueprint = Blueprint("auth", "web_ui_auth")


def load_user(user_id):
return User(session.get("username"))


@events.init.add_listener
def locust_init(environment, **kwargs):
if environment.web_ui:
# environment.web_ui.login_manager.user_loader(load_user)

environment.web_ui.app.config["SECRET_KEY"] = os.getenv("FLASK_SECRET_KEY")

environment.web_ui.auth_args = {
"username_password_callback": "/login_submit",
"auth_providers": [
{
"label": "Github",
"callback_url": "/login/github",
"icon_url": "https://static-00.iconduck.com/assets.00/github-icon-1024x994-4h5sdmko.png",
},
],
}

@auth_blueprint.route("/login/github")
def google_login():
# Implement authentication with desired auth provider
username = "username"
session["username"] = username
login_user(User("username"))

return redirect(url_for("index"))

@auth_blueprint.route("/login_submit")
def login_submit():
username = request.args.get("username")
password = request.args.get("password")

# Implement real password verification here
if password:
session["username"] = username
login_user(User(username))

return redirect(url_for("index"))

environment.web_ui.auth_args = {**environment.web_ui.auth_args, "error": "Invalid username or password"}

return redirect(url_for("login"))

environment.web_ui.app.register_blueprint(auth_blueprint)

0 comments on commit a8f1ef3

Please sign in to comment.