forked from fossasia/open-event-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
467 lines (405 loc) · 15.3 KB
/
auth.py
File metadata and controls
467 lines (405 loc) · 15.3 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import base64
import logging
import random
import string
from datetime import timedelta
from functools import wraps
import requests
from flask import Blueprint, jsonify, make_response, request, send_file
from flask_jwt_extended import (
create_access_token,
create_refresh_token,
current_user,
fresh_jwt_required,
get_jwt_identity,
jwt_refresh_token_required,
jwt_required,
set_refresh_cookies,
unset_jwt_cookies,
)
from healthcheck import EnvironmentDump
from sqlalchemy.orm.exc import NoResultFound
from app.api.helpers.auth import AuthManager, blacklist_token
from app.api.helpers.db import get_count, save_to_db
from app.api.helpers.errors import (
BadRequestError,
NotFoundError,
UnprocessableEntityError,
)
from app.api.helpers.files import make_frontend_url
from app.api.helpers.jwt import jwt_authenticate
from app.api.helpers.mail import send_email_confirmation, send_email_with_action
from app.api.helpers.notification import send_notification_with_action
from app.api.helpers.third_party_auth import (
FbOAuth,
GoogleOAuth,
InstagramOAuth,
TwitterOAuth,
)
from app.api.helpers.utilities import get_serializer, str_generator
from app.extensions.limiter import limiter
from app.models import db
from app.models.mail import PASSWORD_CHANGE, PASSWORD_RESET, PASSWORD_RESET_AND_VERIFY
from app.models.notification import PASSWORD_CHANGE as PASSWORD_CHANGE_NOTIF
from app.models.user import User
from app.settings import get_settings
logger = logging.getLogger(__name__)
authorised_blueprint = Blueprint('authorised_blueprint', __name__, url_prefix='/')
auth_routes = Blueprint('auth', __name__, url_prefix='/v1/auth')
def authenticate(allow_refresh_token=False, existing_identity=None):
data = request.get_json()
username = data.get('email', data.get('username'))
password = data.get('password')
criterion = [username, password]
if not all(criterion):
return jsonify(error='username or password missing'), 400
identity = jwt_authenticate(username, password)
if not identity or (existing_identity and identity != existing_identity):
# For fresh login, credentials should match existing user
return jsonify(error='Invalid Credentials'), 401
remember_me = data.get('remember-me')
include_in_response = data.get('include-in-response')
add_refresh_token = allow_refresh_token and remember_me
expiry_time = timedelta(minutes=90) if add_refresh_token else None
access_token = create_access_token(identity.id, fresh=True, expires_delta=expiry_time)
response_data = {'access_token': access_token}
if add_refresh_token:
refresh_token = create_refresh_token(identity.id)
if include_in_response:
response_data['refresh_token'] = refresh_token
response = jsonify(response_data)
if add_refresh_token and not include_in_response:
set_refresh_cookies(response, refresh_token)
return response
@authorised_blueprint.route('/auth/session', methods=['POST'])
@auth_routes.route('/login', methods=['POST'])
def login():
return authenticate(allow_refresh_token=True)
@auth_routes.route('/fresh-login', methods=['POST'])
@jwt_required
def fresh_login():
return authenticate(existing_identity=current_user)
@auth_routes.route('/token/refresh', methods=['POST'])
@jwt_refresh_token_required
def refresh_token():
current_user = get_jwt_identity()
new_token = create_access_token(identity=current_user, fresh=False)
return jsonify({'access_token': new_token})
@auth_routes.route('/logout', methods=['POST'])
def logout():
response = jsonify({'success': True})
unset_jwt_cookies(response)
return response
@auth_routes.route('/blacklist', methods=['POST'])
@jwt_required
def blacklist_token_rquest():
blacklist_token(current_user)
return jsonify({'success': True})
@auth_routes.route('/oauth/<provider>', methods=['GET'])
def redirect_uri(provider):
if provider == 'facebook':
provider_class = FbOAuth()
elif provider == 'google':
provider_class = GoogleOAuth()
elif provider == 'twitter':
provider_class = TwitterOAuth()
elif provider == 'instagram':
provider_class = InstagramOAuth()
else:
return make_response(jsonify(message=f"No support for {provider}"), 404)
client_id = provider_class.get_client_id()
if not client_id:
return make_response(
jsonify(message=f"{provider} client id is not configured on the server"),
404,
)
url = (
provider_class.get_auth_uri()
+ '?client_id='
+ client_id
+ '&redirect_uri='
+ provider_class.get_redirect_uri()
)
return make_response(jsonify(url=url), 200)
@auth_routes.route('/oauth/token/<provider>', methods=['GET'])
def get_token(provider):
if provider == 'facebook':
provider_class = FbOAuth()
payload = {
'grant_type': 'client_credentials',
'client_id': provider_class.get_client_id(),
'client_secret': provider_class.get_client_secret(),
}
elif provider == 'google':
provider_class = GoogleOAuth()
payload = {
'client_id': provider_class.get_client_id(),
'client_secret': provider_class.get_client_secret(),
}
elif provider == 'twitter':
provider_class = TwitterOAuth()
payload = {
'client_id': provider_class.get_client_id(),
'client_secret': provider_class.get_client_secret(),
}
elif provider == 'instagram':
provider_class = InstagramOAuth()
payload = {
'client_id': provider_class.get_client_id(),
'client_secret': provider_class.get_client_secret(),
}
else:
return make_response(jsonify(message=f"No support for {provider}"), 200)
response = requests.post(provider_class.get_token_uri(), params=payload)
return make_response(jsonify(token=response.json()), 200)
@auth_routes.route('/oauth/login/<provider>', methods=['POST'])
def login_user(provider):
if provider == 'facebook':
provider_class = FbOAuth()
payload = {
'client_id': provider_class.get_client_id(),
'redirect_uri': provider_class.get_redirect_uri(),
'client_secret': provider_class.get_client_secret(),
'code': request.args.get('code'),
}
if not payload['client_id'] or not payload['client_secret']:
raise NotImplementedError({'source': ''}, 'Facebook Login Not Configured')
access_token = requests.get(
'https://graph.facebook.com/v3.0/oauth/access_token', params=payload
).json()
payload_details = {
'input_token': access_token['access_token'],
'access_token': provider_class.get_client_id()
+ '|'
+ provider_class.get_client_secret(),
}
details = requests.get(
'https://graph.facebook.com/debug_token', params=payload_details
).json()
user_details = requests.get(
'https://graph.facebook.com/v3.0/' + details['data']['user_id'],
params={
'access_token': access_token['access_token'],
'fields': 'first_name, last_name, email',
},
).json()
if get_count(db.session.query(User).filter_by(email=user_details['email'])) > 0:
user = db.session.query(User).filter_by(email=user_details['email']).one()
if not user.facebook_id:
user.facebook_id = user_details['id']
user.facebook_login_hash = random.getrandbits(128)
save_to_db(user)
return make_response(
jsonify(
user_id=user.id, email=user.email, oauth_hash=user.facebook_login_hash
),
200,
)
user = User()
user.first_name = user_details['first_name']
user.last_name = user_details['last_name']
user.facebook_id = user_details['id']
user.facebook_login_hash = random.getrandbits(128)
user.password = ''.join(
random.SystemRandom().choice(string.ascii_uppercase + string.digits)
for _ in range(8)
)
if user_details['email']:
user.email = user_details['email']
save_to_db(user)
return make_response(
jsonify(
user_id=user.id, email=user.email, oauth_hash=user.facebook_login_hash
),
200,
)
if provider == 'google':
provider_class = GoogleOAuth()
payload = {
'client_id': provider_class.get_client_id(),
'client_secret': provider_class.get_client_secret(),
}
elif provider == 'twitter':
provider_class = TwitterOAuth()
payload = {
'client_id': provider_class.get_client_id(),
'client_secret': provider_class.get_client_secret(),
}
elif provider == 'instagram':
provider_class = InstagramOAuth()
payload = {
'client_id': provider_class.get_client_id(),
'client_secret': provider_class.get_client_secret(),
}
else:
return make_response(jsonify(message=f"No support for {provider}"), 200)
response = requests.post(provider_class.get_token_uri(), params=payload)
return make_response(jsonify(token=response.json()), 200)
@auth_routes.route('/verify-email', methods=['POST'])
def verify_email():
try:
token = base64.b64decode(request.json['data']['token'])
except base64.binascii.Error:
raise BadRequestError({'source': ''}, 'Invalid Token')
s = get_serializer()
try:
data = s.loads(token)
except Exception:
raise BadRequestError({'source': ''}, 'Invalid Token')
try:
user = User.query.filter_by(email=data[0]).one()
except Exception:
raise BadRequestError({'source': ''}, 'Invalid Token')
else:
user.is_verified = True
save_to_db(user)
return make_response(jsonify(message="Email Verified"), 200)
@auth_routes.route('/resend-verification-email', methods=['POST'])
def resend_verification_email():
try:
email = request.json['data']['email']
except TypeError:
raise BadRequestError({'source': ''}, 'Bad Request Error')
try:
user = User.query.filter_by(email=email).one()
except NoResultFound:
raise UnprocessableEntityError(
{'source': ''}, 'User with email: ' + email + ' not found.'
)
else:
serializer = get_serializer()
hash_ = str(
base64.b64encode(
str(serializer.dumps([user.email, str_generator()])).encode()
),
'utf-8',
)
link = make_frontend_url('/verify', {'token': hash_})
send_email_confirmation(user.email, link)
return make_response(jsonify(message="Verification email resent"), 200)
@auth_routes.route('/reset-password', methods=['POST'])
@limiter.limit(
'3/hour',
key_func=lambda: request.json['data']['email'],
error_message='Limit for this action exceeded',
)
@limiter.limit('1/minute', error_message='Limit for this action exceeded')
def reset_password_post():
try:
email = request.json['data']['email']
except TypeError:
raise BadRequestError({'source': ''}, 'Bad Request Error')
try:
user = User.query.filter_by(email=email).one()
except NoResultFound:
logger.info('Tried to reset password not existing email %s', email)
else:
link = make_frontend_url('/reset-password', {'token': user.reset_password})
if user.was_registered_with_order:
send_email_with_action(
user,
PASSWORD_RESET_AND_VERIFY,
app_name=get_settings()['app_name'],
link=link,
)
else:
send_email_with_action(
user,
PASSWORD_RESET,
app_name=get_settings()['app_name'],
link=link,
token=user.reset_password,
)
return make_response(
jsonify(
message="If your email was registered with us, you'll get an \
email with reset link shortly",
email=email,
),
200,
)
@auth_routes.route('/reset-password', methods=['PATCH'])
def reset_password_patch():
token = request.json['data']['token']
password = request.json['data']['password']
try:
user = User.query.filter_by(reset_password=token).one()
except NoResultFound:
raise NotFoundError({'source': ''}, 'User Not Found')
else:
user.password = password
if not user.is_verified:
user.is_verified = True
save_to_db(user)
return jsonify(
{
"id": user.id,
"email": user.email,
"name": user.fullname if user.fullname else None,
}
)
@auth_routes.route('/change-password', methods=['POST'])
@fresh_jwt_required
def change_password():
old_password = request.json['data']['old-password']
new_password = request.json['data']['new-password']
try:
user = User.query.filter_by(id=current_user.id).one()
except NoResultFound:
raise NotFoundError({'source': ''}, 'User Not Found')
else:
if user.is_correct_password(old_password):
if user.is_correct_password(new_password):
raise BadRequestError(
{'source': ''}, 'Old and New passwords must be different'
)
if len(new_password) < 8:
raise BadRequestError(
{'source': ''}, 'Password should have minimum 8 characters'
)
user.password = new_password
save_to_db(user)
send_email_with_action(
user, PASSWORD_CHANGE, app_name=get_settings()['app_name']
)
send_notification_with_action(
user, PASSWORD_CHANGE_NOTIF, app_name=get_settings()['app_name']
)
else:
raise BadRequestError(
{'source': ''}, 'Wrong Password. Please enter correct current password.'
)
return jsonify(
{
"id": user.id,
"email": user.email,
"name": user.fullname if user.fullname else None,
"password-changed": True,
}
)
def return_file(file_name_prefix, file_path, identifier):
response = make_response(send_file(file_path))
response.headers['Content-Disposition'] = 'attachment; filename={}-{}.pdf'.format(
file_name_prefix,
identifier,
)
return response
# Access for Environment details & Basic Auth Support
def requires_basic_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not AuthManager.check_auth_admin(auth.username, auth.password):
return make_response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials',
401,
{'WWW-Authenticate': 'Basic realm="Login Required"'},
)
return f(*args, **kwargs)
return decorated
@authorised_blueprint.route('/environment')
@requires_basic_auth
def environment_details():
envdump = EnvironmentDump(include_config=False)
return envdump.dump_environment()