forked from ptarjan/relmeauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
188 lines (151 loc) · 6.77 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import os
import re
import wsgiref.handlers
from google.appengine.ext import webapp
from django.utils import simplejson
from BeautifulSoup import BeautifulSoup
import urllib2
import oauth.consumer
import oauth.db.appengine
import config
import model
callback = "http://%s/twitter/callback" % os.environ['HTTP_HOST']
twitter = oauth.consumer.Twitter(config.twitter.CONSUMER_KEY, config.twitter.CONSUMER_SECRET, callback, db=oauth.db.appengine)
callback = "http://%s/yahoo/callback" % os.environ['HTTP_HOST']
yahoo = oauth.consumer.Yahoo(config.yahoo.CONSUMER_KEY, config.yahoo.CONSUMER_SECRET, callback, db=oauth.db.appengine)
callback = "http://%s/google/callback" % os.environ['HTTP_HOST']
google = oauth.consumer.Google(config.google.CONSUMER_KEY, config.google.CONSUMER_SECRET, callback, db=oauth.db.appengine, scope="http://www-opensocial.googleusercontent.com/api/people/")
class MainHandler(webapp.RequestHandler):
def get(self):
r = '<html><head><meta name="google-site-verification" content="FwxmHGqAai6mpX3d-gZB2DidmXlJJ1tbVzh9e31lzso" /></head><body><form action="relmeauth">Sign In:<input name="url"/><input type="submit"></form>'
self.response.out.write(r)
def checkBacklink(href, url):
page = urllib2.urlopen(href).read()
soup = BeautifulSoup(page)
othermes = soup.findAll(rel=re.compile(r'\bme\b'))
for otherme in othermes :
if otherme['href'] == url :
return True
return False
def getRelMe(url):
builtins = (
('twitter.com', twitter, '/twitter'),
# ('yahoo.com', yahoo, '/yahoo'),
('google.com', google, '/google'),
)
for prefix, app, endpoint in builtins:
if prefix in url:
return endpoint
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page)
mes = soup.findAll(rel=re.compile(r'\bme\b'))
for me in mes :
href = me['href']
for prefix, app, endpoint in builtins:
if prefix in href:
if checkBacklink(href, url):
return endpoint, href
return False
class RelMeAuth(webapp.RequestHandler):
def get(self):
url = self.request.get('url')
endpoint, href = getRelMe(url)
if endpoint:
session = model.Session(url=url)
session.put()
self.response.headers.add_header(
'Set-Cookie',
'session=%s; expires=Fri, 31-Dec-2038 23:59:59 GMT' \
% session.key().id())
self.redirect(endpoint)
return
self.response.out.write('Please put a rel="me" on your page to your twitter, yahoo, or google acount')
class TwitterHandler(webapp.RequestHandler):
def get(self):
user, url = twitter.start()
self.redirect(url)
self.response.headers.add_header(
'Set-Cookie',
'twitter=%s; expires=Fri, 31-Dec-2038 23:59:59 GMT' \
% user.get_key().encode())
class TwitterCallbackHandler(webapp.RequestHandler):
def get(self):
token = self.request.get("oauth_token")
verifier = self.request.get("oauth_verifier", None)
user = self.request.cookies.get("twitter")
twitter.verify(user, token, verifier)
session_id = int(self.request.cookies.get("session"))
session = model.Session.get_by_id(session_id)
if not session.url.startswith("http://twitter.com"):
result = twitter.fetch("http://twitter.com/account/verify_credentials.json", user).read()
url = simplejson.loads(result)['url']
if url != session.url :
self.response.out.write("EVIL HACKER %s != %s" % (url, session.url))
return
self.redirect("/test")
class YahooHandler(webapp.RequestHandler):
def get(self):
user, url = yahoo.start()
self.redirect(url)
self.response.headers.add_header(
'Set-Cookie',
'yahoo=%s; expires=Fri, 31-Dec-2038 23:59:59 GMT' \
% user.get_key().encode())
class YahooCallbackHandler(webapp.RequestHandler):
def get(self):
token = self.request.get("oauth_token")
verifier = self.request.get("oauth_verifier", None)
user = self.request.cookies.get("yahoo")
yahoo.verify(user, token, verifier)
self.redirect("/test")
class GoogleHandler(webapp.RequestHandler):
def get(self):
user, url = google.start()
self.redirect(url)
self.response.headers.add_header(
'Set-Cookie',
'google=%s; expires=Fri, 31-Dec-2038 23:59:59 GMT' \
% user.get_key().encode())
class GoogleCallbackHandler(webapp.RequestHandler):
def get(self):
token = self.request.get("oauth_token")
verifier = self.request.get("oauth_verifier", None)
user = self.request.cookies.get("google")
google.verify(user, token, verifier)
session_id = int(self.request.cookies.get("session"))
session = model.Session.get_by_id(session_id)
if not session.url.startswith("http://www.google.com"):
result = google.fetch("http://www-opensocial.googleusercontent.com/api/people/@me", user).read()
gid = simplejson.loads(result)['entry']['id']
url = "http://www.gogole.com/profiles/" + gid
try:
fetched = urllib2.urlopen(url)
except urllib2.HTTPError:
self.response.out.write("EVIL HACKER No profile @ %s" % (url))
return
url = fetched.geturl()
if url != getRelMe(session.url)[1] :
self.response.out.write("EVIL HACKER %s != %s" % (url, getRelMe(session.url)[1]))
return
self.redirect("/test")
class TestHandler(webapp.RequestHandler):
def get(self):
session_id = int(self.request.cookies.get("session"))
session = model.Session.get_by_id(session_id)
self.response.out.write("You are " + session.url)
def main():
application = webapp.WSGIApplication([
('/', MainHandler),
('/relmeauth', RelMeAuth),
('/twitter', TwitterHandler),
('/twitter/callback', TwitterCallbackHandler),
('/yahoo', YahooHandler),
('/yahoo/callback', YahooCallbackHandler),
('/google', GoogleHandler),
('/google/callback', GoogleCallbackHandler),
('/test', TestHandler),
],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()