1
# -*- test-case-name: twisted.web.test.test_httpauth -*-
2
# Copyright (c) 2008 Twisted Matrix Laboratories.
3
# See LICENSE for details.
6
HTTP BASIC authentication.
8
@see: U{http://tools.ietf.org/html/rfc1945}
9
@see: U{http://tools.ietf.org/html/rfc2616}
10
@see: U{http://tools.ietf.org/html/rfc2617}
15
from zope.interface import implements
17
from twisted.cred import credentials, error
18
from twisted.web.iweb import ICredentialFactory
21
class BasicCredentialFactory(object):
23
Credential Factory for HTTP Basic Authentication
25
@type authenticationRealm: C{str}
26
@ivar authenticationRealm: The HTTP authentication realm which will be issued in
29
implements(ICredentialFactory)
33
def __init__(self, authenticationRealm):
34
self.authenticationRealm = authenticationRealm
37
def getChallenge(self, request):
39
Return a challenge including the HTTP authentication realm with which
40
this factory was created.
42
return {'realm': self.authenticationRealm}
45
def decode(self, response, request):
47
Parse the base64-encoded, colon-separated username and password into a
48
L{credentials.UsernamePassword} instance.
51
creds = binascii.a2b_base64(response + '===')
52
except binascii.Error:
53
raise error.LoginFailed('Invalid credentials')
55
creds = creds.split(':', 1)
57
return credentials.UsernamePassword(*creds)
59
raise error.LoginFailed('Invalid credentials')