Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 87 lines (74 sloc) 2.849 kB
ef710cc @progrium initial commit
authored
1 from google.appengine.ext import webapp
2 from google.appengine.ext import db
3 from google.appengine.ext.webapp import util, template
4 from google.appengine.api import memcache
5 from google.appengine.api import urlfetch
6 from django.utils import simplejson
7 from gdata.apps.service import AppsService
8 import logging
9 import base64
267767e @progrium getting it all working again
authored
10 import urllib
ef710cc @progrium initial commit
authored
11
12 DOMAIN = 'hackerdojo.com'
13
14 # Hacker Dojo Domain API helper with caching
15 def dojo(path, force=False):
16 base_url = 'http://domain.hackerdojo.com'
17 cache_ttl = 3600
18 resp = memcache.get(path)
19 if force or not resp:
20 resp = urlfetch.fetch(base_url + path, deadline=10)
21 try:
22 resp = simplejson.loads(resp.content)
23 except Exception, e:
24 resp = []
25 cache_ttl = 10
26 memcache.set(path, resp, cache_ttl)
27 return resp
28
267767e @progrium getting it all working again
authored
29 class MacAddressMapping(db.Model):
30 address = db.StringProperty()
31 username = db.StringProperty()
32 created = db.DateTimeProperty(auto_now_add=True)
33
34 @classmethod
35 def get_by_mac(cls, address):
36 return cls.all().filter('address =', address).get()
37
ef710cc @progrium initial commit
authored
38 class MainHandler(webapp.RequestHandler):
267767e @progrium getting it all working again
authored
39 def get(self, data):
ef710cc @progrium initial commit
authored
40 try:
267767e @progrium getting it all working again
authored
41 data = urllib.unquote(data)
ef710cc @progrium initial commit
authored
42 mac, redirect = base64.b64decode(data).split(',')
43 self.response.out.write(template.render('templates/main.html', {
44 'redirect': redirect,
45 'mac': mac,
267767e @progrium getting it all working again
authored
46 'failure': self.request.get('fail')
ef710cc @progrium initial commit
authored
47 }))
48 except:
49 self.error(400)
267767e @progrium getting it all working again
authored
50 self.response.out.write("bad request")
ef710cc @progrium initial commit
authored
51
52 def post(self, data):
53 client = AppsService(domain=DOMAIN)
267767e @progrium getting it all working again
authored
54 data = urllib.unquote(data)
ef710cc @progrium initial commit
authored
55 try:
56 client.ClientLogin('%s@%s' % (self.request.get('username'), DOMAIN), self.request.get('password'))
57 if not dojo('/users/%s' % self.request.get('username'))['suspended']:
267767e @progrium getting it all working again
authored
58 m = MacAddressMapping(address=self.request.get('mac'), username=self.request.get('username'))
59 m.put()
60 self.redirect(self.request.get('redirect') or 'http://hackerdojo.com')
ef710cc @progrium initial commit
authored
61 else:
62 raise Exception()
63 except:
267767e @progrium getting it all working again
authored
64 self.redirect('/%s?fail=1' % data)
ef710cc @progrium initial commit
authored
65
66
67 class MacHandler(webapp.RequestHandler):
68 def get(self, mac):
267767e @progrium getting it all working again
authored
69 mapping = MacAddressMapping.get_by_mac(mac)
70 if not mapping or dojo('/users/%s' % mapping.username)['suspended']:
ef710cc @progrium initial commit
authored
71 self.error(404)
72 self.response.out.write("not found")
73 else:
267767e @progrium getting it all working again
authored
74 self.response.out.write(mapping.username)
ef710cc @progrium initial commit
authored
75
76
77 def main():
78 application = webapp.WSGIApplication([
79 ('/api/mac/(.+)', MacHandler),
80 ('/(.+)', MainHandler),],
81 debug=True)
82 util.run_wsgi_app(application)
83
84
85 if __name__ == '__main__':
86 main()
Something went wrong with that request. Please try again.