digest.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- test-case-name: twisted.web.test.test_httpauth -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Implementation of RFC2617: HTTP Digest Authentication
  6. @see: U{http://www.faqs.org/rfcs/rfc2617.html}
  7. """
  8. from __future__ import division, absolute_import
  9. from zope.interface import implementer
  10. from twisted.cred import credentials
  11. from twisted.web.iweb import ICredentialFactory
  12. @implementer(ICredentialFactory)
  13. class DigestCredentialFactory(object):
  14. """
  15. Wrapper for L{digest.DigestCredentialFactory} that implements the
  16. L{ICredentialFactory} interface.
  17. """
  18. scheme = b'digest'
  19. def __init__(self, algorithm, authenticationRealm):
  20. """
  21. Create the digest credential factory that this object wraps.
  22. """
  23. self.digest = credentials.DigestCredentialFactory(algorithm,
  24. authenticationRealm)
  25. def getChallenge(self, request):
  26. """
  27. Generate the challenge for use in the WWW-Authenticate header
  28. @param request: The L{IRequest} to with access was denied and for the
  29. response to which this challenge is being generated.
  30. @return: The L{dict} that can be used to generate a WWW-Authenticate
  31. header.
  32. """
  33. return self.digest.getChallenge(request.getClientAddress().host)
  34. def decode(self, response, request):
  35. """
  36. Create a L{twisted.cred.credentials.DigestedCredentials} object
  37. from the given response and request.
  38. @see: L{ICredentialFactory.decode}
  39. """
  40. return self.digest.decode(response,
  41. request.method,
  42. request.getClientAddress().host)