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