-
Notifications
You must be signed in to change notification settings - Fork 11
/
basicauth_app.py
48 lines (38 loc) · 1.72 KB
/
basicauth_app.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
import os
from werkzeug.serving import run_simple
from werkzeug.wrappers import Request, Response
import app
from config import config
# https://github.com/mitsuhiko/werkzeug/blob/master/examples/httpbasicauth.py
class Application(object):
def __init__(self, users, realm='login required'):
self.users = users
self.realm = realm
def check_auth(self, username, password):
return username in self.users and self.users[username] == password
def auth_required(self, request):
return Response('Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="%s"' % self.realm})
def dispatch_request(self, request):
return Response('Logged in as %s' % request.authorization.username)
def __call__(self, environ, start_response):
request = Request(environ)
auth = request.authorization
if not auth or not self.check_auth(auth.username, auth.password):
response = self.auth_required(request)
else:
response = app.app
return response(environ, start_response)
if __name__ == '__main__':
BIND_ADDRESS = os.environ.get('BIND_ADDRESS', 'localhost')
PORT = int(os.environ.get('PORT', '5000'))
USER, PASS = os.environ.get('USERPASS', ':').split(':')
if not USER or not PASS:
raise Exception('Invalid setting for USERPASS.')
app.create_dbs()
config_name = os.environ.get('FLASK_CONFIG', 'default')
app.app.config.from_object(config[config_name])
application = Application({USER: PASS})
run_simple(BIND_ADDRESS, PORT, application, use_debugger=True,
use_evalex=False)