Created
July 20, 2012 19:54
-
-
Save mlynch/3152856 to your computer and use it in GitHub Desktop.
BaseDriveHandler modifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseDriveHandler(w2.RequestHandler): | |
... | |
def StoreUserIdInSession(self, userid): | |
session = sessions.LilCookies(self, SESSION_SECRET) | |
session.set_secure_cookie(name='userid', value=userid) | |
def GetUserIdFromSession(self): | |
session = sessions.LilCookies(self, SESSION_SECRET) | |
return session.get_secure_cookie(name='userid') | |
def GetStoredCreds(self, userid): | |
return StorageByKeyName(Credentials, userid, 'credentials').get() | |
def StoreCreds(self, creds, userid): | |
StorageByKeyName(Credentials, userid, 'credentials').put(creds) | |
def GetActiveCredential(self): | |
userid = self.GetUserIdFromSession() | |
code = self.request.get('code') | |
creds = None | |
if userid: | |
creds = self.GetStoredCreds(userid) | |
if (not creds or not creds.refresh_token) and code: | |
oauth_flow = self.CreateOAuthFlow() | |
# Perform the exchange of the code. If there is a failure with exchanging | |
# the code, return None. | |
try: | |
creds = oauth_flow.step2_exchange(code) | |
except: | |
return None | |
if creds: | |
users_service = CreateService('oauth2', 'v2', creds) | |
# Make a call against the userinfo service to retrieve the user's information. | |
# In this case we are interested in the user's "id" field. | |
try: | |
userid = users_service.userinfo().get().execute().get('id') | |
except: | |
return None | |
self.StoreUserIdInSession(userid) | |
if creds.refresh_token: | |
self.StoreCreds(creds, userid) | |
if not creds or not creds.refresh_token: | |
return None | |
return creds | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment