forked from Str4ngeb0yz/DestructiveFarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
38 lines (27 loc) · 1.01 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
from functools import wraps
from flask import request, Response
from server import reloader
def unauthorized_response():
return Response(
response='Could not verify your access level for that URL. '
'You have to login with proper credentials',
status=401,
headers={'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 unauthorized_response()
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