for python-2.6
[plcapi.git] / PLC / Auth.py
index 3b005b6..b11e855 100644 (file)
@@ -5,6 +5,7 @@
 # Copyright (C) 2006 The Trustees of Princeton University
 #
 # $Id$
+# $URL$
 #
 
 import crypt
@@ -21,6 +22,23 @@ from PLC.Sessions import Session, Sessions
 from PLC.Peers import Peer, Peers
 from PLC.Boot import notify_owners
 
+def map_auth(auth):
+    if auth['AuthMethod'] == "session":
+        expected = SessionAuth()
+    elif auth['AuthMethod'] == "password" or \
+         auth['AuthMethod'] == "capability":
+        expected = PasswordAuth()
+    elif auth['AuthMethod'] == "gpg":
+        expected = GPGAuth()
+    elif auth['AuthMethod'] == "hmac" or \
+         auth['AuthMethod'] == "hmac_dummybox":
+        expected = BootAuth()
+    elif auth['AuthMethod'] == "anonymous":
+        expected = AnonymousAuth()
+    else:
+        raise PLCInvalidArgument("must be 'session', 'password', 'gpg', 'hmac', 'hmac_dummybox', or 'anonymous'", "AuthMethod")
+    return expected
+
 class Auth(Parameter):
     """
     Base class for all API authentication methods, as well as a class
@@ -38,20 +56,7 @@ class Auth(Parameter):
         # mandatory fields were present.
         assert 'AuthMethod' in auth
 
-        if auth['AuthMethod'] == "session":
-            expected = SessionAuth()
-        elif auth['AuthMethod'] == "password" or \
-             auth['AuthMethod'] == "capability":
-            expected = PasswordAuth()
-        elif auth['AuthMethod'] == "gpg":
-            expected = GPGAuth()
-        elif auth['AuthMethod'] == "hmac" or \
-             auth['AuthMethod'] == "hmac_dummybox":
-            expected = BootAuth()
-        elif auth['AuthMethod'] == "anonymous":
-            expected = AnonymousAuth()
-        else:
-            raise PLCInvalidArgument("must be 'session', 'password', 'gpg', 'hmac', 'hmac_dummybox', or 'anonymous'", "AuthMethod")
+        expected = map_auth(auth)
 
         # Re-check using the specified authentication method
         method.type_check("auth", auth, expected, (auth,) + args)
@@ -251,7 +256,8 @@ class BootAuth(Auth):
 
             # We encode in UTF-8 before calculating the HMAC, which is
             # an 8-bit algorithm.
-            digest = hmac.new(key, msg.encode('utf-8'), sha).hexdigest()
+            # python 2.6 insists on receiving a 'str' as opposed to a 'unicode'
+            digest = hmac.new(str(key), msg.encode('utf-8'), sha).hexdigest()
 
             if digest != auth['value']:
                 raise PLCAuthenticationFailure, "Call could not be authenticated"