/examples/web/authdemo.py

https://bitbucket.org/prologic/circuits/ · Python · 39 lines · 19 code · 13 blank · 7 comment · 1 complexity · 0e3230e378fa0b7adfc8209d4c823014 MD5 · raw file

  1. #!/usr/bin/env python
  2. from hashlib import md5
  3. from circuits import handler, Component
  4. from circuits.web import Server, Controller
  5. from circuits.web.tools import check_auth, basic_auth
  6. class Auth(Component):
  7. realm = "Test"
  8. users = {"admin": md5("admin").hexdigest()}
  9. @handler("request", priority=1.0)
  10. def on_request(self, event, request, response):
  11. """Filter Requests applying Basic Authentication
  12. Filter any incoming requests at a higher priority than the
  13. default dispatcher and apply Basic Authentication returning
  14. a 403 Forbidden response if Authentication failed.
  15. """
  16. if not check_auth(request, response, self.realm, self.users):
  17. event.stop()
  18. return basic_auth(request, response, self.realm, self.users)
  19. class Root(Controller):
  20. def index(self):
  21. return "Hello World!"
  22. app = Server(("0.0.0.0", 8000))
  23. Auth().register(app)
  24. Root().register(app)
  25. app.run()