-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
31 lines (23 loc) · 967 Bytes
/
auth.py
File metadata and controls
31 lines (23 loc) · 967 Bytes
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
from functools import wraps
from flask import request, Response, current_app
def check_auth(username, password):
"""
This function is called to check if a username / password combination
is valid.
"""
check_username = username == current_app.config['AUTH_USERNAME']
check_password = password == current_app.config['AUTH_PASSWORD']
return check_username and check_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