Package flumotion :: Package twisted :: Module checkers
[hide private]

Source Code for Module flumotion.twisted.checkers

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_checkers -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """ 
 23  Flumotion Twisted credential checkers 
 24  """ 
 25   
 26  from twisted.cred import checkers 
 27  from twisted.internet import defer 
 28  from twisted.python import failure 
 29  from zope.interface import implements 
 30   
 31  from flumotion.common import log, errors 
 32  from flumotion.twisted import credentials 
 33   
 34  __version__ = "$Rev$" 
 35   
 36   
 37  # FIXME: give the manager's bouncer's checker to the flexcredchecker, 
 38  # and forward to it 
 39   
 40   
41 -class FlexibleCredentialsChecker(log.Loggable):
42 """ 43 I am an in-memory username/password credentials checker that also 44 allows anonymous logins if instructed to do so. 45 """ 46 logCategory = 'credchecker' 47 implements(checkers.ICredentialsChecker) 48 49 credentialInterfaces = (credentials.IUsernamePassword, 50 credentials.IUsernameHashedPassword) 51
52 - def __init__(self, **users):
53 self.users = users 54 self._passwordless = False # do we allow passwordless logins ?
55
56 - def allowPasswordless(self, wellDoWeQuestionMark):
57 self._passwordless = wellDoWeQuestionMark
58
59 - def addUser(self, username, password):
60 self.users[username] = password
61
62 - def _cbPasswordMatch(self, matched, username, avatarId):
63 if matched: 64 return avatarId or username 65 else: 66 return failure.Failure(errors.NotAuthenticatedError())
67 68 ### ICredentialsChecker interface methods 69
70 - def requestAvatarId(self, credentials):
71 avatarId = getattr(credentials, 'avatarId', None) 72 73 if self._passwordless: 74 self.debug('allowing passwordless login for user %s', 75 credentials.username) 76 return defer.succeed(avatarId or credentials.username) 77 elif credentials.username in self.users: 78 self.debug('authenticating user %s' % credentials.username) 79 return defer.maybeDeferred( 80 credentials.checkPassword, 81 self.users[credentials.username]).addCallback( 82 self._cbPasswordMatch, str(credentials.username), 83 avatarId) 84 else: 85 return defer.fail(errors.NotAuthenticatedError())
86 87
88 -class CryptChecker(log.Loggable):
89 """ 90 I check credentials using a crypt-based backend. 91 """ 92 implements(checkers.ICredentialsChecker) 93 credentialInterfaces = (credentials.IUsernameCryptPassword, ) 94 95 logCategory = 'cryptchecker' 96
97 - def __init__(self, **users):
98 self.users = users
99
100 - def addUser(self, username, cryptPassword):
101 """ 102 Add the given username and password. 103 104 @param username: name of the user to add 105 @type username: string 106 @param cryptPassword: the crypted password for this user 107 @type cryptPassword: string 108 """ 109 self.debug('added user %s' % username) 110 self.users[username] = cryptPassword
111
112 - def _cbCryptPasswordMatch(self, matched, username):
113 if matched: 114 self.debug('user %s authenticated' % username) 115 return username 116 else: 117 self.debug('user %s refused, password not matched' % username) 118 return failure.Failure(errors.NotAuthenticatedError())
119 120 ### ICredentialsChecker methods 121
122 - def requestAvatarId(self, credentials):
123 if credentials.username in self.users: 124 return defer.maybeDeferred( 125 credentials.checkCryptPassword, 126 self.users[credentials.username]).addCallback( 127 self._cbCryptPasswordMatch, credentials.username) 128 else: 129 self.debug("user '%s' refused, not in storage backend" % 130 credentials.username) 131 return defer.fail(errors.NotAuthenticatedError())
132 133
134 -class Sha256Checker(log.Loggable):
135 """ 136 I check credentials using a SHA-256-based backend. 137 """ 138 implements(checkers.ICredentialsChecker) 139 credentialInterfaces = (credentials.IUsernameSha256Password, ) 140 141 logCategory = 'sha256checker' 142
143 - def __init__(self, **users):
144 self.users = users
145
146 - def addUser(self, username, salt, sha256Data):
147 """ 148 Add the given username and password. 149 150 @param username: name of the user to add 151 @type username: str 152 @param salt: the salt for this user 153 @type salt: str 154 @param sha256Data: the sha256 data for this user 155 @type sha256Data: str 156 """ 157 self.debug('added user %s' % username) 158 self.users[username] = (salt, sha256Data)
159
160 - def _cbSha256PasswordMatch(self, matched, username):
161 if matched: 162 self.debug('user %s authenticated' % username) 163 return username 164 else: 165 self.debug('user %s refused, password not matched' % username) 166 return failure.Failure(errors.NotAuthenticatedError())
167 168 ### ICredentialsChecker methods 169
170 - def requestAvatarId(self, credentials):
171 if credentials.username in self.users: 172 salt, data = self.users[credentials.username] 173 password = salt + data 174 return defer.maybeDeferred( 175 credentials.checkSha256Password, 176 password).addCallback( 177 self._cbSha256PasswordMatch, credentials.username) 178 else: 179 self.debug('user %s refused, not in database' % 180 credentials.username) 181 return defer.fail(errors.NotAuthenticatedError())
182