added origin_hrn to accepted args
[sfa.git] / sfa / methods / get_credential.py
1 ### $Id$
2 ### $URL$
3
4 from sfa.trust.credential import *
5 from sfa.trust.rights import *
6 from sfa.util.faults import *
7 from sfa.util.method import Method
8 from sfa.util.parameter import Parameter, Mixed
9 from sfa.trust.auth import Auth
10 from sfa.trust.gid import GID
11 from sfa.util.record import GeniRecord
12 from sfa.util.genitable import *
13 from sfa.util.debug import log
14
15 class get_credential(Method):
16     """
17     Retrive a credential for an object
18     If cred == Nonee then the behavior reverts to get_self_credential
19
20     @param cred credential object specifying rights of the caller
21     @param type type of object (user | slice | sa | ma | node)
22     @param hrn human readable name of object
23     @param origin_hrn human readable name of calls origin 
24
25     @return the string representation of a credential object  
26     """
27
28     interfaces = ['registry']
29     
30     accepts = [
31         Mixed(Parameter(str, "credential"),
32               Parameter(None, "No credential")),  
33         Parameter(str, "Human readable name (hrn)"),
34         Mixed(Parameter(str, "Request hash"),
35               Parameter(None, "Request hash not specified")),
36         Parameter(str, "Human readable name (hrn)"),
37         ]
38
39     returns = Parameter(str, "String representation of a credential object")
40
41     def call(self, cred, type, hrn, origin_hrn=None, request_hash=None):
42
43         self.api.auth.authenticateCred(cred, [cred, type, hrn], request_hash)
44         self.api.auth.check(cred, 'getcredential')
45         self.api.auth.verify_object_belongs_to_me(hrn)
46         auth_hrn = self.api.auth.get_authority(hrn)
47
48         # Is this a root or sub authority 
49         if not auth_hrn or hrn == self.api.config.SFA_INTERFACE_HRN:
50             auth_hrn = hrn
51
52         # get record info
53         auth_info = self.api.auth.get_auth_info(auth_hrn)
54         table = GeniTable()
55         records = table.find({'type': type, 'hrn': hrn})
56         if not records:
57             raise RecordNotFound(hrn)
58         record = records[0]
59         
60         # get the origin caller's gid (this is the caller's gid by default)    
61         if origin_hrn:
62             orgin_records = table.find({'hrn': origin_hrn})
63             if not origin_records:
64                 raise RecordNotFound(origin_hrn)
65             origin_record = origin_records[0]
66             origin_caller_gid_object = GID(string = record['gid'])
67         else:
68             origin_caller_gid_object = self.api.auth.client_gid
69
70         
71         # verify_cancreate_credential requires that the member lists
72         # (researchers, pis, etc) be filled in
73         self.api.fill_record_info(record)
74
75         caller_hrn = self.api.auth.client_cred.get_gid_caller().get_hrn()
76         rights = self.api.auth.determine_user_rights(caller_hrn, record)
77         if rights.is_empty():
78             raise PermissionError(self.api.auth.client_cred.get_gid_object().get_hrn() + " has no rights to " + record['name'])
79
80         # TODO: Check permission that self.client_cred can access the object
81
82         gid = record['gid']
83         gid_object = GID(string=gid)
84
85         new_cred = Credential(subject = gid_object.get_subject())
86         new_cred.set_gid_caller(self.api.auth.client_gid)
87         new_cred.set_gid_origin_caller(origin_caller_gid_object)
88         new_cred.set_gid_object(gid_object)
89         new_cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
90         new_cred.set_pubkey(gid_object.get_pubkey())
91         new_cred.set_privileges(rights)
92         new_cred.set_delegate(True)
93
94         auth_kind = "authority,ma,sa"
95         new_cred.set_parent(self.api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
96
97         new_cred.encode()
98         new_cred.sign()
99
100         return new_cred.save_to_string(save_parents=True)
101