示例#1
0
 def disconnect(self, user, association_id=None):
     """Deletes current backend from user if associated.
     Override if extra operations are needed.
     """
     if association_id:
         UserSocialAuth.get_social_auth_for_user(user).get(id=association_id).delete()
     else:
         UserSocialAuth.get_social_auth_for_user(user).filter(provider=self.AUTH_BACKEND.name).delete()
示例#2
0
 def disconnect(self, user, association_id=None):
     """Deletes current backend from user if associated.
     Override if extra operations are needed.
     """
     if association_id:
         UserSocialAuth.get_social_auth_for_user(user)\
                         .get(id=association_id).delete()
     else:
         UserSocialAuth.get_social_auth_for_user(user)\
                         .filter(provider=self.AUTH_BACKEND.name).delete()
示例#3
0
    def getAssociation(self, server_url, handle=None):
        """Return stored assocition"""
        oid_associations = UserSocialAuth.get_oid_associations(server_url,
                                                               handle)
        associations = [association
                        for assoc_id, association in oid_associations
                        if association.getExpiresIn() > 0]
        expired = [assoc_id for assoc_id, association in oid_associations
                   if association.getExpiresIn() == 0]

        if expired:  # clear expired associations
            UserSocialAuth.delete_associations(expired)

        if associations:  # return most recet association
            return associations[0]
def backends_data(user):
    """Return backends data for given user.

    Will return a dict with values:
        associated: UserSocialAuth model instances for currently
                    associated accounts
        not_associated: Not associated (yet) backend names.
        backends: All backend names.

    If user is not authenticated, then first list is empty, and there's no
    difference between the second and third lists.
    """
    available = get_backends().keys()
    values = {'associated': [],
              'not_associated': available,
              'backends': available}

    # user comes from request.user usually, on /admin/ it will be an instance
    # of auth.User and this code will fail if a custom User model was defined
    if hasattr(user, 'is_authenticated') and user.is_authenticated():
        associated = UserSocialAuth.get_social_auth_for_user(user)
        not_associated = list(set(available) -
                              set(assoc.provider for assoc in associated))
        values['associated'] = associated
        values['not_associated'] = not_associated
    return values
示例#5
0
def backends_data(user):
    """Return backends data for given user.

    Will return a dict with values:
        associated: UserSocialAuth model instances for currently
                    associated accounts
        not_associated: Not associated (yet) backend names.
        backends: All backend names.

    If user is not authenticated, then first list is empty, and there's no
    difference between the second and third lists.
    """
    available = get_backends().keys()
    values = {
        'associated': [],
        'not_associated': available,
        'backends': available
    }

    # user comes from request.user usually, on /admin/ it will be an instance
    # of auth.User and this code will fail if a custom User model was defined
    if hasattr(user, 'is_authenticated') and user.is_authenticated():
        associated = UserSocialAuth.get_social_auth_for_user(user)
        not_associated = list(
            set(available) - set(assoc.provider for assoc in associated))
        values['associated'] = associated
        values['not_associated'] = not_associated
    return values
示例#6
0
文件: user.py 项目: ukaoma/nnmware
def get_username(details, user=None,
                 user_exists=UserSocialAuth.simple_user_exists,
                 *args, **kwargs):
    """Return an username for new user. Return current user username
    if user was given.
    """
    if user:
        return {'username': user.username}

    if details.get(USERNAME):
        username = unicode(details[USERNAME])
    else:
        username = uuid4().get_hex()

    uuid_length = 16
    max_length = UserSocialAuth.username_max_length()
    short_username = username[:max_length - uuid_length]
    final_username = username[:max_length]

    # Generate a unique username for current user using username
    # as base but adding a unique hash at the end. Original
    # username is cut to avoid any field max_length.
    while user_exists(username=final_username):
        username = short_username + uuid4().get_hex()[:uuid_length]
        final_username = username[:max_length]

    return {'username': final_username}
 def context_value():
     keys = get_backends().keys()
     accounts = dict(zip(keys, [None] * len(keys)))
     user = request.user
     if hasattr(user, 'is_authenticated') and user.is_authenticated():
         accounts.update((assoc.provider.replace('-', '_'), assoc)
                 for assoc in UserSocialAuth.get_social_auth_for_user(user))
     return accounts
