This repository has been archived by the owner on Feb 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oid.py
executable file
·66 lines (62 loc) · 2.22 KB
/
oid.py
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
#!/usr/bin/env python2.7
from werkzeug.wrappers import Request, Response
from werkzeug.urls import Href
from openid.consumer.consumer import Consumer, SUCCESS, CANCEL, SETUP_NEEDED
from openid.sreg import SRegResponse
from get_html import get_html, choose_lang
import SQLcon
@Request.application
def run(request):
lang = choose_lang(request)
sid = request.args.get("sid")
consumer = Consumer({"sid": sid}, None)
href = Href(request.url)
url = href("../oid.py", {"sid": sid})
# try:
info = consumer.complete(request.args, url) # <-- It crashes here. (When using an OpenID from Yahoo!)
#print info.status
# except Exception as e:
# info = openid.consumer.consumer.Response()
# info.status = e
if info.status == CANCEL:
return Response(get_html("oid_failure", lang), 401, mimetype="text/html")
if info.status == SETUP_NEEDED:
html = get_html("oid_setup_needed", lang)
html = html.replace("<!-- URL -->", info.setup_url)
return Response(html, 423, mimetype="text/html")
if info.status == SUCCESS:
display_identifier = info.getDisplayIdentifier()
sregresp = SRegResponse.fromSuccessResponse(info)
realoid = display_identifier
if info.endpoint.canonicalID:
realoid = info.endpoint.canonicalID
try:
nickname = sregresp.data["nickname"]
except (AttributeError, KeyError):
nickname = ""
try:
email = sregresp.data["email"]
except (AttributeError, KeyError):
email = ""
con = SQLcon.con()
cur = con.cursor()
cur.execute("SELECT * FROM users WHERE openid=%s", (realoid,))
result = cur.fetchall()
if result.__len__() == 0:
cur.execute("INSERT INTO users (username, openid, email, first_login) VALUES (%s, %s, %s, true)", (nickname, realoid, email))
#log in
cur.execute("SELECT uid FROM users WHERE openid=%s", (realoid,))
result = cur.fetchall()
uid = result[0][0]
#print result
cur.execute("UPDATE sessions SET uid=%s WHERE sid=%s", (str(uid), sid))
cur.execute("UPDATE sessions SET oid=%s WHERE sid=%s", (realoid, sid))
con.close()
return Response(get_html("oid_success", lang), 200, mimetype="text/html")
#Something went wrong.
#TODO: More investigation.
return Response(get_html("oid_failure", lang), 500, mimetype="text/html")
if __name__ == "__main__":
import CGI
CGI.app = run
CGI.run()