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: 7640 $" 
 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 """ 62 implements(ICredentials) 63
64 - def __init__(self):
65 self.bouncerName = None 66 self.requesterId = None 67 self.avatarId = None 68 self.id = None 69 self.duration = 0 70 self.domain = None 71 self.state = REQUESTING
72 73 # F0.8 74
75 - def setDomain(self, domain):
76 """ 77 Set the domain of the requester on the keycard. 78 79 @type domain: string 80 """ 81 import warnings 82 warnings.warn('Set the domain on the keycard directly.', 83 DeprecationWarning, stacklevel=2) 84 85 self.domain = domain
86
87 - def getData(self):
88 """ 89 Return a dictionary of the viewable data on the keycard that can be 90 used to identify the keycard. 91 It doesn't include sensitive information though. 92 93 Subclasses should override to add additional information. 94 """ 95 return {'id': self.id, 96 'requester': self.requesterId, 97 'domain': self.domain}
98
99 - def __repr__(self):
100 return "<%s for requesterId %r in state %s>" % ( 101 self.__class__.__name__, 102 self.requesterId, _statesEnum[self.state])
103 104
105 -class KeycardGeneric(Keycard, object):
106 pass
107 108 pb.setUnjellyableForClass(KeycardGeneric, KeycardGeneric) 109 # class KeycardUACCP: username, address, crypt password 110 # from UsernameCryptPasswordCrypt 111 112 113 UCPP = credentials.UsernameCryptPasswordPlaintext 114 115
116 -class KeycardUACPP(Keycard, UCPP):
117 """ 118 I am a keycard with a username, plaintext password and IP address. 119 I get authenticated against a crypt password. 120 """ 121
122 - def __init__(self, username, password, address):
123 UCPP.__init__(self, username, password) 124 Keycard.__init__(self) 125 self.address = address
126
127 - def getData(self):
128 d = Keycard.getData(self) 129 d['username'] = self.username 130 d['address'] = self.address 131 return d
132
133 - def __repr__(self):
134 return "<%s %s %s@%s for requesterId %r in state %s>" % ( 135 self.__class__.__name__, self.id, self.username, self.address, 136 self.requesterId, _statesEnum[self.state])
137 138 pb.setUnjellyableForClass(KeycardUACPP, KeycardUACPP) 139 140 # username, address, crypt password 141 # from UsernameCryptPasswordCrypt 142 143 144 UCPCC = credentials.UsernameCryptPasswordCryptChallenger 145 146
147 -class KeycardUACPCC(Keycard, UCPCC):
148 """ 149 I am a keycard with a username and IP address. 150 I get authenticated through challenge/response on a crypt password. 151 """ 152
153 - def __init__(self, username, address):
154 UCPCC.__init__(self, username) 155 Keycard.__init__(self) 156 self.address = address
157
158 - def getData(self):
159 d = Keycard.getData(self) 160 d['username'] = self.username 161 d['address'] = self.address 162 return d
163
164 - def __repr__(self):
165 return "<%s %s %s@%s for requesterId %r in state %s>" % ( 166 self.__class__.__name__, self.id, self.username, self.address, 167 self.requesterId, _statesEnum[self.state])
168 169 pb.setUnjellyableForClass(KeycardUACPCC, KeycardUACPCC) 170 171
172 -class KeycardToken(Keycard, credentials.Token):
173 """ 174 I am a keycard with a token and IP address and a path (optional). 175 I get authenticated by token and maybe IP address. 176 """ 177
178 - def __init__(self, token, address, path=None):
179 credentials.Token.__init__(self, token) 180 Keycard.__init__(self) 181 self.address = address 182 self.path = path
183
184 - def getData(self):
185 d = Keycard.getData(self) 186 d['token'] = self.token 187 d['address'] = self.address 188 d['path'] = self.path 189 return d
190
191 - def __repr__(self):
192 return "<%s %s token %s for path %s @%s for reqId %r in state %s>" % ( 193 self.__class__.__name__, self.id, self.token, self.path, 194 self.address, self.requesterId, _statesEnum[self.state])
195 196 pb.setUnjellyableForClass(KeycardToken, KeycardToken) 197 198
199 -class KeycardHTTPGetArguments(Keycard, credentials.HTTPGetArguments):
200 """ 201 I am a keycard with a token and IP address and a path (optional). 202 I get authenticated by HTTP request GET parameters and maybe IP address. 203 204 @type address: C{str} 205 @ivar address: The HTTP client IP address. 206 @type path: C{str} 207 @ivar path: The path requested by the HTTP client. 208 """ 209
210 - def __init__(self, arguments, address, path=None):
211 credentials.HTTPGetArguments.__init__(self, arguments) 212 Keycard.__init__(self) 213 self.address = address 214 self.path = path
215
216 - def getData(self):
217 d = Keycard.getData(self) 218 d['arguments'] = self.arguments 219 d['address'] = self.address 220 d['path'] = self.path 221 return d
222
223 - def __repr__(self):
224 return "<%s %s for path %s @%s for reqId %r in state %s>" % ( 225 self.__class__.__name__, self.id, self.path, 226 self.address, self.requesterId, _statesEnum[self.state])
227 228 pb.setUnjellyableForClass(KeycardHTTPGetArguments, KeycardHTTPGetArguments) 229 230 231 USPCC = credentials.UsernameSha256PasswordCryptChallenger 232 233
234 -class KeycardUASPCC(Keycard, USPCC):
235 """ 236 I am a keycard with a username and IP address. 237 I get authenticated through challenge/response on a SHA-256 password. 238 """ 239
240 - def __init__(self, username, address):
241 USPCC.__init__(self, username) 242 Keycard.__init__(self) 243 self.address = address
244
245 - def getData(self):
246 d = Keycard.getData(self) 247 d['username'] = self.username 248 d['address'] = self.address 249 return d
250
251 - def __repr__(self):
252 return "<%s %s %s@%s for requesterId %r in state %s>" % ( 253 self.__class__.__name__, self.id, self.username, self.address, 254 self.requesterId, _statesEnum[self.state])
255 256 pb.setUnjellyableForClass(KeycardUASPCC, KeycardUASPCC) 257 258
259 -class KeycardHTTPDigest(Keycard, credentials.HTTPDigestChallenger):
260
261 - def __init__(self, username):
264
265 - def getData(self):
266 d = Keycard.getData(self) 267 d['username'] = self.username 268 # Realm? Uri? 269 return d
270
271 - def __repr__(self):
272 return "<%s %s %s for requesterId %r in state %s>" % ( 273 self.__class__.__name__, self.id, self.username, 274 self.requesterId, _statesEnum[self.state])
275 276 pb.setUnjellyableForClass(KeycardHTTPDigest, KeycardHTTPDigest) 277 278 279 # F0.8 280 281
282 -class HTTPDigestKeycard(KeycardHTTPDigest):
283
284 - def __init__(self, username):
285 import warnings 286 warnings.warn('Use KeycardHTTPDigest instead.', DeprecationWarning, 287 stacklevel=2) 288 KeycardHTTPDigest.__init__(self, username)
289 290 pb.setUnjellyableForClass(HTTPDigestKeycard, HTTPDigestKeycard) 291