Permalink
Browse files

Verify user credentials and populate userinfo on user login

  • Loading branch information...
1 parent 2e71580 commit b0fc1a3e46432aca69f0848818e759f627e2241c @vsr committed Mar 6, 2012
Showing with 56 additions and 9 deletions.
  1. +30 −7 twitter_users/backends.py
  2. +6 −0 twitter_users/oauth.py
  3. +1 −0 twitter_users/settings.py
  4. +19 −2 twitter_users/views.py
View
37 twitter_users/backends.py
@@ -1,24 +1,47 @@
+from datetime import datetime
from django.contrib.auth.models import User
from twitter_users.models import TwitterInfo
from twitter_users import settings
class TwitterBackend(object):
- def authenticate(self, twitter_id=None, username=None, token=None, secret=None):
+ def authenticate(self, twitter_id=None, screen_name=None, token=None, secret=None, user_info=None):
# find or create the user
+
+ def copy_info(user_info, info):
+ # There must be a better way of doing this. I don't know :$
+ if user_info['lang']: info.lang = user_info['lang']
+ if user_info['created_at']: info.joined = datetime.strptime(user_info['created_at'], "%a %b %d %H:%M:%S +0000 %Y")
+ if user_info['url']: info.url = user_info['url']
+ if user_info['profile_image_url']: info.profile_image = user_info['profile_image_url']
+ if user_info['protected']: info.protected = user_info['protected']
+ if user_info['name']: info.name = user_info['name']
+ if user_info['description']: info.description = user_info['description']
+ if user_info['statuses_count']: info.statuses_count = user_info['statuses_count']
+ if user_info['favourites_count']: info.favourites_count = user_info['favourites_count']
+ if user_info['followers_count']: info.followers_count = user_info['followers_count']
+ if user_info['friends_count']: info.friends_count = user_info['friends_count']
+ if user_info['utc_offset']: info.utc_offset = user_info['utc_offset']
+ if user_info['location']: info.location = user_info['location']
+ if user_info['time_zone']: info.time_zone = user_info['time_zone']
+
try:
info = TwitterInfo.objects.get(id=twitter_id)
# make sure the screen name is current
- if info.name != username:
- info.name = username
- info.save()
+ if info.screen_name != screen_name:
+ info.screen_name = screen_name
+ if user_info:
+ copy_info(user_info, info)
+ info.save()
user = info.user
except TwitterInfo.DoesNotExist:
- email = "%s@twitter.com" % username
- user = User.objects.create_user(settings.USERS_FORMAT % username, email)
+ email = "%s@twitter.com" % screen_name
+ user = User.objects.create_user(settings.USERS_FORMAT % screen_name, email)
user.save()
- info = TwitterInfo(user=user, name=username, id=twitter_id, token=token, secret=secret)
+ info = TwitterInfo(user=user, screen_name=screen_name, id=twitter_id, token=token, secret=secret)
+ if user_info:
+ copy_info(user_info, info)
info.save()
return user
View
6 twitter_users/oauth.py
@@ -17,6 +17,12 @@
class Consumer(oauth2.Consumer):
pass
+class AuthToken(oauth2.Token):
+ pass
+
+class Client(oauth2.Client):
+ pass
+
class Token(object):
def __init__(self, consumer):
self.consumer = consumer
View
1 twitter_users/settings.py
@@ -16,3 +16,4 @@
'twitter_users.models.UserProfile')
USERS_FORMAT = getattr(settings, 'TWITTER_USERS_FORMAT', '%s')
+VERIFY_CREDENTIALS = 'https://api.twitter.com/1/account/verify_credentials.json'
View
21 twitter_users/views.py
@@ -1,5 +1,7 @@
import re
+import urllib
+import json
from django.core.urlresolvers import reverse, NoReverseMatch
from django.http import HttpResponseRedirect
@@ -45,12 +47,27 @@ def twitter_callback(request):
# get an access token from Twitter
consumer = oauth.Consumer(settings.KEY, settings.SECRET)
access_token = oauth.AccessToken(consumer, oauth_token, oauth_verifier)
+
+ oauth_token = oauth.AuthToken(access_token.token, access_token.secret)
+ oauth_consumer = oauth.Consumer(settings.KEY, settings.SECRET)
+ oauth_client = oauth.Client(oauth_consumer, oauth_token)
+ # get user information
+ data = {"skip_status": True, "include_entities": False }
+ verify_uri = settings.VERIFY_CREDENTIALS
+ resp, content = oauth_client.request(verify_uri, 'GET', urllib.urlencode(data))
+
+ if resp.status != 200:
+ # failed to get user info, should actually check for 401
+ return HttpResponseRedirect(settings.LOGOUT_REDIRECT_URL)
+ user_info = json.loads(content)
+
# actually log in
user = authenticate(twitter_id = access_token.user_id,
- username = access_token.username,
+ screen_name = access_token.username,
token = access_token.token,
- secret = access_token.secret)
+ secret = access_token.secret,
+ user_info = user_info)
login(request, user)
# redirect to the authenticated view

0 comments on commit b0fc1a3

Please sign in to comment.