]> git.aero2k.de Git - sirius.git/commitdiff
Add optional local login
authorThorsten <mail@aero2k.de>
Fri, 6 Nov 2026 17:48:44 +0000 (18:48 +0100)
committerThorsten <mail@aero2k.de>
Fri, 6 Nov 2026 17:48:44 +0000 (18:48 +0100)
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 <noreply@anthropic.com>
README.md
sirius/config.py
sirius/models/user.py
sirius/web/local_login.py [new file with mode: 0644]
sirius/web/templates/landing.html
sirius/web/templates/local_login.html [new file with mode: 0644]
sirius/web/test_local_login.py [new file with mode: 0644]
sirius/web/webapp.py

index 2aae1dcafa1257bdf86b0d06ede159fff35c767e..bf81150da98e7a79040d0e65d6fb46cecafd5ceb 100644 (file)
--- 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
index 0b1d4174baf0a932ff71645334a78cd03d66b09e..b9d8f242c53156ee4d25b76b56757b361c649471 100755 (executable)
@@ -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):
index 1687c9bd8b127d826fad9e9f500cd1618437527a..8edafa95a7c765c8ba67e0d2745186a789026455 100644 (file)
@@ -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 (file)
index 0000000..55015e5
--- /dev/null
@@ -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)
index 03a02a9fb0a0c8549308ea33bad36fce2576ab9d..b7c80833fef319efe7c0097de94d32ed130c288b 100644 (file)
@@ -12,9 +12,13 @@ page_content %}
     app, or via the Print Key API.
   </p>
   <p>
+    {% if config.LOCAL_LOGIN_USERNAME and config.LOCAL_LOGIN_PASSWORD_HASH %}
+    <a role="button" class="btn btn-primary" href="/login/local">Sign in</a>
+    {% else %}
     <a role="button" class="btn btn-primary" href="/login/twitter"
       >Sign in with Twitter</a
     >
+    {% endif %}
   </p>
 </div>
 {% endblock %}
diff --git a/sirius/web/templates/local_login.html b/sirius/web/templates/local_login.html
new file mode 100644 (file)
index 0000000..831972a
--- /dev/null
@@ -0,0 +1,28 @@
+{% extends "base.html" %}
+
+{% block title %}Sign in{% endblock %}
+
+{% block page_content %}
+<div class="row">
+       <div class="col-md-4">
+
+<h1>Sign in</h1>
+
+<form method="POST">
+       {{ form.csrf_token }}
+
+       <div class="form-group">
+               <label>{{ form.username.label }}</label>
+               {{ form.username(class="form-control", autocomplete="username") }}
+       </div>
+
+       <div class="form-group">
+               <label>{{ form.password.label }}</label>
+               {{ form.password(class="form-control", autocomplete="current-password") }}
+       </div>
+       <button class="btn btn-primary" type="submit">Sign in</button>
+</form>
+
+</div>
+</div>
+{% endblock %}
diff --git a/sirius/web/test_local_login.py b/sirius/web/test_local_login.py
new file mode 100644 (file)
index 0000000..f6e3703
--- /dev/null
@@ -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)
index 6e05174d398aaec6c587683cfe77781b32d4e890..e9dfb9d8729d74a76ccfe0bb721e40ec6fca8073 100644 (file)
@@ -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)