Got OAuth working
This commit is contained in:
parent
36c129b939
commit
def6635738
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
.venv
|
.venv
|
||||||
config.yaml
|
config.yaml
|
||||||
|
shelf
|
||||||
73
app.py
73
app.py
@ -1,14 +1,31 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
import requests
|
||||||
|
import time
|
||||||
from flask import Flask, render_template, request, redirect, url_for, session
|
from flask import Flask, render_template, request, redirect, url_for, session
|
||||||
from authlib.integrations.flask_client import OAuth
|
from authlib.integrations.flask_client import OAuth
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from dynaconf import Dynaconf
|
from dynaconf import Dynaconf
|
||||||
import logging
|
import logging
|
||||||
|
import shelve
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
config = Dynaconf(settings_files = ['config.yaml'])
|
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:
|
class Pinctrl:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
@ -31,6 +48,7 @@ class Pinctrl:
|
|||||||
pinctrl = Pinctrl()
|
pinctrl = Pinctrl()
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
app.secret_key = config.oauth.secret_key
|
||||||
oauth = OAuth(app)
|
oauth = OAuth(app)
|
||||||
|
|
||||||
oauth.register(
|
oauth.register(
|
||||||
@ -43,24 +61,71 @@ for led, pin in config.pinout.led.items():
|
|||||||
pinctrl.output(pin)
|
pinctrl.output(pin)
|
||||||
pinctrl.off(pin)
|
pinctrl.off(pin)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def homepage():
|
def homepage():
|
||||||
user = session.get('user')
|
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')
|
@app.route('/login')
|
||||||
def login():
|
def login():
|
||||||
|
logging.info("Login requested")
|
||||||
redirect_uri = url_for('auth', _external=True)
|
redirect_uri = url_for('auth', _external=True)
|
||||||
return oauth.jimlab.authorize_redirect(redirect_uri)
|
return oauth.jimlab.authorize_redirect(redirect_uri)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/auth')
|
@app.route('/auth')
|
||||||
def auth():
|
def auth():
|
||||||
token = oauth.jimlab.authorize_access_token()
|
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('/')
|
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('/')
|
# @app.route('/')
|
||||||
# def index():
|
# def index():
|
||||||
# logger.info(oauth.jimlab.get('user'))
|
# logger.info(oauth.jimlab.get('user'))
|
||||||
@ -106,4 +171,4 @@ if __name__ == '__main__':
|
|||||||
for led, pin in config.pinout.led.items():
|
for led, pin in config.pinout.led.items():
|
||||||
pinctrl.off(pin)
|
pinctrl.off(pin)
|
||||||
|
|
||||||
#with shelve.open('db', 'w', writeback=True) as db
|
|
||||||
@ -1,3 +1,4 @@
|
|||||||
flask
|
flask
|
||||||
authlib
|
authlib
|
||||||
|
requests
|
||||||
dynaconf
|
dynaconf
|
||||||
@ -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>
|
|
||||||
@ -1,10 +1,32 @@
|
|||||||
{% extends "base.html" %}
|
<!DOCTYPE html>
|
||||||
{% set title = "Visitor Check-in System" %}
|
<html lang="en">
|
||||||
|
<head>
|
||||||
{% block content %}
|
<meta charset="UTF-8">
|
||||||
<h1>Welcome to Visitor Check-in System</h1>
|
<title>Bellflower</title>
|
||||||
|
</head>
|
||||||
|
<body style="text-align: center;">
|
||||||
|
<h1>Welcome, {{user.name}}</h1>
|
||||||
<p>Visitors are currently {{ status }}</p>
|
<p>Visitors are currently {{ status }}</p>
|
||||||
<form action="/checkin" method="post">
|
<form action="/checkin" method="post">
|
||||||
<input type="submit" value="Check In Visitor">
|
<input type="submit" value="Check In Visitor">
|
||||||
</form>
|
</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>
|
||||||
@ -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>
|
|
||||||
Loading…
x
Reference in New Issue
Block a user