Skip to content

Commit

Permalink
Add permission expiry (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdabtieu committed Oct 11, 2024
1 parent d23b7aa commit 760daca
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests
import sys
import uuid
from datetime import datetime
from datetime import datetime, timedelta

import jwt
from flask import (abort, Flask, flash, redirect, render_template, request,
Expand Down Expand Up @@ -102,8 +102,10 @@
logging.warning("Settings validation: Homepage file nonexistent.")


@app.before_request
def check_for_maintenance():
"""
Pre-request handler to block requests during maintenance mode
"""
# Don't prevent login or getting assets
if request.path == '/login' or request.path.startswith('/assets/'):
return
Expand All @@ -123,6 +125,30 @@ def check_for_maintenance():
else:
flash("Maintenance mode is enabled", "maintenance")

def refresh_perms():

Check failure on line 128 in src/application.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

E302 expected 2 blank lines, found 1

Check failure on line 128 in src/application.py

View workflow job for this annotation

GitHub Actions / build (windows-latest)

E302 expected 2 blank lines, found 1
"""
Pre-request handler to ensure permission freshness
"""
if not session.get("user_id"):
return
if "perms_expiry" not in session: # Old, invalid session
session.clear()
return redirect("/login")
safer_methods = ["GET", "HEAD"]
if request.method not in safer_methods or (request.method in safer_methods and session["perms_expiry"] < datetime.now()):

Check failure on line 138 in src/application.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

E501 line too long (125 > 90 characters)

Check failure on line 138 in src/application.py

View workflow job for this annotation

GitHub Actions / build (windows-latest)

E501 line too long (125 > 90 characters)
perms = db.execute("SELECT * FROM user_perms WHERE user_id=?", session["user_id"])
session["perms"] = set([x["perm_id"] for x in perms])
session["perms_expiry"] = datetime.now() + timedelta(seconds=300)
return

@app.before_request

Check failure on line 144 in src/application.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

E302 expected 2 blank lines, found 1

Check failure on line 144 in src/application.py

View workflow job for this annotation

GitHub Actions / build (windows-latest)

E302 expected 2 blank lines, found 1
def before_hooks():
funcs = [check_for_maintenance, refresh_perms]
for f in funcs:
r = f()
if r:
return r

Check warning on line 151 in src/application.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

W293 blank line contains whitespace

Check warning on line 151 in src/application.py

View workflow job for this annotation

GitHub Actions / build (windows-latest)

W293 blank line contains whitespace

@app.route("/")
def index():
Expand Down Expand Up @@ -267,6 +293,7 @@ def login():
session["user_id"] = rows[0]["id"]
session["username"] = rows[0]["username"]
session["perms"] = set([x["perm_id"] for x in perms])
session["perms_expiry"] = datetime.now() + timedelta(seconds=300)

logger.info((f"User #{session['user_id']} ({session['username']}) logged in "
f"on IP {request.remote_addr}"), extra={"section": "auth"})
Expand Down Expand Up @@ -389,6 +416,7 @@ def confirm_register(token):
session["user_id"] = user["id"]
session["username"] = user["username"]
session["perms"] = set([x["perm_id"] for x in perms])
session["perms_expiry"] = datetime.now() + timedelta(seconds=300)

logger.info((f"User #{session['user_id']} ({session['username']}) has successfully "
f"registered on IP {request.remote_addr}"), extra={"section": "auth"})
Expand Down Expand Up @@ -441,6 +469,7 @@ def confirm_login(token):
session["user_id"] = user["id"]
session["username"] = user["username"]
session["perms"] = set([x["perm_id"] for x in perms])
session["perms_expiry"] = datetime.now() + timedelta(seconds=300)

logger.info((f"User #{session['user_id']} ({session['username']}) logged in via 2FA "
f"on IP {request.remote_addr}"), extra={"section": "auth"})
Expand Down

0 comments on commit 760daca

Please sign in to comment.