Mercurial > hg > bitsyauth
annotate example/persona.py @ 35:d95ac066cb62
example/persona.py
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 27 Dec 2013 13:45:10 -0800 |
parents | 119cf9c81d3d |
children | afeb01315ab6 |
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 | 9 import optparse |
10 import os | |
11 import sys | |
34
119cf9c81d3d
example/persona.html example/persona.py
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
12 from webob import Request, Response, exc |
25 | 13 from wsgiref import simple_server |
14 | |
23
21124b238feb
stub persona/browserid example
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 here = os.path.dirname(os.path.realpath(__file__)) |
21124b238feb
stub persona/browserid example
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 page = os.path.join(here, 'persona.html') |
21124b238feb
stub persona/browserid example
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 |
25 | 18 class PersonaTest(object): |
27 | 19 def __init__(self, page=page): |
20 self.page = page | |
26 | 21 assert os.path.exists(self.page), "File '%s' not found" % self.page |
22 self.page = open(self.page, 'r').read() | |
23 def __call__(self, environ, start_response): | |
35 | 24 request = Request(environ) |
25 method = request.method | |
30
0bf52646061b
example/persona.html example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
26 if method == 'GET': |
31
6af220013aa1
example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
27 content_type = 'text/html' |
30
0bf52646061b
example/persona.html example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
28 body = self.page |
0bf52646061b
example/persona.html example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
29 elif method == 'POST': |
31
6af220013aa1
example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
30 content_type = 'text/plain' |
6af220013aa1
example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
31 body = '\n'.join(['%s: %s' % (key, environ[key]) |
6af220013aa1
example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
32 for key in sorted(environ.keys())]) |
6af220013aa1
example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
33 else: |
6af220013aa1
example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
34 content_type = 'text/plain' |
6af220013aa1
example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
35 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
|
36 start_response("200 OK", [('Content-Type', content_type), |
6af220013aa1
example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
37 ('Content-Length', str(len(body)))]) |
30
0bf52646061b
example/persona.html example/persona.py setup.py
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
38 return [body] |
25 | 39 |
23
21124b238feb
stub persona/browserid example
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
40 def main(args=sys.argv[1:]): |
21124b238feb
stub persona/browserid example
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
41 |
21124b238feb
stub persona/browserid example
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
42 usage = '%prog [options]' |
21124b238feb
stub persona/browserid example
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 parser = optparse.OptionParser(usage=usage, description=__doc__) |
24 | 44 parser.add_option('-p', '--port', dest='port', |
45 type='int', default=8123, | |
46 help="port to serve on") | |
23
21124b238feb
stub persona/browserid example
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
47 options, args = parser.parse_args(args) |
21124b238feb
stub persona/browserid example
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
48 |
27 | 49 app = PersonaTest() |
50 | |
25 | 51 server = simple_server.make_server('127.0.0.1', options.port, app) |
27 | 52 print 'server on\nhttp://localhost:%d/' % options.port |
25 | 53 try: |
54 server.serve_forever() | |
55 except KeyboardInterrupt: | |
56 pass | |
24 | 57 |
23
21124b238feb
stub persona/browserid example
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
58 if __name__ == '__main__': |
21124b238feb
stub persona/browserid example
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
59 main() |