示例#8
0
 def context_value():
     keys = get_backends().keys()
     accounts = dict(zip(keys, [None] * len(keys)))
     user = request.user
     if hasattr(user, 'is_authenticated') and user.is_authenticated():
         accounts.update(
             (assoc.provider.replace('-', '_'), assoc)
             for assoc in UserSocialAuth.get_social_auth_for_user(user))
     return accounts
示例#9
0
文件: store.py 项目: ukaoma/nnmware
    def getAssociation(self, server_url, handle=None):
        """Return stored assocition"""
        oid_associations = UserSocialAuth.get_oid_associations(
            server_url, handle)
        associations = [
            association for assoc_id, association in oid_associations
            if association.getExpiresIn() > 0
        ]
        expired = [
            assoc_id for assoc_id, association in oid_associations
            if association.getExpiresIn() == 0
        ]

        if expired:  # clear expired associations
            UserSocialAuth.delete_associations(expired)

        if associations:  # return most reset association
            return associations[0]
示例#10
0
文件: user.py 项目: ukaoma/nnmware
def create_user(backend, details, response, uid, username, user=None, *args,
                **kwargs):
    """Create user. Depends on get_username pipeline."""
    if user:
        return {'user': user}
    if not username:
        return None

    email = details.get('email') or None
    return {
        'user': UserSocialAuth.create_user(username=username, email=email),
        'is_new': True
    }
示例#11
0
def social_auth_user(backend, uid, user=None, *args, **kwargs):
    """Return UserSocialAuth account for backend/uid pair or None if it
    doesn't exists.

    Raise AuthException if UserSocialAuth entry belongs to another user.
    """
    social_user = UserSocialAuth.get_social_auth(backend.name, uid)
    if social_user:
        if user and social_user.user != user:
            msg = ugettext('This %(provider)s account already in use.')
            raise AuthException(backend, msg % {'provider': backend.name})
        elif not user:
            user = social_user.user
    return {'social_user': social_user, 'user': user}
示例#12
0
def associate_user(backend, user, uid, social_user=None, *args, **kwargs):
    """Associate user social account with user instance."""
    if social_user:
        return None

    try:
        social = UserSocialAuth.create_social_auth(user, uid, backend.name)
    except IntegrityError:
        # Protect for possible race condition, those bastard with FTL
        # clicking capabilities, check issue #131:
        #   https://github.com/omab/django-social-auth/issues/131
        return social_auth_user(backend, uid, user, social_user=social_user,
                                *args, **kwargs)
    else:
        return {'social_user': social, 'user': social.user}
示例#13
0
def associate_by_email(details, user=None, *args, **kwargs):
    """Return user entry with same email address as one returned on details."""
    if user:
        return None

    email = details.get('email')

    if email:
        # try to associate accounts registered with the same email address,
        # only if it's a single object. AuthException is raised if multiple
        # objects are returned
        try:
            return {'user': UserSocialAuth.get_user_by_email(email=email)}
        except MultipleObjectsReturned:
            raise AuthException(kwargs['backend'], 'Not unique email address.')
        except ObjectDoesNotExist:
            pass
示例#14
0
 def useNonce(self, server_url, timestamp, salt):
     """Generate one use number and return *if* it was created"""
     if abs(timestamp - time.time()) > SKEW:
         return False
     return UserSocialAuth.use_nonce(server_url, timestamp, salt)
示例#15
0
 def get_user(self, user_id):
     """
     Return user with given ID from the User model used by this backend
     """
     return UserSocialAuth.get_user(user_id)
示例#16
0
 def storeAssociation(self, server_url, association):
     """Store new assocition if doesn't exist"""
     UserSocialAuth.store_association(server_url, association)
示例#17
0
文件: store.py 项目: ukaoma/nnmware
 def useNonce(self, server_url, timestamp, salt):
     """Generate one use number and return *if* it was created"""
     if abs(timestamp - time.time()) > SKEW:
         return False
     return UserSocialAuth.use_nonce(server_url, timestamp, salt)
示例#18
0
 def get_user(self, user_id):
     """
     Return user with given ID from the User model used by this backend
     """
     return UserSocialAuth.get_user(user_id)
示例#19
0
文件: store.py 项目: ukaoma/nnmware
 def storeAssociation(self, server_url, association):
     """Store new assocition if doesn't exist"""
     UserSocialAuth.store_association(server_url, association)