closed issue 995, thanks Peter

This commit is contained in:
Massimo
2012-09-28 12:38:03 -05:00
parent f7b5922fd9
commit 3ea23a97ed
3 changed files with 38 additions and 29 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.0.9 (2012-09-28 09:25:21) dev Version 2.0.9 (2012-09-28 12:37:56) dev
+36 -27
View File
@@ -2,9 +2,9 @@
# coding: utf8 # coding: utf8
""" """
Dropbox Authentication for web2py Dropbox Authentication for web2py
Developed by Massimo Di Pierro (2012) Developed by Massimo Di Pierro (2012)
Same License as Web2py License Same License as Web2py License
""" """
# mind here session is dropbox session, not current.session # mind here session is dropbox session, not current.session
@@ -27,7 +27,7 @@ class DropboxAccount(object):
key="...", key="...",
secret="...", secret="...",
access_type="...", access_type="...",
url = "http://localhost:8000/%s/default/user/login" % request.application) login_url = "http://localhost:8000/%s/default/user/login" % request.application)
when logged in when logged in
client = auth.settings.login_form.client client = auth.settings.login_form.client
""" """
@@ -40,7 +40,7 @@ class DropboxAccount(object):
login_url = "", login_url = "",
on_login_failure=None, on_login_failure=None,
): ):
self.request=request self.request=request
self.key=key self.key=key
self.secret=secret self.secret=secret
@@ -53,39 +53,48 @@ class DropboxAccount(object):
def get_user(self): def get_user(self):
request = self.request request = self.request
token = current.session.dropbox_token if not current.session.dropbox_request_token:
if not token:
return None
try:
self.sess.set_token(token[0],token[1])
except:
# invalid token, should never happen
return None return None
elif not current.session.dropbox_access_token:
request_token = current.session.dropbox_request_token
self.sess.set_request_token(request_token[0],request_token[1])
access_token = self.sess.obtain_access_token(self.sess.token)
current.session.dropbox_access_token = \
(access_token.key,access_token.secret)
else: else:
user = Storage() access_token = current.session.dropbox_access_token
self.client = client.DropboxClient(self.sess) self.sess.set_token(access_token[0],access_token[1])
data = self.client.account_info()
display_name = data.get('display_name','').split(' ',1)
user = dict(email = data.get('email',None), user = Storage()
first_name = display_name[0], self.client = client.DropboxClient(self.sess)
last_name = display_name[-1], data = self.client.account_info()
registration_id = data.get('uid',None)) display_name = data.get('display_name','').split(' ',1)
if not user['registration_id'] and self.on_login_failure: user = dict(email = data.get('email',None),
redirect(self.on_login_failure) first_name = display_name[0],
return user last_name = display_name[-1],
registration_id = data.get('uid',None))
if not user['registration_id'] and self.on_login_failure:
redirect(self.on_login_failure)
return user
def login_form(self): def login_form(self):
token = self.sess.obtain_request_token()
current.session.dropbox_token = (token.key,token.secret) request_token = self.sess.obtain_request_token()
dropbox_url = self.sess.build_authorize_url(token,self.login_url) current.session.dropbox_request_token = \
(request_token.key,request_token.secret)
dropbox_url = self.sess.build_authorize_url(request_token,
self.login_url)
redirect(dropbox_url) redirect(dropbox_url)
form = IFRAME(_src=dropbox_url, form = IFRAME(_src=dropbox_url,
_scrolling="no", _scrolling="no",
_frameborder="no", _frameborder="no",
_style="width:400px;height:240px;") _style="width:400px;height:240px;")
return form return form
def logout_url(self, next = "/"): def logout_url(self, next = "/"):
current.session.dropbox_token=None current.session.dropbox_request_token=None
current.session.auth=None current.session.auth=None
redirect('https://www.dropbox.com/logout') redirect('https://www.dropbox.com/logout')
return next return next
+1 -1
View File
@@ -136,7 +136,7 @@ def web2py_uuid():
and it XORs the output with the tokens uniquely associated with this machine. and it XORs the output with the tokens uniquely associated with this machine.
""" """
try: try:
bytes = string.join(map(lambda ur, ctoken: chr(random.randrange(256)^ord(ur)^ctoken), os.urandom(16), CTOKENS),'') bytes = string.join(map(lambda ur, ctoken: chr(random.randrange(256)^ord(ur)^ctoken), os.urandom(16),CTOKENS),'')
except NotImplementedError: except NotImplementedError:
bytes = string.join(map(lambda ctoken: chr(random.randrange(256)^ctoken), CTOKENS),'') bytes = string.join(map(lambda ctoken: chr(random.randrange(256)^ctoken), CTOKENS),'')
return str(uuid.UUID(bytes=bytes, version=4)) return str(uuid.UUID(bytes=bytes, version=4))