-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.py
More file actions
36 lines (24 loc) · 709 Bytes
/
auth.py
File metadata and controls
36 lines (24 loc) · 709 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
32
33
34
35
import flask
import functools
import db
def authenticate(message = 'Login required'):
return flask.Response(message, 401,
{'WWW-Authenticate': 'Basic realm="SECCONFDB"'})
credentials = {
'database': 'secconfdb',
'username': 'secconfdb',
'password': '',
}
def requires_auth(f):
""" Decorator which says "we need real credentials for this operation". """
@functools.wraps(f)
def decorated(*args, **kwargs):
global credentials
auth = flask.request.authorization
if not auth: return authenticate()
credentials.update(auth)
try: db.cursor(**credentials)
except db.UnauthorizedAccessException, message:
return authenticate(message)
return f(*args, **kwargs)
return decorated