Skip to content
Browse files

copy original oauth.py

  • Loading branch information...
1 parent d0caceb commit 95b952af4e48011467e85f2828bceadffff95232 @setomits setomits committed
Showing with 43 additions and 10 deletions.
  1. +43 −10 oauth.py
View
53 oauth.py
@@ -45,6 +45,7 @@
from google.appengine.api import memcache
from google.appengine.api import urlfetch
+from google.appengine.ext import db
from cgi import parse_qs
from django.utils import simplejson as json
@@ -93,6 +94,22 @@ def get_oauth_client(service, key, secret, callback_url):
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():
def __init__(self, service_name, consumer_key, consumer_secret, request_url,
@@ -165,7 +182,7 @@ def make_async_request(self, url, token="", secret="", additional_params=None,
A urlfetch response object is returned.
"""
-
+
payload = self.prepare_request(url, token, secret, additional_params,
method)
@@ -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,
protected=False, method=urlfetch.GET, headers={}):
-
+
return self.make_async_request(url, token, secret, additional_params,
protected, method, headers).get_result()
@@ -210,8 +227,19 @@ def get_user_info(self, auth_token, auth_verifier=""):
auth_secret = memcache.get(self._get_memcache_auth_key(auth_token))
if not auth_secret:
- logging.error("There is no auth_secret for %s. Please get auth_token and auth_secret again.")
- raise Exception, "There is no auth_secret"
+ result = AuthToken.gql("""
+ 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,
token=auth_token,
@@ -231,8 +259,9 @@ def get_user_info(self, auth_token, auth_verifier=""):
def _get_auth_token(self):
"""Get Authorization Token.
- Actually gets the authorization token and secret from the service,
- and the auth token is returned.
+ Actually gets the authorization token and secret from the service. The
+ token and secret are stored in our database, and the auth token is
+ returned.
"""
response = self.make_request(self.request_url)
@@ -241,8 +270,11 @@ def _get_auth_token(self):
auth_token = result["token"]
auth_secret = result["secret"]
- self.auth_token = result["token"]
- self.auth_secret = result["secret"]
+ # Save the auth token and secret in our database.
+ auth = AuthToken(service=self.service_name,
+ token=auth_token,
+ secret=auth_secret)
+ auth.put()
# Add the secret to memcache as well.
memcache.set(self._get_memcache_auth_key(auth_token), auth_secret,
@@ -503,7 +535,8 @@ def _lookup_user_info(self, access_token, access_secret):
user_info["country"] = data["country"]
return user_info
-
+
+
class LinkedInClient(OAuthClient):
"""LinkedIn Client.
@@ -548,7 +581,7 @@ def _lookup_user_info(self, access_token, access_secret):
data = json.loads(response.content)
user_info = self._get_default_user_info()
user_info["id"] = data["id"]
- user_info["picture"] = data.get("pictureUrl", "")
+ user_info["picture"] = data["pictureUrl"]
user_info["name"] = data["firstName"] + " " + data["lastName"]
return user_info

0 comments on commit 95b952a

Please sign in to comment.
Something went wrong with that request. Please try again.