f7bc721a9c0c9c9605a8cf77fdbfc37596d8fc08
[sfa.git] / sfa / methods / GetCredential.py
1 from sfa.util.xrn import urn_to_hrn
2 from sfa.util.method import Method
3
4 from sfa.trust.credential import Credential
5
6 from sfa.storage.parameter import Parameter, Mixed
7
8
9 class GetCredential(Method):
10     """
11     Retrive a credential for an object
12     If cred == None then the behavior reverts to GetSelfCredential
13
14     @param hrn human readable name of object (hrn or urn)
15     @param cred credential object specifying rights of the caller
16     @param type type of object (user | slice | node | authority )
17
18     @return the string representation of a credential object  
19     """
20
21     interfaces = ['registry']
22
23     accepts = [
24         Mixed(Parameter(str, "Credential string"),
25               Parameter(type([str]), "List of credentials")),
26         Parameter(str, "Human readable name (hrn or urn)"),
27         Mixed(Parameter(str, "Record type"),
28               Parameter(None, "Type not specified")),
29     ]
30
31     returns = Parameter(str, "String representation of a credential object")
32
33     def call(self, creds, xrn, type):
34
35         if type:
36             hrn = urn_to_hrn(xrn)[0]
37         else:
38             hrn, type = urn_to_hrn(xrn)
39
40         # check creds
41         valid_creds = self.api.auth.checkCredentials(creds, 'getcredential')
42         self.api.auth.verify_object_belongs_to_me(hrn)
43
44         # log the call
45         origin_hrn = Credential(
46             string=valid_creds[0]).get_gid_caller().get_hrn()
47         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s" %
48                              (self.api.interface, origin_hrn, hrn, self.name))
49
50         return self.api.manager.GetCredential(self.api, xrn, type, self.api.auth.client_gid.get_urn())