1 #!/usr/bin/env python 2 3 """ 4 Authentication/authorisation helper classes and functions. 5 """ 6 7 import base64 8 import md5 9 10 class UserInfo: 11 12 """ 13 A class used to represent user information in terms of the authentication 14 scheme employed and the user details. 15 """ 16 17 def __init__(self, auth_header): 18 19 """ 20 Initialise the object with the value of the 'auth_header' - that is, the 21 HTTP Authorization header. 22 """ 23 24 self.scheme, auth_details = auth_header.split(" ") 25 if self.scheme == "Basic": 26 27 # NOTE: Assume that no username or password contains ":". 28 29 self.username, self.password = base64.decodestring(auth_details).split(":") 30 31 else: 32 33 # NOTE: Other schemes not yet supported. 34 35 self.username, self.password = None, None 36 37 def get_token(plaintext, secret_key): 38 39 """ 40 Return a string containing an authentication token made from the given 41 'plaintext' and 'secret_key'. 42 """ 43 44 return plaintext + ":" + md5.md5(plaintext + secret_key).hexdigest() 45 46 # vim: tabstop=4 expandtab shiftwidth=4