def user_data(self, access_token, *args, **kwargs): """ Grab user profile information from facebook. returns: dict or None """ data = None params = backend_setting(self, self.EXTRA_PARAMS_VAR_NAME, {}) params['access_token'] = access_token url = FACEBOOK_ME + urlencode(params) try: response = dsa_urlopen(url) data = json.load(response) except ValueError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Could not load user data from Facebook.', exc_info=True, extra=extra) except HTTPError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Error validating access token.', exc_info=True, extra=extra) raise AuthTokenError(self) else: log('debug', 'Found user data for token %s', sanitize_log_data(access_token), extra={'data': data}) return data
def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" data = None params = setting('FACEBOOK_PROFILE_EXTRA_PARAMS', {}) params['access_token'] = access_token url = FACEBOOK_ME + urlencode(params) try: data = simplejson.load(urlopen(url)) except ValueError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Could not load user data from Facebook.', exc_info=True, extra=extra) except HTTPError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Error validating access token.', exc_info=True, extra=extra) raise AuthTokenError(self) else: log('debug', 'Found user data for token %s', sanitize_log_data(access_token), extra={'data': data}) return data
def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" data = None params = backend_setting(self, self.EXTRA_PARAMS_VAR_NAME, {}) params['access_token'] = access_token url = FACEBOOK_ME + urlencode(params) try: response = requests.get(url) if response.status_code == 200: data = simplejson.loads(response.text) else: raise AuthTokenError() except ValueError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Could not load user data from Facebook.', exc_info=True, extra=extra) except requests.RequestException: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Error validating access token.', exc_info=True, extra=extra) raise AuthTokenError(self) else: log('debug', 'Found user data for token %s', sanitize_log_data(access_token), extra={'data': data}) return data
def user_data(self, access_token, *args, **kwargs): """ Grab user profile information from facebook. returns: dict or None """ data = None params = backend_setting(self, self.EXTRA_PARAMS_VAR_NAME, {}) params['access_token'] = access_token url = FACEBOOK_ME + urlencode(params) try: response = dsa_urlopen(url) data = simplejson.load(response) except ValueError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Could not load user data from Facebook.', exc_info=True, extra=extra) except HTTPError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Error validating access token.', exc_info=True, extra=extra) raise AuthTokenError(self) else: log('debug', 'Found user data for token %s', sanitize_log_data(access_token), extra={'data': data}) return data
def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" data = None params = backend_setting(self, self.EXTRA_PARAMS_VAR_NAME, {}) params['access_token'] = access_token url = FACEBOOK_ME + urlencode(params) try: data = simplejson.load(dsa_urlopen(url)) except ValueError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Could not load user data from Facebook.', exc_info=True, extra=extra) except HTTPError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Error validating access token.', exc_info=True, extra=extra) raise AuthTokenError(self) else: log('debug', 'Found user data for token %s', sanitize_log_data(access_token), extra={'data': data}) mandatory = backend_setting(self, self.MANDATORY_PERMISSIONS_VAR_NAME, []) for permission in mandatory: if not data.get(permission, False): raise AuthIncomplete(self, 'Missing mandatory permission %s' % permission) return data
def user_data(self, access_token): """Loads user data from service""" data = None url = FACEBOOK_ME + urlencode({'access_token': access_token}) try: data = simplejson.load(urlopen(url)) except ValueError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Could not load user data from Facebook.', exc_info=True, extra=extra) except HTTPError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Error validating access token.', exc_info=True, extra=extra) raise AuthTokenError(self) else: log('debug', 'Found user data for token %s', sanitize_log_data(access_token), extra={'data': data}) return data
def user_data(self, access_token): """Loads user data from service""" data = None url = FACEBOOK_ME + urlencode({'access_token': access_token}) try: data = simplejson.load(urlopen(url)) logger.debug('Found user data for token %s', sanitize_log_data(access_token), extra=dict(data=data)) except ValueError: params.update({'access_token': sanitize_log_data(access_token)}) logger.error('Could not load user data from Facebook.', exc_info=True, extra=dict(data=params)) return data
def user_data(self, access_token): """Loads user data from service""" params = {'access_token': access_token,} url = 'https://graph.facebook.com/me?' + urlencode(params) try: data = simplejson.load(urlopen(url)) logger.debug('Found user data for token %s', sanitize_log_data(access_token), extra=dict(data=data)) return data except ValueError: params.update({'access_token': sanitize_log_data(access_token)}) logger.error('Could not load user data from Facebook.', exc_info=True, extra=dict(data=params)) return None
def user_data(self, access_token): """Loads user data from service""" data = None url = FACEBOOK_ME + urlencode({'access_token': access_token}) try: data = simplejson.load(urlopen(url)) except ValueError: extra = {'access_token': sanitize_log_data(access_token)} log('error', 'Could not load user data from Facebook.', exc_info=True, extra=extra) else: log('debug', 'Found user data for token %s', sanitize_log_data(access_token), extra=dict(data=data)) return data
def authenticate(self, *args, **kwargs): """Authenticate user using social credentials Authentication is made if this is the correct backend, backend verification is made by kwargs inspection for current backend name presence. """ # Validate backend and arguments. Require that the Social Auth # response be passed in as a keyword argument, to make sure we # don't match the username/password calling conventions of # authenticate. if not (self.name and kwargs.get(self.name) and 'response' in kwargs): return None # Pull in basics from the backend response = kwargs.get('response') details = self.get_user_details(response) uid = self.get_user_id(details, response) is_new = False user = kwargs.get('user') # If we've got an associated user, we're good to go try: social_user = self.get_social_auth_user(uid) except UserSocialAuth.DoesNotExist: # Oops! We don't know who you are! # There's two ways this can happen # 1. You're new here and we'll need to register you if that's the kind of place this is # 2. You've been here a while, but need to link this profile # Creating users is a pain, but we need to do that first so that we can link later if user is None: # You're new here! # Check to see if we've been told to create new users # First, we need permission from settings in the form of CREATE_USERS # Then we need to make sure that the kwarg for create_user isn't False if CREATE_USERS and kwargs.get('create_user', True): # We can create the user! JOY! # But wait! Shouldn't we check to see if there's already a user with this e-mail address and just link the two? email = details.get('email') if email and ASSOCIATE_BY_MAIL: # try to associate accounts registered with the same email # address, only if it's a single object. ValueError is # raised if multiple objects are returned try: user = User.objects.get(email=email) except MultipleObjectsReturned: raise ValueError('Not unique email address supplied') except User.DoesNotExist: user = None # OK. we're finally ready to create the account if we got to this point without a user if not user: username = self.username(details) print('Creating new user with username %s and email %s', username, sanitize_log_data(email)) logger.debug('Creating new user with username %s and email %s', username, sanitize_log_data(email)) user = User.objects.create_user(username=username, email=email) is_new = True else: # Wait a second! We can't create a user! We're forbidden by the settings or the kwargs # Send signal for cases where tracking failed registering # is useful. socialauth_not_registered.send(sender=self.__class__, uid=uid, response=response, details=details) return None # Now, we've either created a user or we've returned a None. Link! try: social_user = self.associate_auth(user, uid, response, details) except IntegrityError: # Protect for possible race condition, those bastard with FTL # clicking capabilities social_user = self.get_social_auth_user(uid) # Raise ValueError if this account was registered by another user. if user and user != social_user.user: raise ValueError('Account already in use.', social_user) user = social_user.user # Flag user "new" status setattr(user, 'is_new', is_new) # Update extra_data storage, unless disabled by setting if LOAD_EXTRA_DATA: extra_data = self.extra_data(user, uid, response, details) if extra_data and social_user.extra_data != extra_data: social_user.extra_data = extra_data social_user.save() user.social_user = social_user # Update user account data. self.update_user_details(user, response, details, is_new) return user
def authenticate(self, *args, **kwargs): """Authenticate user using social credentials Authentication is made if this is the correct backend, backend verification is made by kwargs inspection for current backend name presence. """ # Validate backend and arguments. Require that the Social Auth # response be passed in as a keyword argument, to make sure we # don't match the username/password calling conventions of # authenticate. if not (self.name and kwargs.get(self.name) and 'response' in kwargs): return None response = kwargs.get('response') details = self.get_user_details(response) uid = self.get_user_id(details, response) is_new = False user = kwargs.get('user') try: social_user = self.get_social_auth_user(uid) except UserSocialAuth.DoesNotExist: if user is None: # new user if not CREATE_USERS or not kwargs.get('create_user', True): # Send signal for cases where tracking failed registering # is useful. socialauth_not_registered.send(sender=self.__class__, uid=uid, response=response, details=details) return None email = details.get('email') if email and ASSOCIATE_BY_MAIL: # try to associate accounts registered with the same email # address, only if it's a single object. ValueError is # raised if multiple objects are returned try: user = User.objects.get(email=email) except MultipleObjectsReturned: raise ValueError('Not unique email address supplied') except User.DoesNotExist: user = None if not user: username = self.username(details) logger.debug('Creating new user with username %s and email %s', username, sanitize_log_data(email)) user = User.objects.create_user(username=username, email=email) is_new = True try: social_user = self.associate_auth(user, uid, response, details) except IntegrityError: # Protect for possible race condition, those bastard with FTL # clicking capabilities social_user = self.get_social_auth_user(uid) # Raise ValueError if this account was registered by another user. if user and user != social_user.user: raise ValueError('Account already in use.', social_user) user = social_user.user # Flag user "new" status setattr(user, 'is_new', is_new) # Update extra_data storage, unless disabled by setting if LOAD_EXTRA_DATA: extra_data = self.extra_data(user, uid, response, details) if extra_data and social_user.extra_data != extra_data: social_user.extra_data = extra_data social_user.save() user.social_user = social_user # Update user account data. self.update_user_details(user, response, details, is_new) return user