-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.py
55 lines (45 loc) · 1.61 KB
/
auth.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
from functools import update_wrapper
from flask import request, jsonify
from conf import *
"""
Simple method to check API credentials
"""
def check_auth(username, password):
for user, pw in API_ACCESS:
if username == user and password == pw:
return True
return False
"""
Basic authentication for protected resources.
Source: "HTTP Basic Auth" by Armin Ronacher
http://flask.pocoo.org/snippets/8/
"""
def authenticate(msg='Please authenticate.'):
resp = jsonify({'success': False, 'status': msg})
resp.status_code = 401
resp.headers['WWW-Authenticate'] = 'Basic realm="API credentials needed to access this resource."'
return resp
"""
Basic authentication for protected resources and SSL enforcement.
Source: "HTTP Basic Auth" by Armin Ronacher
http://flask.pocoo.org/snippets/8/
Changed to require ssl for authentication. Extra argument added to require auth only for POST requests.
"""
def requires_auth(postAuthOnly=False):
def decorator(f):
def wrapped_function(*args, **kwargs):
# require ssl for api requests with auth
if "https://" not in request.url and not (postAuthOnly and request.method != 'POST'):
message = { 'success': False, 'status': 'Resources requiring authentication also require ssl.'}
resp = jsonify(message)
resp.status_code = 403
return resp
if not (request.method != 'POST' and postAuthOnly):
auth = request.authorization
if not auth:
return authenticate()
elif not check_auth(auth.username, auth.password):
return authenticate("Authentication Failed.")
return f(*args, **kwargs)
return update_wrapper(wrapped_function, f)
return decorator