-
Notifications
You must be signed in to change notification settings - Fork 17
/
auth.py
30 lines (22 loc) · 801 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
from functools import wraps
import authdigest
import flask
class FlaskRealmDigestDB(authdigest.RealmDigestDB):
def requires_auth(self, f):
@wraps(f)
def decorated(*args, **kwargs):
request = flask.request
if not self.isAuthenticated(request):
return self.challenge()
return f(*args, **kwargs)
return decorated
class AuthMiddleware(object):
def __init__(self, app, authDB):
self.app = app
self.authDB = authDB
def __call__(self, environ, start_response):
req = flask.Request(environ)
if not self.authDB.isAuthenticated(req):
response = self.authDB.challenge()
return response(environ, start_response)
return self.app(environ, start_response)