-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
50 lines (41 loc) · 1.35 KB
/
utils.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
import os
import os.path
from functools import wraps
from flask import request, Response
def read_env(fn='.env', dir=os.path.dirname(os.path.abspath(__file__))):
"""Read env file into environment, if found
"""
envpath = os.path.join(dir, fn)
env = {}
if os.path.exists(envpath):
with open(envpath, "r") as fh:
for line in fh.readlines():
if '#' not in line and '=' in line:
key, val = line.strip().split('=', 1)
env[key] = val.strip('"')
os.environ[key] = val
return env
"""HTTP Basic Auth
from http://flask.pocoo.org/snippets/8/
Not used
"""
def check_auth(username, password):
"""This function is called to check if the login info is valid.
"""
return password == 'password'
def authenticate():
"""Sends a 401 response that enables basic auth"""
return 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"'}
)
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated