Exemple #1
0
    def authenticate(self, username=None, password=None):
        if username is None or password is None:
            return None

        ra_user = RestAuthUser(connection, username)

        try:
            verified = ra_user.verify_password(password)
        except InternalServerError as e:
            response = e.args[0]
            log.error('RestAuth returned HTTP 500: %s', response.read())
            return None

        if verified:
            kwargs = {
                conf.USERNAME_FIELD: username,
            }

            try:
                user = User.objects.get(**kwargs)
            except User.DoesNotExist:
                user = User(**kwargs)

            if conf.LOCAL_PASSWORDS:
                user.set_password(password)

            if conf.SYNC_GROUPS:
                username = getattr(user, conf.USERNAME_FIELD)
                cache_key = '%s-restauth-groups' % username

                groups = cache.get(cache_key)
                if groups is None:
                    groups = [g.name for g in ra_user.get_groups()]
                    cache.set(cache_key, groups)

                self._sync_group_fields(user, groups)
                self._sync_groups(user, groups)


            user.save()

            return user
Exemple #2
0
    def authenticate(self, username=None, password=None):
        if username is None or password is None:
            return None

        ra_user = RestAuthUser(connection, username)

        try:
            verified = ra_user.verify_password(password)
        except InternalServerError as e:
            response = e.args[0]
            log.error('RestAuth returned HTTP 500: %s', response.read())
            return None

        if verified:
            kwargs = {
                conf.USERNAME_FIELD: username,
            }

            try:
                user = User.objects.get(**kwargs)
            except User.DoesNotExist:
                user = User(**kwargs)

            if conf.LOCAL_PASSWORDS:
                user.set_password(password)

            if conf.SYNC_GROUPS:
                username = getattr(user, conf.USERNAME_FIELD)
                cache_key = '%s-restauth-groups' % username

                groups = cache.get(cache_key)
                if groups is None:
                    groups = [g.name for g in ra_user.get_groups()]
                    cache.set(cache_key, groups)

                self._sync_group_fields(user, groups)
                self._sync_groups(user, groups)

            user.save()

            return user
        if checked is True:
            sys.exit(0)
        elif checked is False:
            sys.exit(1)
        # else: cache miss

from RestAuthClient.common import RestAuthConnection
from RestAuthClient.restauth_user import User

# Setup RestAuth connection
conn = RestAuthConnection(
    config.get(section, 'server'),
    config.get(section, 'user'),
    config.get(section, 'password'),
)
user = User(conn, username)

# Actual RestAuth queries in case cache does not match
if authtype == 'pass':
    if user.verify_password(line2):
        # set in cache if defined
        if cache is not None:
            cache.set_password(username, line2)

        sys.exit(0)
    else:
        sys.exit(1)

elif authtype == 'group':
    checked = set(line2.split())
    groups = set([g.name for g in user.get_groups()])