forked from mattwesthoff/bottle_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_openid.py
More file actions
70 lines (56 loc) · 2.23 KB
/
google_openid.py
File metadata and controls
70 lines (56 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from bottle import route, run, request, template, static_file, redirect
from openid.consumer import consumer
from openid.consumer.discover import DiscoveryFailure
from openid.extensions import ax, pape, sreg
@route('/hello/<name>')
def hello(name='person'):
return '<b>Hello %s</b><br />%s' % (name, request.query.to)
@route('/yeah')
def yeah():
return '<html><body><h1>you came from: %s</h1></body></html>' % request.url
@route('/redirect')
def redir():
redirect(request.query.to)
@route('/startlogin')
def startLogin():
c = consumer.Consumer({}, None)
error = None
try:
auth_request = c.begin("https://www.google.com/accounts/o8/id")
except DiscoveryFailure, e:
error = "OpenID discovery error: %s" % (str(e),)
if error:
return error
ax_request = ax.FetchRequest()
ax_request.add(ax.AttrInfo('http://axschema.org/contact/email', required=True))
ax_request.add(ax.AttrInfo('http://axschema.org/namePerson/first', required=True))
ax_request.add(ax.AttrInfo('http://axschema.org/namePerson/last', required=True))
auth_request.addExtension(ax_request)
trust_root = "http://localhost:8080/"
return_to = "http://localhost:8080/endlogin"
if auth_request.shouldSendRedirect():
url = auth_request.redirectURL(trust_root, return_to)
redirect(url)
else:
form_html = auth_request.formMarkup(trust_root, return_to, False, {'id': 'openid_message'})
html = """<html><body onload="document.getElementById('openid_message').submit()">%s</body></html>"""
return html % form_html
@route('/endlogin')
def endLogin():
c = consumer.Consumer({}, None)
return_to = "http://localhost:8080/endlogin"
response = c.complete(request.query, return_to)
if response.status == consumer.SUCCESS:
ax_response = ax.FetchResponse.fromSuccessResponse(response)
if ax_response:
email = ax_response.get('http://axschema.org/contact/email')
first_name = ax_response.get('http://axschema.org/namePerson/first')
last_name = ax_response.get('http://axschema.org/namePerson/last')
output = "email is %s, firstname: %s, lastname: %s" % (email,first_name,last_name)
return output
return "success!"
if response.status == consumer.CANCEL:
return "cancelled"
if response.status == consumer.FAILURE:
return "failure"
run(host='localhost', port=8080)