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