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