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.util.record import GeniRecord
11 from sfa.util.debug import log
13 class get_credential(Method):
15 Retrive a credential for an object
16 If cred == Nonee then the behavior reverts to get_self_credential
18 @param cred credential object specifying rights of the caller
19 @param type type of object (user | slice | sa | ma | node)
20 @param hrn human readable name of object
22 @return the string representation of a credential object
25 interfaces = ['registry']
28 Mixed(Parameter(str, "credential"),
29 Parameter(None, "No credential")),
30 Parameter(str, "Human readable name (hrn)")
33 returns = Parameter(str, "String representation of a credential object")
35 def call(self, cred, type, hrn):
37 return self.get_self_credential(type, hrn)
39 self.api.auth.check(cred, 'getcredential')
40 self.api.auth.verify_object_belongs_to_me(hrn)
41 auth_hrn = self.api.auth.get_authority(hrn)
44 auth_info = self.api.auth.get_auth_info(auth_hrn)
45 table = self.api.auth.get_auth_table(auth_hrn)
46 records = table.resolve(type, hrn)
48 raise RecordNotFound(hrn)
50 # verify_cancreate_credential requires that the member lists
51 # (researchers, pis, etc) be filled in
52 self.api.fill_record_info(record)
54 rights = self.api.auth.determine_user_rights(self.api.auth.client_cred, record)
56 raise PermissionError(self.api.auth.client_cred.get_gid_object().get_hrn() + " has no rights to " + record.get_name())
58 # TODO: Check permission that self.client_cred can access the object
60 object_gid = record.get_gid_object()
61 new_cred = Credential(subject = object_gid.get_subject())
62 new_cred.set_gid_caller(self.api.auth.client_gid)
63 new_cred.set_gid_object(object_gid)
64 new_cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
65 new_cred.set_pubkey(object_gid.get_pubkey())
66 new_cred.set_privileges(rights)
67 new_cred.set_delegate(True)
69 auth_kind = "authority,ma,sa"
70 new_cred.set_parent(self.api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
75 return new_cred.save_to_string(save_parents=True)
77 def get_self_credential(self, type, hrn):
79 get_self_credential a degenerate version of get_credential used by a client
80 to get his initial credential when de doesnt have one. This is the same as
81 get_credetial(..., cred = None, ...)
83 The registry ensures that the client is the principal that is named by
84 (type, name) by comparing the public key in the record's GID to the
85 private key used to encrypt the client side of the HTTPS connection. Thus
86 it is impossible for one principal to retrive another principal's
87 credential without having the appropriate private key.
89 @param type type of object (user | slice | sa | ma | node)
90 @param hrn human readable name of authority to list
91 @return string representation of a credential object
93 self.api.auth.verify_object_belongs_to_me(hrn)
95 auth_hrn = self.api.auth.get_authority(hrn)
98 auth_info = self.api.auth.get_auth_info(auth_hrn)
100 # find a record that matches
102 table = self.api.auth.get_auth_table(auth_hrn)
103 records = table.resolve('*', hrn)
105 if type in ['*'] or rec.get_type() in [type]:
108 raise RecordNotFound(hrn)
109 gid = record.get_gid_object()
110 peer_cert = self.api.auth.peer_cert
111 if not peer_cert.is_pubkey(gid.get_pubkey()):
112 raise ConnectionKeyGIDMismatch(gid.get_subject())
114 rights = self.api.auth.determine_user_rights(None, record)
115 if rights.is_empty():
116 raise PermissionError(gid.get_hrn() + " has no rights to " + record.get_name())
118 # create the credential
119 gid = record.get_gid_object()
120 cred = Credential(subject = gid.get_subject())
121 cred.set_gid_caller(gid)
122 cred.set_gid_object(gid)
123 cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
124 cred.set_pubkey(gid.get_pubkey())
125 cred.set_privileges(rights)
126 cred.set_delegate(True)
128 auth_kind = "authority,sa,ma"
129 cred.set_parent(self.api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
134 return cred.save_to_string(save_parents=True)