1 #!/usr/bin/env python 2 3 """ 4 Authentication/authorisation helper classes and functions. 5 6 Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk> 7 8 This library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU Lesser General Public 10 License as published by the Free Software Foundation; either 11 version 2.1 of the License, or (at your option) any later version. 12 13 This library is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 Lesser General Public License for more details. 17 18 You should have received a copy of the GNU Lesser General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 """ 22 23 import base64 24 import md5 25 26 class UserInfo: 27 28 """ 29 A class used to represent user information in terms of the authentication 30 scheme employed and the user details. 31 """ 32 33 def __init__(self, auth_header): 34 35 """ 36 Initialise the object with the value of the 'auth_header' - that is, the 37 HTTP Authorization header. 38 """ 39 40 self.scheme, auth_details = auth_header.split(" ") 41 if self.scheme == "Basic": 42 43 # NOTE: Assume that no username or password contains ":". 44 45 self.username, self.password = base64.decodestring(auth_details).split(":") 46 47 else: 48 49 # NOTE: Other schemes not yet supported. 50 51 self.username, self.password = None, None 52 53 def get_token(plaintext, secret_key): 54 55 """ 56 Return a string containing an authentication token made from the given 57 'plaintext' and 'secret_key'. 58 """ 59 60 return plaintext + ":" + md5.md5(plaintext + secret_key).hexdigest() 61 62 # vim: tabstop=4 expandtab shiftwidth=4