-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.py
30 lines (26 loc) · 1.01 KB
/
middleware.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 werkzeug.wrappers import Request, Response, ResponseStream
import os
from dotenv import load_dotenv
class middleware():
'''
Simple WSGI middleware
'''
def __init__(self, app):
self.app = app
self.username = os.getenv("auth_username")
self.password = os.getenv("auth_password")
def __call__(self, environ, start_response):
request = Request(environ)
try:
userName = request.authorization['username']
password = request.authorization['password']
except:
userName = ""
password = ""
# these are hardcoded for demonstration
# verify the username and password from some database or env config variable
if userName == self.username and password == self.password:
environ['user'] = {'name': 'Tony'}
return self.app(environ, start_response)
res = Response(u'Authorization failed', mimetype='text/plain', status=401)
return res(environ, start_response)