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