def authenticate(self, token=None, request=None): """ Reads in a Facebook code and asks Facebook if it's valid and what user it points to. """ args = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_APP_SECRET, 'redirect_uri': request.build_absolute_uri( reverse('facebook_callback')), 'code': token, } # Get a legit access token target = urllib.urlopen( 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)).read() response = cgi.parse_qs(target) access_token = response['access_token'][-1] # Read the user's profile information fb_profile = urllib.urlopen( 'https://graph.facebook.com/me?access_token=%s' % access_token) fb_profile = json.load(fb_profile) try: # Try and find existing user fb_user = UserProfile.objects.get(facebook_id=fb_profile['id']) user = fb_user.user # Update access_token fb_user.access_token = access_token fb_user.save() except UserProfile.DoesNotExist: # No existing user if getattr(settings, 'FACEBOOK_FORCE_SIGNUP', False): # No existing user, use anonymous user = AnonymousUser() user.username = username user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] fb_user = UserProfile( facebook_id=fb_profile['id'], access_token=access_token ) user.facebookprofile = fb_user else: # No existing user, create one user = User.objects.create_user(fb_profile['id'], fb_profile['email']) user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] # with django-primate User has one field called 'name' instead # of first_name and last_name user.name = u'%s %s' % (user.first_name, user.last_name) user.save() # Create the UserProfile fb_user = UserProfile(user=user, facebook_id=fb_profile['id'], access_token=access_token) fb_user.save() return user
def authenticate(self, token=None, request=None): """ Reads in a Facebook code and asks Facebook if it's valid and what user it points to. """ args = { "client_id": settings.FACEBOOK_APP_ID, "client_secret": settings.FACEBOOK_APP_SECRET, "redirect_uri": request.build_absolute_uri("/facebook/authentication_callback"), "code": token, } # Get a legit access token target = urllib.urlopen("https://graph.facebook.com/oauth/access_token?" + urllib.urlencode(args)).read() response = cgi.parse_qs(target) access_token = response["access_token"][-1] # Read the user's profile information fb_profile = urllib.urlopen("https://graph.facebook.com/me?access_token=%s" % access_token) fb_profile = json.load(fb_profile) try: # Try and find existing user fb_user = FacebookProfile.objects.get(facebook_id=fb_profile["id"]) user = fb_user.user # Update access_token fb_user.access_token = access_token fb_user.save() except FacebookProfile.DoesNotExist: # No existing user # Not all users have usernames username = fb_profile.get("username", fb_profile["id"]) if getattr(settings, "FACEBOOK_FORCE_SIGNUP", False): # No existing user, use anonymous user = AnonymousUser() user.username = username user.first_name = fb_profile["first_name"] user.last_name = fb_profile["last_name"] fb_user = FacebookProfile(facebook_id=fb_profile["id"], access_token=access_token) user.facebookprofile = fb_user else: # No existing user, create one try: user = User.objects.create_user(username, fb_profile["email"]) except IntegrityError: # Username already exists, make it unique user = User.objects.create_user(username + fb_profile["id"], fb_profile["email"]) user.first_name = fb_profile["first_name"] user.last_name = fb_profile["last_name"] user.save() # Create the FacebookProfile fb_user = FacebookProfile(user=user, facebook_id=fb_profile["id"], access_token=access_token) fb_user.save() return user
def __call__(self, request): not_anonomous_user = request.META.get('HTTP_X_ANONYMOUS_CONSUMER', True) == 'false' user = AnonymousUser() if not_anonomous_user: user_id = request.META.get('HTTP_X_AUTHENTICATED_USERID', False) user = get_user_model()() user.id = user_id user.username = user_id user.first_name = user_id user.last_name = user_id request.user = user response = self.get_response(request) return response
def authenticate(self, token=None, request=None): """ Reads in a Facebook code and asks Facebook if it's valid and what user it points to. """ args = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_APP_SECRET, 'redirect_uri': request.build_absolute_uri('/facebook/authentication_callback'), 'code': token, } # Get a legit access token target = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)).read() response = cgi.parse_qs(target) access_token = response['access_token'][-1] # Read the user's profile information fb_profile = urllib.urlopen('https://graph.facebook.com/me?access_token=%s' % access_token) fb_profile = json.load(fb_profile) fb_friends = urllib.urlopen('https://graph.facebook.com/me/friends?access_token=%s' % access_token) fb_friends = json.load(fb_friends) try: # Try and find existing user fb_user = FacebookProfile.objects.get(facebook_id=fb_profile['id']) user = fb_user.user # Update access_token fb_user.access_token = access_token fb_user.save() FacebookFriends.objects.filter(user=user).delete() for friend in fb_friends['data']: FacebookFriends.objects.create(user=user,facebook_id=friend['id'],name=friend['name']) except FacebookProfile.DoesNotExist: # No existing user # Not all users have usernames username = fb_profile.get('username', fb_profile['email'].split('@')[0]) if getattr(settings, 'FACEBOOK_FORCE_SIGNUP', False): # No existing user, use anonymous user = AnonymousUser() user.username = username user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] fb_user = FacebookProfile( facebook_id=fb_profile['id'], access_token=access_token ) user.facebookprofile = fb_user else: # No existing user, create one try: user = User.objects.create_user(username, fb_profile['email']) except IntegrityError: # Username already exists, make it unique user = User.objects.create_user(username + fb_profile['id'], fb_profile['email']) user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] user.save() # Create the FacebookProfile fb_user = FacebookProfile(user=user, facebook_id=fb_profile['id'], access_token=access_token) fb_user.save() return user
def authenticate(self, token=None, request=None): """ Reads in a Facebook code and asks Facebook if it's valid and what user it points to. """ #rebuild redirect_uri for user id or next url redirect_uri = request.build_absolute_uri('/facebook/authentication_callback') redirect_args = {} if request.GET.get('next'): redirect_args['next'] = request.GET.get('next') if request.GET.get('user'): redirect_args['user'] = str(request.user.id) if len(redirect_args) != 0: redirect_uri = redirect_uri + '?' + urllib.urlencode(redirect_args) args = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_APP_SECRET, 'redirect_uri': redirect_uri, 'code': token, } # Get a legit access token target = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)).read() response = cgi.parse_qs(target) access_token = response['access_token'][-1] # Read the user's profile information fb_profile = urllib.urlopen('https://graph.facebook.com/me?access_token=%s' % access_token) fb_profile = json.load(fb_profile) #if user is just trying to connect facebook not full login if request.GET.get('user'): user = request.user try: # Try and find existing user fb_user = FacebookProfile.objects.get(facebook_id=fb_profile['id']) user = fb_user.user if request.user.id != user.id: return None except FacebookProfile.DoesNotExist: fb_user = FacebookProfile( user=user, facebook_id=fb_profile['id'], access_token=access_token ) fb_user.save() return user #full login try: # Try and find existing user fb_user = FacebookProfile.objects.get(facebook_id=fb_profile['id']) user = fb_user.user # Update access_token fb_user.access_token = access_token fb_user.save() except FacebookProfile.DoesNotExist: # Not all users have usernames username = fb_profile.get('username', fb_profile['email'].split('@')[0]) if getattr(settings, 'FACEBOOK_FORCE_SIGNUP', False): user = AnonymousUser() user.signup_required = True user.username = username user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] fb_user = FacebookProfile( facebook_id=fb_profile['id'], access_token=access_token ) user.facebookprofile = fb_user else: if getattr(settings, 'FACEBOOK_FORCE_VERIFICATION', False) and \ User.objects.filter(email__iexact=fb_profile['email']).exists(): user = AnonymousUser() user.verification_required = True user.email = fb_profile['email'] user.facebookprofile = FacebookProfile( facebook_id=fb_profile['id'], access_token=access_token ) else: try: user = User.objects.create_user(username, fb_profile['email']) except IntegrityError: # Username already exists, make it unique user = User.objects.create_user(username + fb_profile['id'], fb_profile['email']) user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] user.save() # Create the FacebookProfile fb_user = FacebookProfile(user=user, facebook_id=fb_profile['id'], access_token=access_token) fb_user.save() return user
def authenticate(self, token=None, request=None): """ Reads in a Facebook code and asks Facebook if it's valid and what user it points to. """ args = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_APP_SECRET, 'redirect_uri': request.build_absolute_uri(reverse('facebook-callback')), 'code': token, } # Get a legit access token target = urllib.urlopen( 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)).read() response = cgi.parse_qs(target) access_token = response['access_token'][-1] # Read the user's profile information fb_profile = urllib.urlopen( 'https://graph.facebook.com/me?access_token=%s' % access_token) fb_profile = json.load(fb_profile) try: # Try and find existing user fb_user = FacebookProfile.objects.get(facebook_id=fb_profile['id']) user = fb_user.user # Update access_token fb_user.access_token = access_token fb_user.save() except FacebookProfile.DoesNotExist: # No existing user if getattr(settings, 'FACEBOOK_FORCE_SIGNUP', False): # No existing user, use anonymous user = AnonymousUser() user.username = username user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] fb_user = FacebookProfile(facebook_id=fb_profile['id'], access_token=access_token) user.facebookprofile = fb_user else: # No existing user, create one user = User.objects.create_user(fb_profile['id'], fb_profile['email']) user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] # Facebook allows for longer name. This fixes the inconsistencies between # Django and Postgres if len(user.first_name) > 30: user.first_name = user.first_name[:30] if len(user.last_name) > 30: user.last_name = user.last_name[:30] # with django-primate User has one field called 'name' instead # of first_name and last_name user.name = u'%s %s' % (user.first_name, user.last_name) user.save() # Create the FacebookProfile fb_user = FacebookProfile(user=user, facebook_id=fb_profile['id'], access_token=access_token) fb_user.save() return user
def userregister(request): """ A registration form endpoint for registering and logging in. This view will permit a user to register if their username is unique, their password is not empty, and an email address is provided. This view returns JSON, with a 'success' property if registration or login was successful. If registration was successful, the JSON also contains a 'redirect' property. If registration was unsuccessful, the JSON also contains a 'message' property, describing why the registration failed. Parameters: request -- An HttpRequest, with the form submitted parameters. Returns: A JSON object indicating if registration/login was successful. """ username = request.POST.get('newusername', None) password = request.POST.get('newpassword1', None) email = request.POST.get('email', None) fname = request.POST.get('firstname', None) lname = request.POST.get('lastname', None) hint = request.POST.get('passwordhint', None) org = request.POST.get('organization', None) anonymous = False status = {'success': False} if username != '' and password != '': if (username == 'anonymous' and password == 'anonymous'): user = AnonymousUser() else: name_exists = User.objects.filter(username__exact=username) if name_exists: status['message'] = 'name exists' return HttpResponse(json.dumps(status)) try: User.objects.create_user(username, email, password) except Exception as error: status[ 'message'] = 'Sorry, we weren\'t able to create your account.' return HttpResponse(json.dumps(status)) # authenticate the user, and add additional registration info user = authenticate(username=username, password=password) user.first_name = fname user.last_name = lname user.save() profile = user.profile profile.organization = org profile.pass_hint = hint profile.save() login(request, user) status['success'] = True status['redirect'] = '/districtmapping/plan/0/view/' return HttpResponse(json.dumps(status)) else: status['message'] = 'Username cannot be empty.' return HttpResponse(json.dumps(status))
def userregister(request): """ A registration form endpoint for registering and logging in. This view will permit a user to register if their username is unique, their password is not empty, and an email address is provided. This view returns JSON, with a 'success' property if registration or login was successful. If registration was successful, the JSON also contains a 'redirect' property. If registration was unsuccessful, the JSON also contains a 'message' property, describing why the registration failed. Parameters: request -- An HttpRequest, with the form submitted parameters. Returns: A JSON object indicating if registration/login was successful. """ username = request.POST.get('newusername', None) password = request.POST.get('newpassword1', None) email = request.POST.get('email', None) fname = request.POST.get('firstname', None) lname = request.POST.get('lastname', None) hint = request.POST.get('passwordhint', None) org = request.POST.get('organization', None) anonymous = False status = { 'success':False } if username != '' and password != '': if (username == 'anonymous' and password == 'anonymous'): user = AnonymousUser() else: name_exists = User.objects.filter(username__exact=username) if name_exists: status['message'] ='name exists' return HttpResponse(json.dumps(status), mimetype='application/json') email_exists = email != '' and User.objects.filter(email__exact = email) if email_exists: status['message'] ='email exists' return HttpResponse(json.dumps(status), mimetype='application/json') try: User.objects.create_user(username, email, password) except Exception as error: status['message'] = 'Sorry, we weren\'t able to create your account.' return HttpResponse(json.dumps(status), mimetype='application/json') # authenticate the user, and add additional registration info user = authenticate(username=username, password=password) user.first_name = fname user.last_name = lname user.save() profile = user.get_profile() profile.organization = org profile.pass_hint = hint profile.save() login( request, user ) status['success'] = True status['redirect'] = '/districtmapping/plan/0/view/' return HttpResponse(json.dumps(status), mimetype='application/json') else: status['message'] = 'Username cannot be empty.' return HttpResponse(json.dumps(status), mimetype='application/json')
def authenticate(self, token=None, request=None): """ Reads in a Facebook code and asks Facebook if it's valid and what user it points to. """ args = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_APP_SECRET, 'redirect_uri': request.build_absolute_uri('/facebook/authentication_callback'), 'code': token, } # Get a legit access token target = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)).read() response = cgi.parse_qs(target) access_token = response['access_token'][-1] # Read the user's profile information fb_profile = urllib.urlopen('https://graph.facebook.com/me?access_token=%s' % access_token) fb_profile = json.load(fb_profile) try: # Try and find existing user fb_user = FacebookProfile.objects.get(facebook_id=fb_profile['id']) user = fb_user.user # Update access_token fb_user.access_token = access_token fb_user.save() except FacebookProfile.DoesNotExist: # No existing user # Not all users have usernames username = (fb_profile['first_name']+fb_profile['last_name']).lower() if getattr(settings, 'FACEBOOK_FORCE_SIGNUP', False): # No existing user, use anonymous user = AnonymousUser() user.username = username user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] fb_user = FacebookProfile( facebook_id=fb_profile['id'], access_token=access_token ) user.facebookprofile = fb_user else: # No existing user, create one try: user = User.objects.create_user(username, fb_profile['email']) except IntegrityError: # Username already exists, make it unique user = User.objects.create_user(username + fb_profile['id'], fb_profile['email']) user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] user.save() image_url = 'https://graph.facebook.com/'+fb_profile['id']+'/picture?access_token='+access_token+'&type=large' savepath = 'media/members/'+fb_profile['id']+'.jpg' urllib.urlretrieve(image_url, savepath) #enregistrer l'image dans media/members profile = get_object_or_404(Profile, user=user) profile.email = fb_profile['email'] #profile.city = fb_profile['location']['name'] profile.avatar = 'members/'+fb_profile['id']+'.jpg' profile.save() # Create the FacebookProfile fb_user = FacebookProfile(user=user, facebook_id=fb_profile['id'], access_token=access_token) fb_user.save() return user
def authenticate(self, token=None, request=None): """ Reads in a Facebook code and asks Facebook if it's valid and what user it points to. """ args = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_APP_SECRET, 'redirect_uri': request.build_absolute_uri('/facebook/authentication_callback'), 'code': token, } # Get a legit access token target = urllib.urlopen( 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)).read() response = cgi.parse_qs(target) access_token = response['access_token'][-1] # Read the user's profile information fb_profile = urllib.urlopen( 'https://graph.facebook.com/me?access_token=%s' % access_token) fb_profile = json.load(fb_profile) try: # Try and find existing user fb_user = FacebookProfile.objects.get(facebook_id=fb_profile['id']) user = fb_user.user # Update access_token fb_user.access_token = access_token fb_user.save() except FacebookProfile.DoesNotExist: # No existing user # Not all users have usernames username = fb_profile.get('username', fb_profile['email'].split('@')[0]) if getattr(settings, 'FACEBOOK_FORCE_SIGNUP', False): # No existing user, use anonymous user = AnonymousUser() user.username = username user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] fb_user = FacebookProfile(facebook_id=fb_profile['id'], access_token=access_token) user.facebookprofile = fb_user else: # No existing user, create one try: user = User.objects.create_user(username, fb_profile['email']) except IntegrityError: # Username already exists, make it unique user = User.objects.create_user( username + fb_profile['id'], fb_profile['email']) user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] user.save() # Create the FacebookProfile fb_user = FacebookProfile(user=user, facebook_id=fb_profile['id'], access_token=access_token) fb_user.save() return user
def authenticate(self, token=None, request=None,redirect_uri='/'): """ Reads in a Facebook code and asks Facebook if it's valid and what user it points to. """ args = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_APP_SECRET, 'redirect_uri': request.build_absolute_uri( redirect_uri ), 'code': token, } # Get Model to use appmodel = settings.AUTH_PROFILE_MODULE.split(".") FacebookProfile = get_model(appmodel[0],appmodel[1]) # Get a legit access token target = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)).read() response = cgi.parse_qs(target) try: access_token = response['access_token'][-1] except KeyError: mail_admins("ERR",response) return None # Read the user's profile information fb_profile = urllib.urlopen('https://graph.facebook.com/me?fields=id,first_name,last_name,email,name&access_token=%s' % access_token) fb_profile = json.load(fb_profile) try: # Try and find existing user fb_user = FacebookProfile.objects.get(facebook_id=fb_profile['id']) user = fb_user.user # Update access_token fb_user.access_token = access_token fb_user.save() except FacebookProfile.DoesNotExist: # No existing user # Not all users have usernames username = fb_profile.get('username', fb_profile['email'].split('@')[0]) if getattr(settings, 'FACEBOOK_FORCE_SIGNUP', False): # No existing user, use anonymous user = AnonymousUser() user.username = username user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] fb_user = FacebookProfile( facebook_id=fb_profile['id'], access_token=access_token ) user.facebookprofile = fb_user else: # No existing user, create one try: user = User.objects.create_user(username, fb_profile['email']) except IntegrityError: # Username already exists, make it unique user = User.objects.create_user(username + fb_profile['id'], fb_profile['email']) user.first_name = fb_profile['first_name'] user.last_name = fb_profile['last_name'] user.save() # Create the FacebookProfile fb_user = FacebookProfile(user=user, facebook_id=fb_profile['id'], access_token=access_token) fb_user.save() return user