-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
75 lines (59 loc) · 2.18 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import random
import string
import os
from flask import Flask, redirect, url_for, abort, render_template, session
from flask_dance.contrib.google import make_google_blueprint, google
from oauthlib.oauth2.rfc6749.errors import TokenExpiredError
from werkzeug.middleware.proxy_fix import ProxyFix
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk import last_event_id
sentry_dsn = os.environ["SENTRY_DSN"]
sentry_sdk.init(
dsn=sentry_dsn,
integrations=[FlaskIntegration()]
)
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
app.secret_key = os.environ["FLASK_SECRET_KEY"]
app.config["GOOGLE_OAUTH_CLIENT_ID"] = os.environ["GOOGLE_CLIENT_ID"]
app.config["GOOGLE_OAUTH_CLIENT_SECRET"] = os.environ["GOOGLE_CLIENT_SECRET"]
google_bp = make_google_blueprint(scope=["profile", "email"])
app.register_blueprint(google_bp, url_prefix="/login")
TOKENS = {}
@app.route("/")
def index():
if "next_url" in session and google.authorized:
url = session.pop("next_url")
return redirect(url)
return redirect("https://discord.gg/FGbgQ4a", 301)
@app.route("/auth")
def auth():
if not google.authorized:
session["next_url"] = "/auth"
return redirect(url_for("google.login"))
try:
resp = google.get("/oauth2/v1/userinfo")
email = resp.json()["email"]
if not email.endswith("@mun.ca"):
del google_bp.token
return render_template("not_mun.html")
else:
prefix = email.split("@")[0]
if prefix not in TOKENS:
TOKENS[prefix] = "".join([random.choice(string.ascii_letters) for _ in range(20)])
return render_template("verified.html", token=TOKENS[prefix])
except TokenExpiredError:
return redirect(url_for("google.login"))
@app.route("/identity/<token>")
def identity(token):
for ident, t in TOKENS.items():
if t == token:
return ident
abort(404)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
return render_template("500.html", sentry_event_id=last_event_id(), sentry_dsn=sentry_dsn), 500