Skip to content

Commit

Permalink
copy original oauth.py
Browse files Browse the repository at this point in the history
  • Loading branch information
setomits committed May 19, 2011
1 parent d0caceb commit 95b952a
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions oauth.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@


from google.appengine.api import memcache from google.appengine.api import memcache
from google.appengine.api import urlfetch from google.appengine.api import urlfetch
from google.appengine.ext import db


from cgi import parse_qs from cgi import parse_qs
from django.utils import simplejson as json from django.utils import simplejson as json
Expand Down Expand Up @@ -93,6 +94,22 @@ def get_oauth_client(service, key, secret, callback_url):
raise Exception, "Unknown OAuth service %s" % service raise Exception, "Unknown OAuth service %s" % service




class AuthToken(db.Model):
"""Auth Token.
A temporary auth token that we will use to authenticate a user with a
third party website. (We need to store the data while the user visits
the third party website to authenticate themselves.)
TODO: Implement a cron to clean out old tokens periodically.
"""

service = db.StringProperty(required=True)
token = db.StringProperty(required=True)
secret = db.StringProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)


class OAuthClient(): class OAuthClient():


def __init__(self, service_name, consumer_key, consumer_secret, request_url, def __init__(self, service_name, consumer_key, consumer_secret, request_url,
Expand Down Expand Up @@ -165,7 +182,7 @@ def make_async_request(self, url, token="", secret="", additional_params=None,
A urlfetch response object is returned. A urlfetch response object is returned.
""" """

payload = self.prepare_request(url, token, secret, additional_params, payload = self.prepare_request(url, token, secret, additional_params,
method) method)


Expand All @@ -183,7 +200,7 @@ def make_async_request(self, url, token="", secret="", additional_params=None,


def make_request(self, url, token="", secret="", additional_params=None, def make_request(self, url, token="", secret="", additional_params=None,
protected=False, method=urlfetch.GET, headers={}): protected=False, method=urlfetch.GET, headers={}):

return self.make_async_request(url, token, secret, additional_params, return self.make_async_request(url, token, secret, additional_params,
protected, method, headers).get_result() protected, method, headers).get_result()


Expand All @@ -210,8 +227,19 @@ def get_user_info(self, auth_token, auth_verifier=""):
auth_secret = memcache.get(self._get_memcache_auth_key(auth_token)) auth_secret = memcache.get(self._get_memcache_auth_key(auth_token))


if not auth_secret: if not auth_secret:
logging.error("There is no auth_secret for %s. Please get auth_token and auth_secret again.") result = AuthToken.gql("""
raise Exception, "There is no auth_secret" WHERE
service = :1 AND
token = :2
LIMIT
1
""", self.service_name, auth_token).get()

if not result:
logging.error("The auth token %s was not found in our db" % auth_token)
raise Exception, "Could not find Auth Token in database"
else:
auth_secret = result.secret


response = self.make_request(self.access_url, response = self.make_request(self.access_url,
token=auth_token, token=auth_token,
Expand All @@ -231,8 +259,9 @@ def get_user_info(self, auth_token, auth_verifier=""):
def _get_auth_token(self): def _get_auth_token(self):
"""Get Authorization Token. """Get Authorization Token.
Actually gets the authorization token and secret from the service, Actually gets the authorization token and secret from the service. The
and the auth token is returned. token and secret are stored in our database, and the auth token is
returned.
""" """


response = self.make_request(self.request_url) response = self.make_request(self.request_url)
Expand All @@ -241,8 +270,11 @@ def _get_auth_token(self):
auth_token = result["token"] auth_token = result["token"]
auth_secret = result["secret"] auth_secret = result["secret"]


self.auth_token = result["token"] # Save the auth token and secret in our database.
self.auth_secret = result["secret"] auth = AuthToken(service=self.service_name,
token=auth_token,
secret=auth_secret)
auth.put()


# Add the secret to memcache as well. # Add the secret to memcache as well.
memcache.set(self._get_memcache_auth_key(auth_token), auth_secret, memcache.set(self._get_memcache_auth_key(auth_token), auth_secret,
Expand Down Expand Up @@ -503,7 +535,8 @@ def _lookup_user_info(self, access_token, access_secret):
user_info["country"] = data["country"] user_info["country"] = data["country"]


return user_info return user_info



class LinkedInClient(OAuthClient): class LinkedInClient(OAuthClient):
"""LinkedIn Client. """LinkedIn Client.
Expand Down Expand Up @@ -548,7 +581,7 @@ def _lookup_user_info(self, access_token, access_secret):
data = json.loads(response.content) data = json.loads(response.content)
user_info = self._get_default_user_info() user_info = self._get_default_user_info()
user_info["id"] = data["id"] user_info["id"] = data["id"]
user_info["picture"] = data.get("pictureUrl", "") user_info["picture"] = data["pictureUrl"]
user_info["name"] = data["firstName"] + " " + data["lastName"] user_info["name"] = data["firstName"] + " " + data["lastName"]
return user_info return user_info


Expand Down

0 comments on commit 95b952a

Please sign in to comment.