forked from locustio/locust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7df8b9b
commit a8f1ef3
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |