Package flumotion :: Package common :: Module keycards
[hide private]

Source Code for Module flumotion.common.keycards

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_keycards -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008 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  serializable keycards used for authentication 
 24  """ 
 25   
 26  from twisted.cred.credentials import ICredentials 
 27  from twisted.spread import pb 
 28  from zope.interface import implements 
 29   
 30  from flumotion.twisted import credentials 
 31   
 32  __version__ = "$Rev: 8774 $" 
 33  _statesEnum = ['REFUSED', 'REQUESTING', 'AUTHENTICATED'] 
 34  # state enum values 
 35  (REFUSED, 
 36   REQUESTING, 
 37   AUTHENTICATED) = range(3) 
 38   
 39   
40 -class Keycard(pb.Copyable, pb.RemoteCopy):
41 """ 42 I am the base class for keycards which together with credentials are 43 a serializable object used in authentication inside Flumotion. 44 45 @ivar bouncerName: name of the bouncer to authenticate against; set by 46 requester 47 @type bouncerName: str 48 @ivar requesterId: avatarId of the requester 49 @type requesterId: str 50 @ivar avatarId: avatarId preferred by requester 51 @type avatarId: str 52 @ivar id: id of keycard decided by bouncer after authenticating 53 @type id: object 54 @ivar duration: duration for which the keycard is valid, or 0 for 55 unlimited 56 @type duration: int 57 @ivar domain: requester can pass a domain id to the bouncer 58 @type domain: str 59 @ivar state: state the keycard is in 60 @type state: int 61 @ivar address: IP address of requester (optional) 62 @type address: str 63 @ivar username: username of requester (optional) 64 @type username: str 65 @ivar password: password of requester (optional) 66 @type password: str 67 @ivar path: path of request (optional) 68 @type path: str 69 @type token: token for request (optional) 70 @type token: str 71 @ivar arguments: arguments passed with request (optional) 72 @type arguments: dict of str->str 73 """ 74 implements(ICredentials) 75 76 address = None 77 username = None 78 password = None 79 path = None 80 token = '' 81 arguments = {} 82
83 - def __init__(self):
84 self.bouncerName = None 85 self.requesterId = None 86 self.avatarId = None 87 self.id = None 88 self.duration = 0 89 self.domain = None 90 self.state = REQUESTING 91 self.arguments = {}
92
93 - def getData(self):
94 """ 95 Return a dictionary of the viewable data on the keycard that can be 96 used to identify the keycard. 97 It doesn't include sensitive information though. 98 99 Subclasses should override to add additional information. 100 """ 101 return {'id': self.id, 102 'requester': self.requesterId, 103 'domain': self.domain, 104 'username': self.username, 105 'address': self.address, 106 'path': self.path, 107 'token': self.token, 108 'arguments': self.arguments}
109
110 - def __repr__(self):
111 return "<%s for requesterId %r in state %s>" % ( 112 self.__class__.__name__, 113 self.requesterId, _statesEnum[self.state])
114 115
116 -class KeycardGeneric(Keycard, object):
117 pass
118 119 pb.setUnjellyableForClass(KeycardGeneric, KeycardGeneric) 120 # class KeycardUACCP: username, address, crypt password 121 # from UsernameCryptPasswordCrypt 122 123 124 UCPP = credentials.UsernameCryptPasswordPlaintext 125 126
127 -class KeycardUACPP(Keycard, UCPP):
128 """ 129 I am a keycard with a username, plaintext password and IP address. 130 I get authenticated against a crypt password. 131 """ 132
133 - def __init__(self, username, password, address):
134 Keycard.__init__(self) 135 UCPP.__init__(self, username, password) 136 self.address = address
137
138 - def getData(self):
139 d = Keycard.getData(self) 140 d['username'] = self.username 141 d['address'] = self.address 142 return d
143
144 - def __repr__(self):
145 return "<%s %s %s@%s for requesterId %r in state %s>" % ( 146 self.__class__.__name__, self.id, self.username, self.address, 147 self.requesterId, _statesEnum[self.state])
148 149 pb.setUnjellyableForClass(KeycardUACPP, KeycardUACPP) 150 151 # username, address, crypt password 152 # from UsernameCryptPasswordCrypt 153 154 155 UCPCC = credentials.UsernameCryptPasswordCryptChallenger 156 157
158 -class KeycardUACPCC(Keycard, UCPCC):
159 """ 160 I am a keycard with a username and IP address. 161 I get authenticated through challenge/response on a crypt password. 162 """ 163
164 - def __init__(self, username, address):
165 Keycard.__init__(self) 166 UCPCC.__init__(self, username) 167 self.address = address
168
169 - def getData(self):
170 d = Keycard.getData(self) 171 d['username'] = self.username 172 d['address'] = self.address 173 return d
174
175 - def __repr__(self):
176 return "<%s %s %s@%s for requesterId %r in state %s>" % ( 177 self.__class__.__name__, self.id, self.username, self.address, 178 self.requesterId, _statesEnum[self.state])
179 180 pb.setUnjellyableForClass(KeycardUACPCC, KeycardUACPCC) 181 182
183 -class KeycardToken(Keycard, credentials.Token):
184 """ 185 I am a keycard with a token and IP address and a path (optional). 186 I get authenticated by token and maybe IP address. 187 """ 188
189 - def __init__(self, token, address, path=None):
190 Keycard.__init__(self) 191 credentials.Token.__init__(self, token) 192 self.address = address 193 self.path = path
194
195 - def getData(self):
196 d = Keycard.getData(self) 197 d['token'] = self.token 198 d['address'] = self.address 199 d['path'] = self.path 200 return d
201
202 - def __repr__(self):
203 return "<%s %s token %s for path %s @%s for reqId %r in state %s>" % ( 204 self.__class__.__name__, self.id, self.token, self.path, 205 self.address, self.requesterId, _statesEnum[self.state])
206 207 pb.setUnjellyableForClass(KeycardToken, KeycardToken) 208 209
210 -class KeycardHTTPGetArguments(Keycard, credentials.HTTPGetArguments):
211 """ 212 I am a keycard with a token and IP address and a path (optional). 213 I get authenticated by HTTP request GET parameters and maybe IP address. 214 215 @type address: C{str} 216 @ivar address: The HTTP client IP address. 217 @type path: C{str} 218 @ivar path: The path requested by the HTTP client. 219 """ 220
221 - def __init__(self, arguments, address, path=None):
226
227 - def getData(self):
228 d = Keycard.getData(self) 229 d['arguments'] = self.arguments 230 d['address'] = self.address 231 d['path'] = self.path 232 return d
233
234 - def __repr__(self):
235 return "<%s %s for path %s @%s for reqId %r in state %s>" % ( 236 self.__class__.__name__, self.id, self.path, 237 self.address, self.requesterId, _statesEnum[self.state])
238 239 pb.setUnjellyableForClass(KeycardHTTPGetArguments, KeycardHTTPGetArguments) 240 241 242 USPCC = credentials.UsernameSha256PasswordCryptChallenger 243 244
245 -class KeycardUASPCC(Keycard, USPCC):
246 """ 247 I am a keycard with a username and IP address. 248 I get authenticated through challenge/response on a SHA-256 password. 249 """ 250
251 - def __init__(self, username, address):
252 Keycard.__init__(self) 253 USPCC.__init__(self, username) 254 self.address = address
255
256 - def getData(self):
257 d = Keycard.getData(self) 258 d['username'] = self.username 259 d['address'] = self.address 260 return d
261
262 - def __repr__(self):
263 return "<%s %s %s@%s for requesterId %r in state %s>" % ( 264 self.__class__.__name__, self.id, self.username, self.address, 265 self.requesterId, _statesEnum[self.state])
266 267 pb.setUnjellyableForClass(KeycardUASPCC, KeycardUASPCC) 268 269
270 -class KeycardHTTPDigest(Keycard, credentials.HTTPDigestChallenger):
271
272 - def __init__(self, username):
275
276 - def getData(self):
277 d = Keycard.getData(self) 278 d['username'] = self.username 279 # Realm? Uri? 280 return d
281
282 - def __repr__(self):
283 return "<%s %s %s for requesterId %r in state %s>" % ( 284 self.__class__.__name__, self.id, self.username, 285 self.requesterId, _statesEnum[self.state])
286 287 pb.setUnjellyableForClass(KeycardHTTPDigest, KeycardHTTPDigest) 288