-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
164 lines (127 loc) · 5.2 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import json
import logging
from typing import cast
import flask
from flask import send_from_directory
from werkzeug.exceptions import BadRequestKeyError
from calendar_connector.calendar_converter import CalendarConverter
from calendar_connector.consts import route_change_presence, PRESENCE
from calendar_connector.data_decoder import decode_data
from calendar_connector.sporteasy_connector import SporteasyConnector
from calendar_connector.database.user import generate_links_data
from calendar_connector.custom_exceptions import BadTokenException
from calendar_connector.presence_updater import set_presence_to_event
from calendar_connector.database.create_tables import create_db
from calendar_connector.cryptography import encrypt_message, decrypt_message
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.INFO,
)
create_db()
app = flask.Flask(__name__)
CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers": "*",
}
@app.before_request
def handle_preflight() -> flask.Response | None:
if flask.request.method == "OPTIONS":
return flask.Response("", headers=CORS_HEADERS)
return None
def _list_teams_response() -> str:
username, password, _ = decode_data()
connector = SporteasyConnector()
connector.login(username, password)
teams = connector.list_teams()
return json.dumps(teams)
def _generate_request_payload() -> str:
input_data = cast(dict[str, str], flask.request.json)
if not ({"username", "password", "team_id"} >= set(input_data.keys())):
raise Exception("Invalid keys")
data = {
"username": input_data["username"],
"password": input_data["password"],
}
if input_data.get("team_id") is not None:
data["team_id"] = input_data["team_id"]
string = json.dumps(data)
encrypted = encrypt_message(string)
return encrypted.decode("utf-8")
def _decode_encrypted_data() -> tuple[str, str, str | None]:
data_encrypted = flask.request.args["data"]
decrypted = decrypt_message(data_encrypted.encode("utf-8"))
data = json.loads(decrypted)
return data["username"], data["password"], data.get("team_id")
def request_handler() -> flask.Response:
if flask.request.args.get("encrypted", "0") == "1":
username, password, team_id = _decode_encrypted_data()
else:
username, password, team_id = decode_data()
ip = flask.request.remote_addr
logging.info(f"New incoming request from {ip=} and {username=}")
url_root = flask.request.url_root
disable_save_login = flask.request.args.get("disable_save_login") is not None
calendar_converter = CalendarConverter()
calendar_text = calendar_converter.get_calendar_text(
username, password, not disable_save_login, url_root, team_id
)
return flask.Response(
calendar_text,
headers={"Content-Type": "text/calendar; charset=utf-8"},
mimetype="text/calendar",
)
@app.route(route_change_presence)
def change_my_presence() -> flask.Response:
try:
team_id = flask.request.args["team_id"]
event_id = flask.request.args["event_id"]
user_id = flask.request.args["user_id"]
token = flask.request.args["token"]
presence = flask.request.args["presence"].lower() == PRESENCE.present
except BadRequestKeyError as e:
return flask.Response("Parameter missing", status=500)
hash_token = generate_links_data(team_id, event_id, user_id, presence)
if token != hash_token:
raise BadTokenException()
set_presence_to_event(int(team_id), int(event_id), int(user_id), presence)
return flask.send_file("calendar_connector/html/auto_close.html")
@app.get("/api/list-teams")
def list_teams() -> flask.Response:
try:
return flask.Response(_list_teams_response(), headers=CORS_HEADERS)
except Exception as e:
return flask.Response(str(e), headers=CORS_HEADERS)
@app.post("/api/generate_request_payload")
def generate_request_payload() -> flask.Response:
try:
payload = _generate_request_payload()
return flask.Response(payload, headers=CORS_HEADERS)
except Exception as e:
error = str(e)
return flask.Response(error, headers=CORS_HEADERS)
@app.get("/api")
def main_request_api_handler() -> flask.Response:
if os.environ.get("DEBUG"):
return request_handler()
try:
return request_handler()
except Exception as e:
return flask.Response(str(e))
@app.get("/")
def serve_static_index() -> flask.Response:
data = flask.request.args.get("data", None)
if data is not None:
# Redirect user to /api if data is passed to keep compatibility
redirect_route = flask.url_for("main_request_api_handler")
redirect_url = f"{redirect_route}?data={data}"
return flask.redirect(redirect_url) # type: ignore
return send_from_directory("web-app/build", "index.html")
@app.get("/<path:path>")
def serve_static_files(path: str) -> flask.Response:
return send_from_directory("web-app/build", path)
if __name__ == "__main__":
app.run(host="0.0.0.0")