Initial work on auth-tokens
This commit is contained in:
parent
5551b6438a
commit
adea2b1545
@ -2,8 +2,10 @@ import inspect
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from flask import Flask, render_template
|
from flask import Flask, render_template
|
||||||
|
from flask_login import LoginManager
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
|
auth,
|
||||||
db,
|
db,
|
||||||
entry,
|
entry,
|
||||||
inventory,
|
inventory,
|
||||||
@ -21,6 +23,15 @@ def create_app():
|
|||||||
|
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
|
|
||||||
|
login_manager = LoginManager()
|
||||||
|
login_manager.login_view = 'auth.login'
|
||||||
|
login_manager.init_app(app)
|
||||||
|
|
||||||
|
@login_manager.user_loader
|
||||||
|
def load_user(user_id):
|
||||||
|
# since the user_id is just the primary key of our user table, use it in the query for the user
|
||||||
|
return auth.User()
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def utility_processor():
|
def utility_processor():
|
||||||
return dict(inspect.getmembers(template_utils, inspect.isfunction))
|
return dict(inspect.getmembers(template_utils, inspect.isfunction))
|
||||||
@ -28,8 +39,11 @@ def create_app():
|
|||||||
app.register_blueprint(location.bp)
|
app.register_blueprint(location.bp)
|
||||||
app.register_blueprint(inventory.bp)
|
app.register_blueprint(inventory.bp)
|
||||||
app.register_blueprint(entry.bp)
|
app.register_blueprint(entry.bp)
|
||||||
|
app.register_blueprint(auth.auth)
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
return render_template("index.html")
|
return render_template("index.html")
|
||||||
|
|
||||||
|
print("Jon started. Token: %s" % auth.ACCESS_TOKEN)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
37
jon/auth.py
Normal file
37
jon/auth.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
from flask import Blueprint, request, redirect
|
||||||
|
from flask_login import login_user, UserMixin, login_required, logout_user
|
||||||
|
from . import db
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
|
auth = Blueprint('auth', __name__)
|
||||||
|
|
||||||
|
ACCESS_TOKEN = ''.join(random.choice(string.ascii_lowercase) for i in range(64))
|
||||||
|
|
||||||
|
class User(UserMixin):
|
||||||
|
id: int = 0
|
||||||
|
|
||||||
|
@auth.route('/login')
|
||||||
|
def login():
|
||||||
|
token = request.args.get('token')
|
||||||
|
next: str = request.args.get('next') or "/"
|
||||||
|
if token is None:
|
||||||
|
# TODO: make template
|
||||||
|
return """
|
||||||
|
No token provided!
|
||||||
|
<form action="" method="get">
|
||||||
|
<input type="password" name="token" placeholder="Token" />
|
||||||
|
<input type="hidden" hidden name="next" value="{next}" />
|
||||||
|
<input type="submit" value="login" />
|
||||||
|
</form>
|
||||||
|
""".format(next=next)
|
||||||
|
if token == ACCESS_TOKEN:
|
||||||
|
login_user(User(), remember=True)
|
||||||
|
return redirect(next)
|
||||||
|
else:
|
||||||
|
return "Invalid token!"
|
||||||
|
|
||||||
|
@auth.route('/logout')
|
||||||
|
def logout():
|
||||||
|
logout_user()
|
||||||
|
return redirect("/")
|
@ -1,4 +1,5 @@
|
|||||||
from flask import Blueprint, redirect, render_template, request, session
|
from flask import Blueprint, redirect, render_template, request, session
|
||||||
|
from flask_login import login_required
|
||||||
|
|
||||||
from . import db
|
from . import db
|
||||||
|
|
||||||
@ -7,6 +8,7 @@ bp = Blueprint("inventory", __name__, url_prefix="/inventory")
|
|||||||
|
|
||||||
|
|
||||||
@bp.get("/")
|
@bp.get("/")
|
||||||
|
@login_required
|
||||||
def index():
|
def index():
|
||||||
location = session.get("location", None)
|
location = session.get("location", None)
|
||||||
items = db.run_query("get_inventory_overview.sql", {
|
items = db.run_query("get_inventory_overview.sql", {
|
||||||
@ -19,6 +21,7 @@ def index():
|
|||||||
|
|
||||||
|
|
||||||
@bp.get("/report")
|
@bp.get("/report")
|
||||||
|
@login_required
|
||||||
def read_report():
|
def read_report():
|
||||||
location = session.get("location", None)
|
location = session.get("location", None)
|
||||||
items = db.run_query("get_inventory_report.sql", {
|
items = db.run_query("get_inventory_report.sql", {
|
||||||
@ -31,6 +34,7 @@ def read_report():
|
|||||||
|
|
||||||
|
|
||||||
@bp.get("/item/<item_id>")
|
@bp.get("/item/<item_id>")
|
||||||
|
@login_required
|
||||||
def read_item(item_id: int):
|
def read_item(item_id: int):
|
||||||
item = db.run_query("get_item_by_id.sql", {
|
item = db.run_query("get_item_by_id.sql", {
|
||||||
"item_id": item_id
|
"item_id": item_id
|
||||||
@ -53,6 +57,7 @@ def read_item(item_id: int):
|
|||||||
|
|
||||||
|
|
||||||
@bp.post("/item/<item_id>/deactivate")
|
@bp.post("/item/<item_id>/deactivate")
|
||||||
|
@login_required
|
||||||
def deactivate_item(item_id: int):
|
def deactivate_item(item_id: int):
|
||||||
item = db.run_query("get_item_by_id.sql", {
|
item = db.run_query("get_item_by_id.sql", {
|
||||||
"item_id": item_id
|
"item_id": item_id
|
||||||
@ -70,6 +75,7 @@ def deactivate_item(item_id: int):
|
|||||||
|
|
||||||
|
|
||||||
@bp.post("/correction")
|
@bp.post("/correction")
|
||||||
|
@login_required
|
||||||
def create_correction():
|
def create_correction():
|
||||||
try:
|
try:
|
||||||
item_id = int(request.form.get("item_id"))
|
item_id = int(request.form.get("item_id"))
|
||||||
@ -95,6 +101,7 @@ def create_correction():
|
|||||||
|
|
||||||
|
|
||||||
@bp.post("/transfer")
|
@bp.post("/transfer")
|
||||||
|
@login_required
|
||||||
def transfer_items():
|
def transfer_items():
|
||||||
try:
|
try:
|
||||||
from_item_id = int(request.form.get("from_item_id"))
|
from_item_id = int(request.form.get("from_item_id"))
|
||||||
|
@ -88,6 +88,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li {{ "class=current-page" if request.path.startswith("/logout") else "" }}><a href="/logout">Logout</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
blinker==1.6.2
|
blinker==1.6.2
|
||||||
click==8.1.3
|
click==8.1.3
|
||||||
Flask==2.3.2
|
Flask==2.3.2
|
||||||
|
flask-login
|
||||||
itsdangerous==2.1.2
|
itsdangerous==2.1.2
|
||||||
Jinja2==3.1.2
|
Jinja2==3.1.2
|
||||||
MarkupSafe==2.1.2
|
MarkupSafe==2.1.2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user