-
Notifications
You must be signed in to change notification settings - Fork 14
/
keymaster.py
44 lines (37 loc) · 1.25 KB
/
keymaster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from google.appengine.api import urlfetch, memcache
from google.appengine.ext import webapp
import urllib
## Example usage
#
# def needs_key():
# key = keymaster.get('my-key')
# if key:
# # Do something with key
# else:
# keymaster.request('my-key')
#
# def main():
# application = webapp.WSGIApplication([
# ('/key/(.+)', keymaster.Handler({
# 'my-key': ('6f7e21711e29e6d4b4e64daceb2a7348', '2isy046g', needs_key),
# 'another-key': ('keymaster-hash', 'keymaster-secret', optional_key_arrival_callback),
# })),
# ], debug=True)
_keys = {}
def get(keyname):
return memcache.get(keyname, namespace='keymaster')
def request(keyname):
urlfetch.fetch('http://www.thekeymaster.org/%s' % _keys[keyname][0], method='POST', payload=urllib.urlencode({'secret': _keys[keyname][1]}), deadline=10)
class _Handler(webapp.RequestHandler):
def get(self, keyname):
request(keyname)
def post(self, keyname):
key = self.request.get('key')
if key:
memcache.set(keyname, key, namespace='keymaster')
if len(_keys[keyname]) > 2:
_keys[keyname][2]()
def Handler(keys):
global _keys
_keys = keys
return _Handler