import repository from arizona
[raven.git] / apps / gacks / gacksreceipt.py
1 ##
2 # Implements Gacks Receipts
3 ##
4
5 from sfa.trust.certificate import *
6 from sfa.trust.rights import *
7 from sfa.trust.gid import *
8 from gackshandle import *
9 import xmlrpclib
10
11 ##
12 # Credential is a tuple:
13 #     (Sequence, Handle, Action, GacksRecordList, RequestHash, Signature)
14 #
15 # Signature is implicit in the fact that the receipt is an x509 certificate and
16 # is signed by the party that created the certificate.
17
18 class GacksReceipt(Certificate):
19     sequence = None
20     handle = None
21     action = None
22     records = []
23     requestHash = None
24
25     def __init__(self, create=False, subject=None, string=None, filename=None, sequence=None, handle=None, action=None, reclist=None, requestHash=None):
26         Certificate.__init__(self, create=create, subject=subject, string=string, filename=filename)
27         if sequence:
28             self.SetSequence(sequence)
29         if handle:
30             self.SetHandle(handle)
31         if action:
32             self.SetAction(action)
33         if requestHash:
34             self.SetRequestHash(requestHash)
35         if reclist:
36             for rec in reclist:
37                 self.AddRecord(rec)
38
39     def SetSequence(self, s):
40         self.sequence = s
41
42     def GetSequence(self):
43         if not self.sequence:
44             self.decode()
45         return self.sequence
46
47     def SetHandle(self,h):
48         self.handle = h
49
50     def GetHandle(self):
51         if not self.handle:
52             self.decode()
53         return self.handle
54
55     def SetAction(self,a):
56         self.action = a
57
58     def GetAction(self):
59         if not self.action:
60             self.decode()
61         return self.action
62
63     def AddRecord(self,r):
64         self.records.append(r)
65
66     def GetRecords(self):
67         if not self.records:
68             self.decode()
69         return self.records
70
71     def SetRequestHash(self, h):
72         self.requestHash = h
73
74     def GetRequestHash(self):
75         if not self.requestHash:
76             self.decode()
77         return self.requestHash
78
79     def set_data(self, str):
80         # recent PLC does not put URI:http:// in front
81         Certificate.set_data(self, "URI:http://" + str)
82
83     def get_data(self):
84         str = Certificate.get_data(self)
85         while (str.startswith("URI:http://")):
86             str = str[11:]
87         return str
88
89     def encode(self):
90         dict = {"handle": None,
91                 "action": self.action,
92                 "records": None,
93                 "requestHash": self.requestHash}
94         if self.handle:
95             dict["handle"] = self.handle.as_string()
96         if self.records:
97             recDictList = []
98             for record in self.records:
99                 recDictList.append(record.as_dict())
100             dict["records"] = recDictList
101         str = xmlrpclib.dumps((dict,), allow_none=True)
102         self.set_data(str)
103
104     def decode(self):
105         data = self.get_data()
106         if data:
107             dict = xmlrpclib.loads(self.get_data())[0][0]
108         else:
109             dict = {}
110
111         handleStr = dict.get("handle", None)
112         if handleStr:
113             self.handle = GacksHandle(string=handleStr)
114         else:
115             self.handle = None
116
117         self.action = dict.get("action", None)
118
119         self.records = []
120         recDictList = dict.get("records", None)
121         if recDictList:
122             for recDict in recDictList:
123                 self.records.append(GacksRecord(dict = recDict))
124
125         self.requestHash = dict.get("requestHash", None)
126
127     def dump(self):
128         print "handle:"
129         if self.handle:
130             self.handle.dump()
131         print "action:", self.action
132         print "records:"
133         for record in self.records:
134             record.dump()
135