def authenticate(self, username, cookies): API_KEY = settings.FACEBOOK_API_KEY API_SECRET = settings.FACEBOOK_API_SECRET REST_SERVER = 'http://api.facebook.com/restserver.php' if API_KEY in cookies: signature_hash = get_facebook_signature(API_KEY, API_SECRET, cookies, True) if(signature_hash == cookies[API_KEY]) and (datetime.fromtimestamp(float(cookies[API_KEY+'_expires'])) > datetime.now()): user_info_response = get_user_info(API_KEY, API_SECRET, cookies) username_ = 'facebook_%s' % user_info_response[0]['first_name'] if not username == username_: return None try: user = User.objects.get(username = username) return user except User.DoesNotExist: user_email = '%s@facebookuser.%s.com'%(user_info_response[0]['first_name'], settings.SITE_NAME) user_pass = ''.join([random.choice('abcdefghijklmnopqrstuvwxyz') for i in xrange(8)]) user = User.objects.create(username = username, email=user_email, password=user_pass) user.first_name = user_info_response[0]['first_name'] user.last_name = user_info_response[0]['last_name'] auth_meta = AuthMeta(user=user, provider='Facebook').save() return user else: return None else: return None
def authenticate(self, cookies): API_KEY = FACEBOOK_API_KEY API_SECRET = FACEBOOK_API_SECRET REST_SERVER = FACEBOOK_REST_SERVER if API_KEY in cookies: signature_hash = get_facebook_signature(API_KEY, API_SECRET, cookies, True) if(signature_hash == cookies[API_KEY]) and (datetime.fromtimestamp(float(cookies[API_KEY+'_expires'])) > datetime.now()): user_info_response = get_user_info(API_KEY, API_SECRET, cookies) username = user_info_response[0]['first_name'] try: profile = FacebookUserProfile.objects.get(facebook_uid = user_info_response[0]['uid']) return profile.user except FacebookUserProfile.DoesNotExist: fb_data = user_info_response[0] name_count = AuthUser.objects.filter(username__istartswith = username).count() if name_count: username = '******' % (username, name_count + 1) #user_email = '%s@facebookuser.%s.com'%(user_info_response[0]['first_name'], settings.SITE_NAME) user = User.objects.create(username = username) user.first_name = fb_data['first_name'] user.last_name = fb_data['last_name'] user.save() location = str(fb_data['current_location']) fb_profile = FacebookUserProfile(facebook_uid = fb_data['uid'], user = user, profile_image_url = fb_data['pic_small'], location=location) fb_profile.save() auth_meta = AuthMeta(user=user, provider='Facebook').save() return user else: return None else: return None
def facebook_login_done(request): API_KEY = settings.FACEBOOK_API_KEY API_SECRET = settings.FACEBOOK_API_SECRET REST_SERVER = "http://api.facebook.com/restserver.php" # FB Connect will set a cookie with a key == FB App API Key if the user has been authenticated if API_KEY in request.COOKIES: signature_hash = get_facebook_signature(API_KEY, API_SECRET, request.COOKIES, True) # The hash of the values in the cookie to make sure they're not forged # AND If session hasn't expired if (signature_hash == request.COOKIES[API_KEY]) and ( datetime.fromtimestamp(float(request.COOKIES[API_KEY + "_expires"])) > datetime.now() ): # Log the user in now. user_info_response = get_user_info(API_KEY, API_SECRET, request.COOKIES) username = "******" % user_info_response[0]["first_name"] session_key = (request.COOKIES[API_KEY + "_session_key"],) user = authenticate(username=username, cookies=request.COOKIES) # if user is authenticated then login user if user: login(request, user) else: # Delete cookies and redirect to main Login page. del request.COOKIES[API_KEY + "_session_key"] del request.COOKIES[API_KEY + "_user"] return HttpResponseRedirect(reverse("socialauth_login_page")) return HttpResponseRedirect(reverse("socialauth_signin_complete"))
def authenticate(self, cookies): API_KEY = FACEBOOK_API_KEY API_SECRET = FACEBOOK_API_SECRET REST_SERVER = FACEBOOK_REST_SERVER if API_KEY in cookies: signature_hash = get_facebook_signature(API_KEY, API_SECRET, cookies, True) if (signature_hash == cookies[API_KEY]) and ( datetime.fromtimestamp(float( cookies[API_KEY + '_expires'])) > datetime.now()): user_info_response = get_user_info(API_KEY, API_SECRET, cookies) username = user_info_response[0]['first_name'] try: user_profile = FacebookUserProfile.objects.get( user__is_active=True, facebook_uid=user_info_response[0]['uid']) if user_profile.user.is_active: return user_profile.user else: return except FacebookUserProfile.DoesNotExist: fb_data = user_info_response[0] name_count = AuthUser.objects.filter( username__istartswith=username).count() if name_count: username = '******' % (username, name_count + 1) #user_email = '%s@facebookuser.%s.com'%(user_info_response[0]['first_name'], settings.SITE_NAME) user = User.objects.create(username=username) user.first_name = fb_data['first_name'] user.last_name = fb_data['last_name'] user.save() location = str(fb_data['current_location']) fb_profile = FacebookUserProfile( facebook_uid=fb_data['uid'], user=user, profile_image_url=fb_data['pic_small'], location=location) fb_profile.save() AuthMeta(user=user, provider='Facebook').save() return user else: return None else: return None