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