PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/hgwebproxy/utils.py

https://bitbucket.org/mariocesar/django-hgwebproxy/
Python | 50 lines | 40 code | 1 blank | 9 comment | 0 complexity | f3749359ed0ecd1b0f85d10f5c6a3125 MD5 | raw file
  1. import re
  2. from django.contrib.auth import authenticate
  3. def is_mercurial(request):
  4. """
  5. User agent processor to determine whether the incoming
  6. user is someone using a browser, or a mercurial client
  7. In order to qualify as a mercurial user they must have a user
  8. agent value that starts with mercurial and an Accept header that
  9. starts with application/mercurial. This guarantees we only force
  10. those who are actual mercurial users to use Basic Authentication
  11. """
  12. agent = re.compile(r'^(mercurial).*')
  13. accept = request.META.get('HTTP_ACCEPT', None)
  14. result = agent.match(request.META.get('HTTP_USER_AGENT', ""))
  15. if result and accept.startswith('application/mercurial-'):
  16. return True
  17. else:
  18. return False
  19. def basic_auth(request, realm, repo):
  20. """
  21. Very simple Basic authentication handler
  22. """
  23. if request.user.is_authenticated():
  24. user = request.user
  25. username = request.user.username
  26. else:
  27. auth_string = request.META.get('HTTP_AUTHORIZATION')
  28. if auth_string is None or not auth_string.startswith("Basic"):
  29. return False
  30. _, basic_hash = auth_string.split(' ', 1)
  31. username, password = basic_hash.decode('base64').split(':', 1)
  32. user = authenticate(username=username, password=password)
  33. if user:
  34. if request.method == "POST":
  35. if repo.can_push(user):
  36. return username
  37. else:
  38. if repo.can_pull(user):
  39. return username
  40. return False