annotate example/wsgibrowserid.py @ 37:6b5946843928

example/persona.html example/wsgibrowserid.py
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 27 Dec 2013 13:58:22 -0800
parents
children 4dfffa966edd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 test Mozilla persona auth
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 https://login.persona.org/about
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 """
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import browserid
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import optparse
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import os
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import sys
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 from webob import Request, Response, exc
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 from wsgiref import simple_server
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 here = os.path.dirname(os.path.realpath(__file__))
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 page = os.path.join(here, 'persona.html')
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 class PersonaTest(object):
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 def __init__(self, page=page):
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 self.page = page
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 assert os.path.exists(self.page), "File '%s' not found" % self.page
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 self.page = open(self.page, 'r').read()
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 def __call__(self, environ, start_response):
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 request = Request(environ)
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 method = request.method
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 if method == 'GET':
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 content_type = 'text/html'
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 body = self.page
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 elif method == 'POST':
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 content_type = 'text/plain'
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 assertion = request.POST.get('assertion')
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 print ("Assertion: %s" % assertion)
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 body = '\n'.join(['%s: %s' % (key, environ[key])
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 for key in sorted(environ.keys())])
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 else:
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 content_type = 'text/plain'
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 body = 'Try GET or POST to do something interesting (How did you get here?)'
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 start_response("200 OK", [('Content-Type', content_type),
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 ('Content-Length', str(len(body)))])
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 return [body]
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 def main(args=sys.argv[1:]):
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 usage = '%prog [options]'
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 parser = optparse.OptionParser(usage=usage, description=__doc__)
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 parser.add_option('-p', '--port', dest='port',
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 type='int', default=8123,
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 help="port to serve on")
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 options, args = parser.parse_args(args)
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 app = PersonaTest()
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 server = simple_server.make_server('127.0.0.1', options.port, app)
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 print 'server on\nhttp://localhost:%d/' % options.port
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 try:
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 server.serve_forever()
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 except KeyboardInterrupt:
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 pass
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 if __name__ == '__main__':
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 main()