From def66357381ed33bd6fa0a3ba1ac3a78cb6fab8e Mon Sep 17 00:00:00 2001 From: Jim Shepich III Date: Mon, 14 Jul 2025 16:18:37 -0400 Subject: [PATCH] Got OAuth working --- .gitignore | 1 + app.py | 73 ++++++++++++++++++++++++++++++++++++++++--- requirements.txt | 1 + templates/base.html | 10 ------ templates/index.html | 34 ++++++++++++++++---- templates/status.html | 11 ------- 6 files changed, 99 insertions(+), 31 deletions(-) delete mode 100644 templates/base.html delete mode 100644 templates/status.html diff --git a/.gitignore b/.gitignore index 4a4c19a..8f5b1e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .venv config.yaml +shelf \ No newline at end of file diff --git a/app.py b/app.py index a974305..ca0fef1 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,31 @@ import subprocess +import requests +import time from flask import Flask, render_template, request, redirect, url_for, session from authlib.integrations.flask_client import OAuth from time import sleep from dynaconf import Dynaconf import logging +import shelve logger = logging.getLogger(__name__) config = Dynaconf(settings_files = ['config.yaml']) +# Setup shelf. +with shelve.open('shelf', 'c', writeback=True) as shelf: + if 'logins' not in shelf.keys(): + shelf['logins'] = {} + + if 'checkins' not in shelf.keys(): + shelf['checkins'] = {} + + if 'admissions' not in shelf.keys(): + shelf['admissions'] = {} + + + + class Pinctrl: def __init__(self): pass @@ -31,6 +48,7 @@ class Pinctrl: pinctrl = Pinctrl() app = Flask(__name__) +app.secret_key = config.oauth.secret_key oauth = OAuth(app) oauth.register( @@ -43,24 +61,71 @@ for led, pin in config.pinout.led.items(): pinctrl.output(pin) pinctrl.off(pin) + @app.route('/') def homepage(): user = session.get('user') - return render_template('index.html', user=user) + if not user: + return redirect(url_for('login')) + + logging.info(f"User {user} is logged in.") + with shelve.open('shelf', 'r') as shelf: + return render_template( + 'index.html', + user=user, + logins=shelf.get('logins', {}), + checkins=shelf.get('checkins', {}), + admissions=shelf.get('admissions', {}), + ) + + @app.route('/login') def login(): + logging.info("Login requested") redirect_uri = url_for('auth', _external=True) return oauth.jimlab.authorize_redirect(redirect_uri) - @app.route('/auth') def auth(): token = oauth.jimlab.authorize_access_token() - session['user'] = token['userinfo'] + logger.critical(f"Token received: {token}") + + # Fetch user info + resp = oauth.jimlab.get('https://auth.jimlab.io/application/o/userinfo/') # or the correct user info endpoint + userinfo = resp.json() + logger.critical(f"Userinfo: {userinfo}") + + session['user'] = userinfo + + # Log the login. + with shelve.open('shelf', 'w', writeback=True) as shelf: + username = userinfo.get('preferred_username', 'unknown') + if username not in shelf['logins']: + shelf['logins'][username] = [] + shelf['logins'][username].append((userinfo, time.time())) + return redirect('/') +@app.route('/checkin', methods=['POST']) +def check_in(): + + if not session.get('user'): + return redirect(url_for('login')) + user = session['user'] + username = user.get('preferred_username', 'unknown') + logger.info(f"Check-in requested by user: {username}") + + with shelve.open('shelf', 'w', writeback=True) as shelf: + if username not in shelf['checkins']: + shelf['checkins'][username] = [] + shelf['checkins'][username].append(time.time()) + + return redirect(url_for('homepage')) + + + # @app.route('/') # def index(): # logger.info(oauth.jimlab.get('user')) @@ -106,4 +171,4 @@ if __name__ == '__main__': for led, pin in config.pinout.led.items(): pinctrl.off(pin) -#with shelve.open('db', 'w', writeback=True) as db \ No newline at end of file + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index dde9d99..fe95fcf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ flask authlib +requests dynaconf \ No newline at end of file diff --git a/templates/base.html b/templates/base.html deleted file mode 100644 index d9f2469..0000000 --- a/templates/base.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - {{ title }} - - - {% block content %}{% endblock %} - - \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index f7534eb..70f2367 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,10 +1,32 @@ -{% extends "base.html" %} -{% set title = "Visitor Check-in System" %} - -{% block content %} -

Welcome to Visitor Check-in System

+ + + + + Bellflower + + +

Welcome, {{user.name}}

Visitors are currently {{ status }}

-{% endblock %} \ No newline at end of file + + + + + + + + + + {% for username in checkins.keys() %} + + + + + {% endfor %} + + +
Visitor NameCheck-in Time
{{ username }}{{ checkins[username][-1] }}
+ + \ No newline at end of file diff --git a/templates/status.html b/templates/status.html deleted file mode 100644 index 064a8c8..0000000 --- a/templates/status.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - Status - - -

Current Status: {{ status }}

- Go back to main page - - \ No newline at end of file