~cbehrens/nova/lp844160-build-works-with-zones

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/web/_auth/basic.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- test-case-name: twisted.web.test.test_httpauth -*-
 
2
# Copyright (c) 2008 Twisted Matrix Laboratories.
 
3
# See LICENSE for details.
 
4
 
 
5
"""
 
6
HTTP BASIC authentication.
 
7
 
 
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}
 
11
"""
 
12
 
 
13
import binascii
 
14
 
 
15
from zope.interface import implements
 
16
 
 
17
from twisted.cred import credentials, error
 
18
from twisted.web.iweb import ICredentialFactory
 
19
 
 
20
 
 
21
class BasicCredentialFactory(object):
 
22
    """
 
23
    Credential Factory for HTTP Basic Authentication
 
24
 
 
25
    @type authenticationRealm: C{str}
 
26
    @ivar authenticationRealm: The HTTP authentication realm which will be issued in
 
27
        challenges.
 
28
    """
 
29
    implements(ICredentialFactory)
 
30
 
 
31
    scheme = 'basic'
 
32
 
 
33
    def __init__(self, authenticationRealm):
 
34
        self.authenticationRealm = authenticationRealm
 
35
 
 
36
 
 
37
    def getChallenge(self, request):
 
38
        """
 
39
        Return a challenge including the HTTP authentication realm with which
 
40
        this factory was created.
 
41
        """
 
42
        return {'realm': self.authenticationRealm}
 
43
 
 
44
 
 
45
    def decode(self, response, request):
 
46
        """
 
47
        Parse the base64-encoded, colon-separated username and password into a
 
48
        L{credentials.UsernamePassword} instance.
 
49
        """
 
50
        try:
 
51
            creds = binascii.a2b_base64(response + '===')
 
52
        except binascii.Error:
 
53
            raise error.LoginFailed('Invalid credentials')
 
54
 
 
55
        creds = creds.split(':', 1)
 
56
        if len(creds) == 2:
 
57
            return credentials.UsernamePassword(*creds)
 
58
        else:
 
59
            raise error.LoginFailed('Invalid credentials')