Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. from werkzeug.wrappers import Request, Response, ResponseStream
  2.  
  3. class middleware():
  4. '''
  5. Simple WSGI middleware
  6. '''
  7.  
  8. def __init__(self, app):
  9. self.app = app
  10. self.userName = 'Tony'
  11. self.password = 'IamIronMan'
  12.  
  13. def __call__(self, environ, start_response):
  14. request = Request(environ)
  15. userName = request.authorization['username']
  16. password = request.authorization['password']
  17.  
  18. # these are hardcoded for demonstration
  19. # verify the username and password from some database or env config variable
  20. if userName == self.userName and password == self.password:
  21. environ['user'] = { 'name': 'Tony' }
  22. return self.app(environ, start_response)
  23.  
  24. res = Response(u'Authorization failed', mimetype= 'text/plain', status=401)
  25. return res(environ, start_response)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement