1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import hmac
17 from gofer import *
18 from threading import RLock
25 def _fn(*a,**k):
26 k = fn(*a,**k)
27 if callable(k):
28 return k()
29 else:
30 return k
31 return _fn
32
39
41 return 'digest: "%s" for: uuid=%s, not matched' % \
42 (self.args[0],
43 self.args[1])
44
46
48 return 'key for uuid=%s, not found' % self.args[0]
49
50
51
52
53
54 -class Role:
55
56 ROLES = {
57 0:'signing',
58 1:'validation',
59 'signing':0,
60 'validation':1,}
61
63 if isinstance(id, str):
64 self.id = self.ROLES[id]
65 else:
66 self.id = id
67
70
73
76
78 if isinstance(kp, dict):
79 self.dict = {}
80 for k,v in kp.items():
81 r = Role(k)
82 self.dict[int(r)] = v
83 return
84 if isinstance(kp, str):
85 self.dict = {0:kp, 1:kp}
86 return
87 if callable(kp):
88 self.dict = {0:kp, 1:kp}
89 return
90 if isinstance(kp, (tuple,list)):
91 self.dict = {0:kp[0], 1:kp[1]}
92 return
93 raise ValueError(kp)
94
96 err = 0
97 for k in (0, 1):
98 v = self.dict.get(k)
99 if isinstance(v, str):
100 continue
101 if callable(v):
102 continue
103 err += 1
104 return (err == 0)
105
108
109 DEFAULT = None
110
112 self.__mutex = RLock()
113 self.__keydict = {}
114 self.update(keychain)
115
116 - def add(self, id, *kp, **roles):
117 self.set(id, *kp, **roles)
118
121
122 @synchronized
125
126 @synchronized
127 - def set(self, id, *kp, **roles):
128 if len(kp) == 0:
129 kp = KeyPair(roles)
130 if kp.valid():
131 self.__keydict[id] = kp.dict
132 else:
133 raise ValueError()
134 return
135 if len(kp) == 1:
136 kp = KeyPair(kp[0])
137 self.__keydict[id] = kp.dict
138 return
139 if len(kp) == 2:
140 kp = KeyPair(kp)
141 self.__keydict[id] = kp.dict
142 return
143 raise ValueError()
144
145 @synchronized
147 self.__keydict.pop(id, None)
148
149 @synchronized
151 if isinstance(d, KeyChain):
152 self.__keydict.update(d.dict())
153 return
154 for k,v in d.items():
155 self.set(k,v)
156
157 @synchronized
158 - def get(self, id, d=None):
159 return self.__keydict.get(id, d)
160
161 @resolved
162 @synchronized
163 - def find(self, role, id, d=None):
164 kp = self.get(id)
165 if not kp:
166 kp = self.get(self.DEFAULT)
167 if kp:
168 key = kp.get(role)
169 else:
170 key = d
171 return key
172
173 @synchronized
175 return dict(self.__keydict)
176
177 @synchronized
180
181 @synchronized
183 return self.__keydict[id]
184
185 @synchronized
187 return repr(self.__keydict)
188
189 @synchronized
191 return str(self.__keydict)
192
193
194
195
196
197
198 -class HMAC:
199
200 PROPERTY = 'digest'
201
205
207 envelope.pop(self.PROPERTY, None)
208 uuid = envelope.routing[1]
209 key = self.__key(0, uuid)
210 if key:
211 hash = hmac.new(key)
212 hash.update(repr(envelope))
213 envelope.digest = hash.hexdigest()
214 return envelope
215
228
229 - def __key(self, role, uuid):
236
237
238
239
240
241 from gofer.messaging import Envelope
253
256
257 routing=('A','B')
258 e = Envelope(routing=routing)
259 keychain = KeyChain()
260 keychain.set('B', '0xAA', '0xBB')
261 keychain.set('C', ('0xXX', '0xYY'))
262 keychain.set('D', {0:'0xXX', 1:'0xYY'})
263 T = ('C', '0xGG', '0xZZ')
264 keychain.set(*T)
265 auth = HMAC(keychain)
266 outbound = auth.outbound(e)
267 print outbound
268
269 keychain = KeyChain()
270 keychain.add('A', signing='0xBB', validation='0xAA')
271 auth = HMAC(keychain)
272 auth.inbound(outbound)
273 print 'test2:validated'
274
285
286 if __name__ == '__main__':
287 test1()
288 test2()
289 test3()
290