forked from DestructiveVoice/DestructiveFarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
34 lines (26 loc) · 978 Bytes
/
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
from functools import wraps
from flask import request, Response
from server import reloader
def authenticate():
return Response(
'Could not verify your access level for that URL. '
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def auth_required(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
config = reloader.get_config()
if auth is None or auth.password != config['SERVER_PASSWORD']:
return authenticate()
return f(*args, **kwargs)
return decorated
def api_auth_required(f):
@wraps(f)
def decorated(*args, **kwargs):
config = reloader.get_config()
if config['ENABLE_API_AUTH']:
if request.headers.get('X-Token', '') != config['API_TOKEN']:
return Response('Provided token is invalid.', 403)
return f(*args, **kwargs)
return decorated