annotate example/persona.py @ 33:67439bcdac48

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