-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_server.py
57 lines (49 loc) · 1.87 KB
/
email_server.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
from textwrap import dedent
import tornado.web
import tornado.ioloop
import base64
from config import load_config
from email_scraper import get_email_html, login
config = load_config()
class MainHandler(tornado.web.RequestHandler):
def get_current_user(self):
auth_header = self.request.headers.get("Authorization")
if auth_header:
auth_type, auth_data = auth_header.split()
if auth_type == "Basic":
username, password = base64.b64decode(auth_data).decode().split(":")
if password == config["email_password"]:
return username
return None
def get(self):
user = self.get_current_user()
if user is None:
return self.request_auth()
email_url = self.get_query_argument("email_url", default=None)
if email_url is None:
self.write("<p>No email URL specified</p>")
else:
email_html = get_email_html(login(config["email_password"]), email_url)
if email_html is not None:
framed_document = (
email_html
.replace("\n", " ") # i don't think this is technically necessary
.replace('"', '"')
)
self.write(dedent(f"""
<!DOCTYPE HTML>
<html>
<body>
<iframe sandbox style="width: 100vw; height: 100vh" srcdoc="{framed_document}"></iframe>
</body>
</html>
"""))
else:
self.redirect(email_url)
def request_auth(self):
self.set_header("WWW-Authenticate", 'Basic realm="Authentication Required"')
self.set_status(401)
self.finish()
email_view_server = tornado.web.Application([
(r"/", MainHandler),
])