this one is actually used in methods
[sfa.git] / geni / util / auth.py
index 48c3922..5d16959 100644 (file)
@@ -1,17 +1,20 @@
 #
 # GeniAPI authentication 
 #
+### $Id$
+### $URL$
 #
 
 import time
+
+from geni.trust.credential import Credential
+from geni.trust.trustedroot import TrustedRootList
+from geni.trust.rights import RightList
 from geni.util.faults import *
-from geni.util.excep import *
-from geni.util.credential import Credential
-from geni.util.trustedroot import TrustedRootList
 from geni.util.hierarchy import Hierarchy
-from geni.util.rights import RightList
 from geni.util.genitable import *
 from geni.util.config import *
+from geni.util.misc import *
 
 class Auth:
     """
@@ -81,7 +84,7 @@ class Auth:
 
         @param auth_name human readable name of authority
         """
-        auth_info = self.get_auth_info(auth_name)
+        auth_info = get_auth_info(auth_name)
         table = GeniTable(hrn=auth_name,
                           cninfo=auth_info.get_dbinfo())
         # if the table doesn't exist, then it means we haven't put any records
@@ -140,13 +143,66 @@ class Auth:
             return
         if name.startswith(object_hrn + "."):
             return
+        if name.startswith(get_authority(name)):
+            return
+    
         raise PermissionError(name)
-  
+
+    def determine_user_rights(self, src_cred, record):
+        """
+        Given a user credential and a record, determine what set of rights the
+        user should have to that record.
+
+        Src_cred can be None when obtaining a user credential, but should be
+        set to a valid user credential when obtaining a slice or authority
+        credential.
+
+        This is intended to replace determine_rights() and
+        verify_cancreate_credential()
+        """
+
+        type = record.get_type()
+        if src_cred:
+            cred_object_hrn = src_cred.get_gid_object().get_hrn()
+        else:
+            # supplying src_cred==None is only valid when obtaining user
+            # credentials.
+            #assert(type == "user")
+            
+            cred_object_hrn = None
+
+        rl = RightList()
+
+        if type=="slice":
+            researchers = record.get("researcher", [])
+            if (cred_object_hrn in researchers):
+                rl.add("refresh")
+                rl.add("embed")
+                rl.add("bind")
+                rl.add("control")
+                rl.add("info")
+
+        elif type == "authority":
+            pis = record.get("pi", [])
+            operators = record.get("operator", [])
+            rl.add("authority,sa,ma")
+            if (cred_object_hrn in pis):
+                rl.add("sa")
+            if (cred_object_hrn in operators):
+                rl.add("ma")
+
+        elif type == "user":
+            rl.add("refresh")
+            rl.add("resolve")
+            rl.add("info")
+
+        return rl
+
     def verify_cancreate_credential(self, src_cred, record):
         """
-        Verify that a user can retrive a particular type of credential. 
+        Verify that a user can retrive a particular type of credential.
         For slices, the user must be on the researcher list. For SA and
-        MA the user must be on the pi and operator lists respectively 
+        MA the user must be on the pi and operator lists respectively
         """
 
         type = record.get_type()
@@ -154,45 +210,17 @@ class Auth:
         if cred_object_hrn in [self.config.GENI_REGISTRY_ROOT_AUTH]:
             return
         if type=="slice":
-            researchers = record.get_geni_info().get("researcher", [])
+            researchers = record.get("researcher", [])
             if not (cred_object_hrn in researchers):
                 raise PermissionError(cred_object_hrn + " is not in researcher list for " + record.get_name())
         elif type == "sa":
-            pis = record.get_geni_info().get("pi", [])
+            pis = record.get("pi", [])
             if not (cred_object_hrn in pis):
                 raise PermissionError(cred_object_hrn + " is not in pi list for " + record.get_name())
         elif type == "ma":
-            operators = record.get_geni_info().get("operator", [])
+            operators = record.get("operator", [])
             if not (cred_object_hrn in operators):
                 raise PermissionError(cred_object_hrn + " is not in operator list for " + record.get_name())
 
-    def get_leaf(self, hrn):
-        parts = hrn.split(".")
-        return ".".join(parts[-1:])
-
     def get_authority(self, hrn):
-
-        parts = hrn.split(".")
-        return ".".join(parts[:-1])
-
-    def get_auth_type(self, type):
-        if (type=="slice") or (type=="user") or (type=="sa"):
-            return "sa"
-        elif (type=="component") or (type=="ma"):
-            return "ma"
-        else:
-            raise UnknownGeniType(type)
-
-    def hrn_to_pl_slicename(self, hrn):
-        parts = hrn.split(".")
-        return parts[-2] + "_" + parts[-1]
-
-    # assuming hrn is the hrn of an authority, return the plc authority name
-    def hrn_to_pl_authname(self, hrn):
-        parts = hrn.split(".")
-        return parts[-1]
-
-    # assuming hrn is the hrn of an authority, return the plc login_base
-    def hrn_to_pl_login_base(self, hrn):
-        return self.hrn_to_pl_authname(hrn)
-      
+        return get_authority(hrn)