Skip to content

Commit

Permalink
Merge branch 'oauth'
Browse files Browse the repository at this point in the history
  • Loading branch information
fsoest committed Apr 30, 2024
2 parents 4dc17b1 + b2f1507 commit d88973d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 14 deletions.
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ CORE_API=
DC_PATH=
DF_LINK=
MOODLE_AUTH=
MOODLE_LINK
MOODLE_LINK=
OAUTH_CLIENT_ID=
OAUTH_CLIENT_SECRET=
OAUTH_HOST=
APP_URL=
FLASK_SECRET_KEY=
60 changes: 55 additions & 5 deletions monitor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
from flask import Flask, render_template, request
import os
import requests
from urllib.parse import quote_plus

from flask import Flask, render_template, request, session, redirect, url_for
from .core_requests import get_endorsements, get_roster, get_logins, get_station_data, get_rating, required_courses
from .monitor_login import check_connection
from dotenv import load_dotenv


load_dotenv()
oauth_host = os.getenv('OAUTH_HOST')


def login_url():
id = os.environ['OAUTH_CLIENT_ID']
redirect_url = quote_plus(f'{os.getenv('APP_URL')}/callback')
url = f"{oauth_host}/oauth/authorize?client_id={id}&response_type=code&redirect_uri={redirect_url}&scope={'full_name'}"
return url


def create_app():
app = Flask(__name__, instance_relative_config=True)
app.secret_key = os.environ['FLASK_SECRET_KEY']

@app.route('/', methods=('GET', 'POST'))
def main():
user_id = session.get('user_id')
if user_id is None:
return redirect(login_url())
cid = int(user_id)
if request.method == 'POST':
# Check whether ID exists
try:
rating = get_rating(int(request.form['cid']))
rating = get_rating(cid)
except:
rating = False
out = {
Expand All @@ -26,7 +47,7 @@ def main():
roster = get_roster()
datahub = get_station_data()
connection = {
'cid': int(request.form['cid']),
'cid': cid,
'callsign': request.form['station'].upper(),
'name': '',
'rating': rating,
Expand All @@ -38,8 +59,37 @@ def main():
is_ctr_sector = request.form['station'].upper().split('_')[-1] == 'CTR'
fam_msg = is_ctr_sector and out['may_control']

return render_template('main.html', request=request, out=out, fam_msg=fam_msg)
return render_template('main.html', request=request, out=out, fam_msg=fam_msg, name=session.get('user_name'))
else:
return render_template('main.html', request=request)
return render_template('main.html', request=request, name=session.get('user_name'))

@app.route('/callback')
def callback():
session.clear()
code = request.args.get('code')
auth_url = f"{oauth_host}/oauth/token"
payload = {
'grant_type': 'authorization_code',
'client_id': os.environ['OAUTH_CLIENT_ID'],
'client_secret': os.environ['OAUTH_CLIENT_SECRET'],
'code': code,
'redirect_uri': f'{os.getenv('APP_URL')}/callback'
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
response = requests.request("POST", auth_url, headers=headers, data=payload)
if response.status_code != 200:
return redirect(url_for('main'))
headers = {
'Accept': 'application/json',
'Authorization': f'Bearer {response.json()["access_token"]}'
}
response = requests.request("GET", f"{oauth_host}/api/user", headers=headers)
if response.status_code != 200:
return redirect(url_for('main'))
session['user_id'] = response.json()['data']['cid']
session['user_name'] = response.json()['data']['personal']['name_first']
return redirect(url_for('main'))
return app
2 changes: 0 additions & 2 deletions monitor/core_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def get_rating(id: int) -> int:
return requests.get(f'https://api.vatsim.net/api/ratings/{id}/').json()['rating']


#@cached(cache=TTLCache(maxsize=float('inf'), ttl=60))
def check_course_completion(course: dict, cid: int) -> bool:
return False
course_id = course['link'].split('id=')[-1]
headers = {
'Authorization': moodle_auth
Expand Down
22 changes: 22 additions & 0 deletions monitor/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Sticky footer styles
-------------------------------------------------- */
html {
position: relative;
min-height: 100%;
}
body {
margin-bottom: 60px; /* Margin bottom by footer height */
padding-top: 15px;
}
.footer {
position: absolute;
bottom: 0;
width: 100%;
height: 60px; /* Set the fixed height of the footer here */
line-height: 60px; /* Vertically center the text there */
background-color: #f5f5f5;
}

a {
color: lightslategray;
}
11 changes: 10 additions & 1 deletion monitor/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!doctype html>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">

<section class="content">
<div class="container-fluid">
Expand All @@ -11,4 +11,13 @@

{% block content %}{% endblock %}
</div>

<footer class="footer">
<div class="container">
<span class="text-muted">
<a href="https://vatsim-germany.org/imprint">Imprint</a>
<a href="https://vatsim-germany.org/gdpr">Data protection</a>
</span>
</div>
</footer>
</section>
6 changes: 1 addition & 5 deletions monitor/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
<div class="container">
<div class="row">
<h1>Can I Staff It?</h1>
<h3>Hi, {{ name }} 👋</h3>

<form class="mt-3" method="post">
<div>
<label class="form-label" for="station">Station</label>
<input class="form-control" name="station" id="station" placeholder="EDDF_S_TWR" required>
</div>

<div class="mt-3">
<label class="form-label" for="cid">VATSIM ID</label>
<input class="form-control text-uppercase" name="cid" id="cid" size="9" type="number" value="{{ request.form['cid'] }}" placeholder="100000" required>
</div>

<input class="btn btn-primary mt-4" type="submit" value="Submit">
</form>

Expand Down

0 comments on commit d88973d

Please sign in to comment.