annotate example/wsgibrowserid_example.py @ 53:d9e712cfd098

STUB: bitsyauth/__init__.py
author Jeff Hammel <k0scist@gmail.com>
date Thu, 06 Mar 2014 22:27:26 -0800
parents 8255e769828b
children
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 """
38
4dfffa966edd example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
4 test Mozilla persona auth via wsgibrowserid
37
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
38
4dfffa966edd example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
6 https://github.com/ianb/wsgibrowserid
37
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 optparse
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import os
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import sys
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 from webob import Request, Response, exc
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 from wsgiref import simple_server
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 here = os.path.dirname(os.path.realpath(__file__))
38
4dfffa966edd example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
16 page = os.path.join(here, 'wsgibrowserid.html')
37
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 class PersonaTest(object):
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 def __init__(self, page=page):
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 self.page = page
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 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
22 self.page = open(self.page, 'r').read()
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 def __call__(self, environ, start_response):
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 request = Request(environ)
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 method = request.method
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 if method == 'GET':
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 content_type = 'text/html'
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 body = self.page
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 elif method == 'POST':
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 content_type = 'text/plain'
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 assertion = request.POST.get('assertion')
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 print ("Assertion: %s" % assertion)
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 body = '\n'.join(['%s: %s' % (key, environ[key])
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 for key in sorted(environ.keys())])
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 else:
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 content_type = 'text/plain'
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 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
38 start_response("200 OK", [('Content-Type', content_type),
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 ('Content-Length', str(len(body)))])
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 return [body]
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 def main(args=sys.argv[1:]):
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 usage = '%prog [options]'
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 parser = optparse.OptionParser(usage=usage, description=__doc__)
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 parser.add_option('-p', '--port', dest='port',
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 type='int', default=8123,
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 help="port to serve on")
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 options, args = parser.parse_args(args)
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 app = PersonaTest()
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 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
54 print 'server on\nhttp://localhost:%d/' % options.port
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 try:
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 server.serve_forever()
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 except KeyboardInterrupt:
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 pass
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 if __name__ == '__main__':
6b5946843928 example/persona.html example/wsgibrowserid.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 main()