From: Thorsten Date: Fri, 6 Nov 2026 17:48:44 +0000 (+0100) Subject: Add optional local login X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=fb55fdbe7252f38db1baa924306c642940973fe9;p=sirius.git Add optional local login Twitter is the only way to sign in, which makes the web UI unusable without an X developer app. Setting LOCAL_LOGIN_USERNAME and LOCAL_LOGIN_PASSWORD_HASH enables a single account at /login/local and replaces the Twitter button on the landing page. Without them the route returns 404. Co-Authored-By: Claude Opus 5.5 --- diff --git a/README.md b/README.md index 2aae1dc..bf81150 100644 --- a/README.md +++ b/README.md @@ -61,10 +61,20 @@ The server can be configured with the following variables: TWITTER_OAUTH_CLIENT_KEY=... TWITTER_OAUTH_CLIENT_SECRET=... OAUTH_REDIRECT_URI=... +LOCAL_LOGIN_USERNAME=... +LOCAL_LOGIN_PASSWORD_HASH=... FLASK_CONFIG=... DATABASE_URL=... ``` +Setting both `LOCAL_LOGIN_*` variables enables a single local account at +`/login/local`, replacing the Twitter button on the landing page. Generate the +hash with: + +```sh +python -c "from getpass import getpass; from werkzeug.security import generate_password_hash as g; print(g(getpass()))" +``` + These can be set in the `.env` file, and an example is available in `.env.sample` in your checkout. ### Creating fake printers diff --git a/sirius/config.py b/sirius/config.py index 0b1d417..b9d8f24 100755 --- a/sirius/config.py +++ b/sirius/config.py @@ -12,6 +12,8 @@ class Config: TWITTER_OAUTH_CLIENT_KEY = os.getenv("TWITTER_OAUTH_CLIENT_KEY") TWITTER_OAUTH_CLIENT_SECRET = os.getenv("TWITTER_OAUTH_CLIENT_SECRET") OAUTH_REDIRECT_URI = os.getenv("OAUTH_REDIRECT_URI") + LOCAL_LOGIN_USERNAME = os.getenv("LOCAL_LOGIN_USERNAME") + LOCAL_LOGIN_PASSWORD_HASH = os.getenv("LOCAL_LOGIN_PASSWORD_HASH") @staticmethod def init_app(app): diff --git a/sirius/models/user.py b/sirius/models/user.py index 1687c9b..8edafa9 100644 --- a/sirius/models/user.py +++ b/sirius/models/user.py @@ -1,5 +1,6 @@ -"""User models. Login only via twitter for now to avoid the whole -forgot-password/reset-password dance. +"""User models. Login via twitter, or a single local account configured +through the environment, to avoid the whole forgot-password/reset-password +dance. """ import collections import datetime diff --git a/sirius/web/local_login.py b/sirius/web/local_login.py new file mode 100644 index 0000000..55015e5 --- /dev/null +++ b/sirius/web/local_login.py @@ -0,0 +1,55 @@ +import flask +import flask_login as login +import flask_wtf +import wtforms +from werkzeug.security import check_password_hash + +from sirius.models import user as user_model +from sirius.models.db import db + +blueprint = flask.Blueprint("local_login", __name__) + + +class LoginForm(flask_wtf.FlaskForm): + username = wtforms.StringField( + "Username", + validators=[wtforms.validators.DataRequired()], + ) + password = wtforms.PasswordField( + "Password", + validators=[wtforms.validators.DataRequired()], + ) + + +def credentials_valid(username, password): + config = flask.current_app.config + return username == config["LOCAL_LOGIN_USERNAME"] and check_password_hash( + config["LOCAL_LOGIN_PASSWORD_HASH"], password + ) + + +def get_or_create_user(username): + user = user_model.User.query.filter_by(username=username).first() + if user is None: + user = user_model.User(username=username) + db.session.add(user) + db.session.commit() + + return user + + +@blueprint.route("/local", methods=["GET", "POST"]) +def local_login(): + config = flask.current_app.config + if not config["LOCAL_LOGIN_USERNAME"] or not config["LOCAL_LOGIN_PASSWORD_HASH"]: + flask.abort(404) + + form = LoginForm() + if form.validate_on_submit(): + if credentials_valid(form.username.data, form.password.data): + login.login_user(get_or_create_user(form.username.data)) + return flask.redirect("/") + + flask.flash("Wrong username or password.") + + return flask.render_template("local_login.html", form=form) diff --git a/sirius/web/templates/landing.html b/sirius/web/templates/landing.html index 03a02a9..b7c8083 100644 --- a/sirius/web/templates/landing.html +++ b/sirius/web/templates/landing.html @@ -12,9 +12,13 @@ page_content %} app, or via the Print Key API.

+ {% if config.LOCAL_LOGIN_USERNAME and config.LOCAL_LOGIN_PASSWORD_HASH %} + Sign in + {% else %} Sign in with Twitter + {% endif %}

{% endblock %} diff --git a/sirius/web/templates/local_login.html b/sirius/web/templates/local_login.html new file mode 100644 index 0000000..831972a --- /dev/null +++ b/sirius/web/templates/local_login.html @@ -0,0 +1,28 @@ +{% extends "base.html" %} + +{% block title %}Sign in{% endblock %} + +{% block page_content %} +
+
+ +

Sign in

+ +
+ {{ form.csrf_token }} + +
+ + {{ form.username(class="form-control", autocomplete="username") }} +
+ +
+ + {{ form.password(class="form-control", autocomplete="current-password") }} +
+ +
+ +
+
+{% endblock %} diff --git a/sirius/web/test_local_login.py b/sirius/web/test_local_login.py new file mode 100644 index 0000000..f6e3703 --- /dev/null +++ b/sirius/web/test_local_login.py @@ -0,0 +1,60 @@ +from unittest import mock + +from werkzeug.security import generate_password_hash + +from sirius.models import user as user_model +from sirius.testing import base + + +# pylint: disable=no-member +class TestLocalLogin(base.Base): + def create_app(self): + app = super().create_app() + app.config["LOCAL_LOGIN_USERNAME"] = "localuser" + app.config["LOCAL_LOGIN_PASSWORD_HASH"] = generate_password_hash("sesame") + return app + + def post_login(self, username, password): + return self.client.post( + "/login/local", data=dict(username=username, password=password) + ) + + def signed_in_user_id(self): + with self.client.session_transaction() as session: + return session.get("user_id") + + def test_correct_password_signs_in_and_creates_user(self): + response = self.post_login("localuser", "sesame") + self.assertRedirects(response, "/") + user = user_model.User.query.filter_by(username="localuser").one() + self.assertEqual(self.signed_in_user_id(), user.id) + + def test_existing_user_is_reused(self): + self.post_login("localuser", "sesame") + self.post_login("localuser", "sesame") + self.assertEqual( + user_model.User.query.filter_by(username="localuser").count(), 1 + ) + + def test_wrong_password_is_rejected(self): + response = self.post_login("localuser", "wrong") + self.assert200(response) + self.assertIsNone(self.signed_in_user_id()) + + def test_other_user_cannot_use_the_password(self): + response = self.post_login("testuser", "sesame") + self.assert200(response) + self.assertIsNone(self.signed_in_user_id()) + + def test_landing_offers_local_login(self): + self.assertIn(b"/login/local", self.client.get("/").data) + + +class TestLocalLoginDisabled(base.Base): + def test_not_found_when_unconfigured(self): + with mock.patch.dict( + self.app.config, + {"LOCAL_LOGIN_USERNAME": None, "LOCAL_LOGIN_PASSWORD_HASH": None}, + ): + self.assert404(self.client.get("/login/local")) + self.assertNotIn(b"/login/local", self.client.get("/").data) diff --git a/sirius/web/webapp.py b/sirius/web/webapp.py index 6e05174..e9dfb9d 100644 --- a/sirius/web/webapp.py +++ b/sirius/web/webapp.py @@ -20,6 +20,7 @@ from sirius.models import hardware from sirius.models import messages from sirius.web import landing +from sirius.web import local_login from sirius.web import twitter from sirius.web import login from sirius.web import admin @@ -53,6 +54,7 @@ def create_app(config_name): app.register_blueprint(stats.blueprint) app.register_blueprint(landing.blueprint) app.register_blueprint(twitter.blueprint, url_prefix="/login") + app.register_blueprint(local_login.blueprint, url_prefix="/login") app.register_blueprint(printer_overview.blueprint) app.register_blueprint(printer_print.blueprint) app.register_blueprint(external_api.blueprint)