forked from canonical/ubuntu.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.py
347 lines (275 loc) · 10.7 KB
/
decorators.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import os
from distutils.util import strtobool
from functools import wraps
from datetime import datetime
from dateutil.parser import parse
import flask
import pytz
import talisker.requests
from webapp.shop.api.ua_contracts.api import UAContractsAPI
from webapp.shop.api.ua_contracts.advantage_mapper import AdvantageMapper
from webapp.shop.api.badgr.api import BadgrAPI
from webapp.shop.api.credly.api import CredlyAPI
from webapp.shop.api.trueability.api import TrueAbilityAPI
from webapp.login import user_info
from requests import Session
AREA_LIST = {
"account": "Account pages",
"advantage": "UA pages",
"cred": "Credentials",
}
PERMISSION_LIST = {
"user": "Endpoint needs logged in user.",
}
RESPONSE_LIST = {
"html": "Returns user friendly HTML response.",
"json": "Returns json response.",
}
MARKETING_FLAGS = {
"utm_campaign": "salesforce-campaign-id",
"utm_source": "ad_source",
"gclid": "google-click-id",
"gbraid": "google-gbraid-id",
"wbraid": "google-wbraid-id",
"fbclid": "facebook-click-id",
"referrer": "referrer",
}
SERVICES = {
"canonical-ua": {
"short": "ua",
"name": "Canonical UA",
},
"blender": {
"short": "blender",
"name": "Blender Support",
},
"canonical-cube": {
"short": "cube",
"name": "Canonical CUBE",
},
}
MAINTENANCE_URLS = [
"/pro/subscribe",
"/pro/maintenance-check",
"/credentials/shop",
]
def shop_decorator(area=None, permission=None, response="json", redirect=None):
permission = permission if permission in PERMISSION_LIST else None
response = response if response in RESPONSE_LIST else "json"
area = area if area in AREA_LIST else "account"
session = talisker.requests.get_session()
badgr_session = init_badgr_session(area)
trueability_session = init_trueability_session(area)
credly_session = init_credly_session(area)
def decorator(func):
@wraps(func)
def decorated_function(*args, **kwargs):
# Set marketing flag
for query_parameter, metadata_key in MARKETING_FLAGS.items():
if query_parameter in flask.request.args:
flask.session.pop(metadata_key, None)
value = flask.request.args.get(query_parameter)
flask.session[metadata_key] = value
# shop under maintenance
maintenance = strtobool(os.getenv("STORE_MAINTENANCE", "false"))
cred_maintenance = strtobool(
os.getenv("CRED_MAINTENANCE", "False")
)
is_store_maintenance_in_timeframe = False
is_cred_maintenance_in_timeframe = False
store_maintenance_start = os.getenv("STORE_MAINTENANCE_START")
store_maintenance_end = os.getenv("STORE_MAINTENANCE_END")
cred_maintenance_start = os.getenv("CRED_MAINTENANCE_START")
cred_maintenance_end = os.getenv("CRED_MAINTENANCE_END")
if store_maintenance_start and store_maintenance_end:
maintenance_start = parse(os.getenv("STORE_MAINTENANCE_START"))
maintenance_end = parse(os.getenv("STORE_MAINTENANCE_END"))
time_now = datetime.utcnow().replace(tzinfo=pytz.utc)
is_store_maintenance_in_timeframe = (
maintenance_start <= time_now < maintenance_end
)
if cred_maintenance_start and cred_maintenance_end:
_maintenance_start = parse(os.getenv("CRED_MAINTENANCE_START"))
_maintenance_end = parse(os.getenv("CRED_MAINTENANCE_END"))
_time_now = datetime.now(pytz.utc)
is_cred_maintenance_in_timeframe = (
_maintenance_start <= _time_now <= _maintenance_end
)
if _time_now > _maintenance_end:
cred_maintenance = False
is_in_maintenance = (
maintenance and is_store_maintenance_in_timeframe
)
cred_is_in_maintenance = (
cred_maintenance and is_cred_maintenance_in_timeframe
)
if flask.request.path in MAINTENANCE_URLS and is_in_maintenance:
return flask.render_template("advantage/maintenance.html")
if (
flask.request.path in MAINTENANCE_URLS
and cred_is_in_maintenance
):
return flask.render_template(
"advantage/maintenance.html",
description="We're updating the Credentials store",
title="Credentials Maintenance",
)
user_token = flask.session.get("authentication_token")
if permission == "user" and response == "json":
if not user_token:
message = {"error": "authentication required"}
return flask.jsonify(message), 401
if permission == "user" and response == "html":
if not user_token:
redirect_path = redirect or flask.request.full_path
return flask.redirect(f"/login?next={redirect_path}")
ua_contracts_api = get_ua_contracts_api_instance(
user_token, response, session, flask.request.remote_addr
)
advantage_mapper = AdvantageMapper(ua_contracts_api)
is_community_member = False
is_cred_admin = False
if user_info(flask.session):
is_community_member = user_info(flask.session).get(
"is_community_member", False
)
is_cred_admin = user_info(flask.session).get(
"is_credentials_admin", False
)
return func(
badgr_issuer=os.getenv(
"BADGR_ISSUER", "eTedPNzMTuqy1SMWJ05UbA"
),
badge_certification=os.getenv(
"CERTIFIED_BADGE", "hs8gVorCRgyO2mNUfeXaLw"
),
ua_contracts_api=ua_contracts_api,
advantage_mapper=advantage_mapper,
badgr_api=get_badgr_api_instance(area, badgr_session),
trueability_api=get_trueability_api_instance(
area, trueability_session
),
credly_api=get_credly_api_instance(area, credly_session),
is_in_maintenance=is_in_maintenance,
is_community_member=is_community_member,
show_cred_maintenance_alert=bool(cred_maintenance),
cred_is_in_maintenance=cred_is_in_maintenance,
is_cred_admin=is_cred_admin,
cred_maintenance_start=cred_maintenance_start,
cred_maintenance_end=cred_maintenance_end,
*args,
**kwargs,
)
return decorated_function
return decorator
def canonical_staff():
def decorator(func):
@wraps(func)
def decorated_function(*args, **kwargs):
sso_user = user_info(flask.session)
if sso_user and sso_user.get("email", "").endswith(
"@canonical.com"
):
return func(*args, **kwargs)
message = {"error": "unauthorized"}
return flask.jsonify(message), 403
return decorated_function
return decorator
def credentials_group():
def decorator(func):
@wraps(func)
def decorated_function(*args, **kwargs):
sso_user = user_info(flask.session)
if sso_user and (
(sso_user.get("is_credentials_admin", False) is True)
or (sso_user.get("is_credentials_support", False) is True)
):
return func(*args, **kwargs)
return flask.render_template(
"account/forbidden.html", reason="is_not_admin"
)
return decorated_function
return decorator
def credentials_admin():
def decorator(func):
@wraps(func)
def decorated_function(*args, **kwargs):
sso_user = user_info(flask.session)
if (
sso_user
and sso_user.get("is_credentials_admin", False) is True
):
return func(*args, **kwargs)
return flask.render_template(
"account/forbidden.html", reason="is_not_admin"
)
return decorated_function
return decorator
def init_badgr_session(area) -> Session:
if area != "cred":
return None
badgr_session = Session()
talisker.requests.configure(badgr_session)
return badgr_session
def init_credly_session(area) -> Session:
if area != "cred":
return None
credly_session = Session()
talisker.requests.configure(credly_session)
return credly_session
def init_trueability_session(area) -> Session:
if area != "cred":
return None
trueability_session = Session()
talisker.requests.configure(trueability_session)
return trueability_session
def get_redirect_default(area) -> str:
redirect_path = "/account"
if area == "advantage":
redirect_path = "/pro/dashboard"
elif area == "cred":
redirect_path = "/credentials"
return redirect_path
def get_badgr_api_instance(area, badgr_session) -> BadgrAPI:
if area != "cred":
return None
return BadgrAPI(
os.getenv("BADGR_URL", "https://api.eu.badgr.io"),
os.getenv("BAGDR_USER"),
os.getenv("BADGR_PASSWORD"),
badgr_session,
)
def get_credly_api_instance(area, credly_session) -> CredlyAPI:
if area != "cred":
return None
return CredlyAPI(
base_url=os.getenv("CREDLY_URL", "https://sandbox-api.credly.com/v1"),
auth_token=os.getenv("CREDLY_TOKEN", ""),
org_id=os.getenv(
"CREDLY_ORGANIZATION_ID", "069adc37-b51e-45ee-8c9d-4a2c89ce6622"
),
session=credly_session,
)
def get_trueability_api_instance(area, trueability_session) -> TrueAbilityAPI:
if area != "cred":
return None
return TrueAbilityAPI(
os.getenv("TRUEABILITY_URL", "https://app3.trueability.com"),
os.getenv("TRUEABILITY_API_KEY", ""),
trueability_session,
)
def get_ua_contracts_api_instance(
user_token, response, session, remote_addr
) -> UAContractsAPI:
ua_contracts_api = UAContractsAPI(
session=session,
authentication_token=user_token,
token_type="Macaroon",
api_url=os.getenv(
"CONTRACTS_API_URL", "https://contracts.canonical.com"
),
remote_addr=remote_addr,
)
if response == "html":
ua_contracts_api.set_is_for_view(True)
return ua_contracts_api