Setting tag sfa-0.9-13
[sfa.git] / sfa / methods / get_gid.py
1 from sfa.util.faults import *
2 from sfa.util.namespace import *
3 from sfa.util.method import Method
4 from sfa.util.parameter import Parameter, Mixed
5 from sfa.trust.auth import Auth
6 from sfa.trust.gid import GID
7 from sfa.trust.certificate import Certificate
8
9 class get_gid(Method):
10     """
11     Returns the client's gid if one exists      
12
13     @param cert certificate string 
14     @param xrn human readable name (hrn or urn)  
15     @param type object type 
16     @return client gid  
17     """
18
19     interfaces = ['registry']
20     
21     accepts = [
22         Parameter(str, "Certificate string"),
23         Parameter(str, "Human readable name (hrn or urn)"), 
24         Parameter(str, "Object type") 
25         ]
26
27     returns = Parameter(str, "GID string")
28     
29     def call(self, cert, xrn, type):
30      
31         # convert xrn to hrn     
32         if type:
33             hrn = urn_to_hrn(xrn)[0]
34         else:
35             hrn, type = urn_to_hrn(xrn)
36  
37         self.api.auth.verify_object_belongs_to_me(hrn)
38
39         # resolve the record
40         manager_base = 'sfa.managers'
41         mgr_type = self.api.config.SFA_REGISTRY_TYPE
42         manager_module = manager_base + ".registry_manager_%s" % mgr_type
43         manager = __import__(manager_module, fromlist=[manager_base])
44         records = manager.resolve(self.api, xrn, type, origin_hrn=hrn)
45         if not records:
46             raise RecordNotFound(hrn)
47         record = records[0]
48
49         # make sure client's certificate is the gid's pub key 
50         gid = GID(string=record['gid'])
51         certificate = Certificate(string=cert) 
52         if not certificate.is_pubkey(gid.get_pubkey()):
53             raise ConnectionKeyGIDMismatch(gid.get_subject())
54
55         return record['gid'] 
56