-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
122 lines (94 loc) · 3.38 KB
/
views.py
File metadata and controls
122 lines (94 loc) · 3.38 KB
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
import logging
import os
from flask import abort
from flask import redirect
from flask import render_template
from flask import request
from flask import send_from_directory
from flask import url_for
from flask.views import MethodView
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
from main import app
import utils
from sdk import constants
MB_HOMEPAGE = 'https://example.com'
log = logging.getLogger(__name__)
class IndexView(MethodView):
"""
Index redirects to Mailbeaker home
"""
def get(self):
log.info('', extra = {
'server': 'link',
'view': 'index',
})
return redirect(MB_HOMEPAGE)
class LinkView(MethodView):
"""
View for redirecting a link to the original url.
"""
def get(self, link_jwt):
log.info('', extra = {
'server': 'link',
'view': 'link',
'event': 'started',
})
context = dict()
action, redirect_url = utils.get_link_info(link_jwt)
# Everything checks out. Redirect directly to the URL.
if action == constants.ACTION_PASS:
return redirect(redirect_url)
# Rule matches, requesting warning page.
elif action == constants.ACTION_WARN:
context = {
'title': "We've got a bad feeling about this.",
'redirect_url': redirect_url,
}
return render_template('warn.html', **context)
# Rule matches, requesting blocking page.
elif action == constants.ACTION_BLOCK:
url = urlparse(redirect_url)
domain = url.netloc
context = {
'title': "We're stepping in to protect you.",
'domain': domain
}
return render_template('block.html', **context)
elif action == constants.ACTION_INVALID:
# Invalid link. Might have been due to a JWT verification
# failure. We try decoding the JWT without verification
# to see if we can get the info from it.
return render_template('error.html', **context), 400
# TODO remove the above and re-enable this after adding option to verify to SDK JWT checks
"""
link_jwt = utils.decode_jwt_v1(link_jwt, verify=False)
if link_jwt:
redirect_url = link_jwt.get('u')
context = {
'title': "There was an unexpected error with that link.",
'redirect_url': redirect_url,
}
return render_template('link_error.html', **context), 400
else:
context = {
'title': "We're sorry, but something went wrong.",
}
return render_template('error.html', **context), 400
"""
return redirect(redirect_url)
class FaviconView(MethodView):
def get(self):
return send_from_directory(
os.path.join(app.root_path, 'static'),
'mailbeaker-ico.png',
mimetype='image/vnd.microsoft.icon'
)
###########
# URL Rules
###########
app.add_url_rule('/', view_func=IndexView.as_view('index'))
app.add_url_rule('/favicon.ico/', view_func=FaviconView.as_view('favicon'))
app.add_url_rule('/v1/<string:link_jwt>/', view_func=LinkView.as_view('links'))