def authenticate(self, request=None): fb_info = facebook.get_user_from_cookie(request.COOKIES, settings.FACEBOOK_APP_ID, settings.FACEBOOK_APP_SECRET) if fb_info and 'uid' in fb_info: return get_user_model().objects.get_by_facebook_id(uid=fb_info['uid']) return None
def is_member(self, user): try: self.member.get(id=user.id) except get_user_model().DoesNotExist: return False return True
def authenticate(self, username=None, password=None): user_model = get_user_model() try: user = user_model.objects.get(username=username) if user.auth_secret == password: return user except user_model.DoesNotExist: return None
def get_user(self, user_id): ''' Get a user. ''' user_model = get_user_model() try: return user_model.objects.get(pk=user_id) except user_model.DoesNotExist: return None
def authenticate(self, request=None): fb_info = facebook.get_user_from_cookie(request.COOKIES, settings.FACEBOOK_APP_ID, settings.FACEBOOK_APP_SECRET) if fb_info and 'uid' in fb_info: return get_user_model().objects.get_by_facebook_id( uid=fb_info['uid']) return None
class PairableUserManager: # Settings for the generation of the auth_key auth_chars = string.digits auth_length = 7 auth_key_chars = string.letters + string.digits auth_key_length = 24 def _gen_unique_auth_secret(self): ''' Generate a globally-unique authentication secret This ensures that this auth_secret is unique, so it can be used later on to locate the UID during a pairing. ''' #another good technique for this is to create a hash of #'sitesecret:uid' sec = self._gen_random_string(self.auth_chars, self.auth_length) while self.filter(auth_secret=sec): sec = self._gen_random_string(self.auth_chars, self.auth_length) return sec def _gen_random_string(self, chars, length): ''' Generate a random string. ''' auth_secret = ''.join([random.choice(chars) for i in range(length)]) return auth_secret def pair_phone(self, auth_secret): ''' Pairs a user's phone to the database Will update the user's auth_secret with a randomly-generated string that should be sent by the client on each following request. If successful, will mark the user's paired flag. Returns the newly paired user. raises PairingException ''' try: u = self.get_by_auth_secret(auth_secret) if u.paired: raise PairingException('User has already paired') u.auth_secret = self._gen_random_string(self.auth_key_chars, self.auth_key_length) u.paired = True u.save() return u except get_user_model().DoesNotExist, e: raise PairingException('User not found for given auth_secret') except get_user_model().MultipleObjectsReturned, e: raise PairingException('Multiple users for auth_secret found.')
def authenticate(self, username=None, password=None): user_model = get_user_model() if email_re.search(username): try: user = user_model.objects.get(email=username) except user_model.DoesNotExist: return None if user.check_password(password): return user return None
def authenticate(self, username=None, password=None): ''' Locast authenticate that returns the custom user model as defined in settings. See locast.auth.get_user_model ''' user_model = get_user_model() try: user = user_model.objects.get(username=username) if user.check_password(password): return user except user_model.DoesNotExist: return None
def pair_phone(self, auth_secret): ''' Pairs a user's phone to the database Will update the user's auth_secret with a randomly-generated string that should be sent by the client on each following request. If successful, will mark the user's paired flag. Returns the newly paired user. raises PairingException ''' try: u = self.get_by_auth_secret(auth_secret) if u.paired: raise PairingException('User has already paired') u.auth_secret = self._gen_random_string(self.auth_key_chars, self.auth_key_length) u.paired = True u.save() return u except get_user_model().DoesNotExist, e: raise PairingException('User not found for given auth_secret')
def _pre_save(self, *args, **kargs): ''' Force all users to have at least the initial 7-digit auth_secret. ''' if not self.auth_secret: self.auth_secret = get_user_model( ).objects._gen_unique_auth_secret()
def get_by_facebook_id(self, uid): try: return self.get(facebook_id=uid) except get_user_model().DoesNotExist: return None
def get_by_username(self, username): try: return self.get(username=username) except get_user_model().DoesNotExist: return None
def _pre_save(self, *args, **kargs): ''' Force all users to have at least the initial 7-digit auth_secret. ''' if not self.auth_secret: self.auth_secret = get_user_model().objects._gen_unique_auth_secret()