X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FAuth.py;h=c21ec8ce10e25e4367e44048f267f68f1ef34c90;hb=e7bfdef7debb7fbd6c851e7b3cdfb9544cf09df9;hp=0bac9db676e202b9cb1f1535afd994ef357be7a5;hpb=c348a3b1e8b7333d7f9ad17be6991aca8539c77e;p=plcapi.git diff --git a/PLC/Auth.py b/PLC/Auth.py index 0bac9db..c21ec8c 100644 --- a/PLC/Auth.py +++ b/PLC/Auth.py @@ -4,7 +4,7 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: Auth.py,v 1.11 2007/01/11 05:28:21 mlhuang Exp $ +# $Id: Auth.py,v 1.14 2007/01/31 22:41:00 mlhuang Exp $ # import crypt @@ -23,17 +23,36 @@ from PLC.Boot import notify_owners class Auth(Parameter): """ Base class for all API authentication methods, as well as a class - that can be used to represent Mixed(SessionAuth(), PasswordAuth(), - GPGAuth()), i.e. the three principal API authentication methods. + that can be used to represent all supported API authentication + methods. """ - def __init__(self, auth = {}): + def __init__(self, auth = None): + if auth is None: + auth = {'AuthMethod': Parameter(str, "Authentication method to use", optional = False)} Parameter.__init__(self, auth, "API authentication structure") def check(self, method, auth, *args): - method.type_check("auth", auth, - Mixed(SessionAuth(), PasswordAuth(), GPGAuth()), - (auth,) + args) + # Method.type_check() should have checked that all of the + # 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") + + # Re-check using the specified authentication method + method.type_check("auth", auth, expected, (auth,) + args) class GPGAuth(Auth): """ @@ -73,7 +92,7 @@ class GPGAuth(Auth): for key in keys: try: from PLC.GPG import gpg_verify - gpg_verify(method.name, args, auth['signature'], key) + gpg_verify(args, key, auth['signature'], method.name) return except PLCAuthenticationFailure, fault: pass @@ -182,6 +201,9 @@ class BootAuth(Auth): # mandatory fields were present. assert auth.has_key('node_id') + if 'node' not in method.roles: + raise PLCAuthenticationFailure, "Not allowed to call method" + try: nodes = Nodes(method.api, {'node_id': auth['node_id'], 'peer_id': None}) if not nodes: @@ -215,7 +237,7 @@ class BootAuth(Auth): raise PLCAuthenticationFailure, "Cannot determine IP address of requestor" if nodenetwork['ip'] != method.source[0]: - raise PLCAuthenticationFailure, "Requestor IP %s does not mach node IP %s" % \ + raise PLCAuthenticationFailure, "Requestor IP %s does not match node IP %s" % \ (method.source[0], nodenetwork['ip']) else: raise PLCAuthenticationFailure, "No node key or boot nonce" @@ -250,8 +272,10 @@ class AnonymousAuth(Auth): }) def check(self, method, auth, *args): - # Sure, dude, whatever - return True + if 'anonymous' not in method.roles: + raise PLCAuthenticationFailure, "Not allowed to call method anonymously" + + method.caller = None class PasswordAuth(Auth): """ @@ -260,7 +284,7 @@ class PasswordAuth(Auth): def __init__(self): Auth.__init__(self, { - 'AuthMethod': Parameter(str, "Authentication method to use, typically 'password'", optional = False), + 'AuthMethod': Parameter(str, "Authentication method to use, always 'password' or 'capability'", optional = False), 'Username': Parameter(str, "PlanetLab username, typically an e-mail address", optional = False), 'AuthString': Parameter(str, "Authentication string, typically a password", optional = False), })