Compare commits

...

2 Commits

Author SHA1 Message Date
b0f065d976 Logo 2025-07-15 00:03:44 -04:00
def6635738 Got OAuth working 2025-07-14 16:18:37 -04:00
7 changed files with 222 additions and 31 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.venv
config.yaml
shelf

View File

@ -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

View File

@ -1,3 +1,4 @@
flask
authlib
requests
dynaconf

123
static/bellflower.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 876 KiB

View File

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body style="text-align: center;">
{% block content %}{% endblock %}
</body>
</html>

View File

@ -1,10 +1,32 @@
{% extends "base.html" %}
{% set title = "Visitor Check-in System" %}
{% block content %}
<h1>Welcome to Visitor Check-in System</h1>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bellflower</title>
</head>
<body style="text-align: center;">
<h1>Welcome, {{user.name}}</h1>
<p>Visitors are currently {{ status }}</p>
<form action="/checkin" method="post">
<input type="submit" value="Check In Visitor">
</form>
{% endblock %}
<table id="checkins">
<thead>
<tr>
<th>Visitor Name</th>
<th>Check-in Time</th>
</tr>
</thead>
<tbody>
{% for username in checkins.keys() %}
<tr>
<td>{{ username }}</td>
<td>{{ checkins[username][-1] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

View File

@ -1,11 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Status</title>
</head>
<body>
<h1>Current Status: {{ status }}</h1>
<a href="/">Go back to main page</a>
</body>
</html>