# Copyright (C) 2006 The Trustees of Princeton University
#
# $Id$
+# $URL$
#
import crypt
-import sha
+import hashlib
import hmac
import time
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
# 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":
- expected = BootAuth()
- elif auth['AuthMethod'] == "anonymous":
- expected = AnonymousAuth()
- else:
- raise PLCInvalidArgument("must be 'session', 'password', 'gpg', 'hmac', or 'anonymous'", "AuthMethod")
+ expected = map_auth(auth)
# Re-check using the specified authentication method
method.type_check("auth", auth, expected, (auth,) + args)
# 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'), hashlib.sha1).hexdigest()
if digest != auth['value']:
raise PLCAuthenticationFailure, "Call could not be authenticated"