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
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):
-"""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
--- /dev/null
+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)
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 %}
--- /dev/null
+{% 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 %}
--- /dev/null
+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)
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
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)