forked from livid/v2ex-gae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sso.py
101 lines (88 loc) · 4.72 KB
/
sso.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
# coding=utf-8
import os
import re
import time
import datetime
import hashlib
import string
import random
import config
from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from v2ex.babel import Member
from v2ex.babel import Counter
from v2ex.babel import Section
from v2ex.babel import Node
from v2ex.babel import Topic
from v2ex.babel import Reply
from v2ex.babel import Note
from v2ex.babel import SYSTEM_VERSION
from v2ex.babel.security import *
from v2ex.babel.ua import *
from v2ex.babel.da import *
from v2ex.babel.l10n import *
from v2ex.babel.ext.cookies import Cookies
template.register_template_library('v2ex.templatetags.filters')
class SSOV0Handler(webapp.RequestHandler):
def get(self):
site = GetSite()
self.response.headers['Content-type'] = 'application/json'
u = self.request.get('u').strip().lower()
p = self.request.get('p').strip()
failed = '{"ok" : 0}'
if (len(u) > 0) and (len(p) > 0):
q = db.GqlQuery("SELECT * FROM Member WHERE username_lower = :1 AND password = :2", u, p)
if q.count() > 0:
member = q[0]
if member.avatar_mini_url:
if (member.avatar_mini_url[0:1] == '/'):
member.avatar_mini_url = 'http://' + site.domain + member.avatar_mini_url
member.avatar_normal_url = 'http://' + site.domain + member.avatar_normal_url
member.avatar_large_url = 'http://' + site.domain + member.avatar_large_url
else:
member.avatar_mini_url = 'http://' + site.domain + '/static/img/avatar_mini.png'
member.avatar_normal_url = 'http://' + site.domain + '/static/img/avatar_normal.png'
member.avatar_large_url = 'http://' + site.domain + '/static/img/avatar_large.png'
self.response.out.write('{"ok" : 1, "num" : ' + str(member.num) + ', "username" : "' + member.username + '", "username_lower" : "' + member.username_lower + '", "email" : "' + member.email + '", "avatar_mini_url" : "' + member.avatar_mini_url + '", "avatar_normal_url" : "' + member.avatar_normal_url + '", "avatar_large_url" : "' + member.avatar_large_url + '", "created" : ' + str(time.mktime(member.created.timetuple())) + ', "last_modified" : ' + str(time.mktime(member.last_modified.timetuple())) + '}')
else:
self.response.out.write(failed)
else:
self.response.out.write(failed)
class SSOX0Handler(webapp.RequestHandler):
def get(self):
site = GetSite()
self.response.headers['Content-type'] = 'application/json'
x = self.request.get('x').strip()
n = self.request.get('n').strip().lower()
failed = '{"ok" : 0}'
if x == config.ssox:
q = db.GqlQuery("SELECT * FROM Member WHERE username_lower = :1", n)
if q.count() > 0:
member = q[0]
if member.avatar_mini_url:
if (member.avatar_mini_url[0:1] == '/'):
member.avatar_mini_url = 'http://' + site.domain + member.avatar_mini_url
member.avatar_normal_url = 'http://' + site.domain + member.avatar_normal_url
member.avatar_large_url = 'http://' + site.domain + member.avatar_large_url
else:
member.avatar_mini_url = 'http://' + site.domain + '/static/img/avatar_mini.png'
member.avatar_normal_url = 'http://' + site.domain + '/static/img/avatar_normal.png'
member.avatar_large_url = 'http://' + site.domain + '/static/img/avatar_large.png'
self.response.out.write('{"ok" : 1, "num" : ' + str(member.num) + ', "username" : "' + member.username + '", "username_lower" : "' + member.username_lower + '", "email" : "' + member.email + '", "password" : "' + member.password + '", "avatar_mini_url" : "' + member.avatar_mini_url + '", "avatar_normal_url" : "' + member.avatar_normal_url + '", "avatar_large_url" : "' + member.avatar_large_url + '", "created" : ' + str(time.mktime(member.created.timetuple())) + ', "last_modified" : ' + str(time.mktime(member.last_modified.timetuple())) + '}')
else:
self.response.out.write(failed)
else:
self.response.out.write(failed)
def main():
application = webapp.WSGIApplication([
('/sso/v0', SSOV0Handler),
('/sso/x0', SSOX0Handler)
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()