<gh_stars>0
from django.conf import settings
from mongoengine.django.auth import User
import requests
import json


class HeliosAuthBackend(object):
    """
    Authenticate against the API.
    """

    def authenticate(self, username=None, password=<PASSWORD>):

        payload = {'username': username, 'password': password}
        url = '%s/auth/login/' % (settings.API_BASE_URL,)
        r = requests.post(url, data=json.dumps(payload))

        if r.status_code == requests.codes.ok:

            token = r.headers.get('x-auth-token')
            if not token:
                return None

            try:
                user = User.objects.get(username=username)
                user.password = <PASSWORD>
                user.save()
            except User.DoesNotExist:
                # Create a new user. Note that we can set password
                # to anything, because it won't be checked; the password
                # from settings.py will.
                user = User(username=username, password=<PASSWORD>)
                user.is_staff = True
                user.is_superuser = True
                user.save()
            return user
        elif r.status_code >= requests.codes.internal_server_error:
            raise Exception('Server error. ' + str(r.status_code))
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None
from django.conf import settings
from mongoengine.django.auth import User
import requests
import json


class HeliosAuthBackend(object):
    """
    Authenticate against the API.
    """

    def authenticate(self, username=None, password=<PASSWORD>):

        payload = {'username': username, 'password': password}
        url = '%s/auth/login/' % (settings.API_BASE_URL,)
        r = requests.post(url, data=json.dumps(payload))

        if r.status_code == requests.codes.ok:

            token = r.headers.get('x-auth-token')
            if not token:
                return None

            try:
                user = User.objects.get(username=username)
                user.password = <PASSWORD>
                user.save()
            except User.DoesNotExist:
                # Create a new user. Note that we can set password
                # to anything, because it won't be checked; the password
                # from settings.py will.
                user = User(username=username, password=<PASSWORD>)
                user.is_staff = True
                user.is_superuser = True
                user.save()
            return user
        elif r.status_code >= requests.codes.internal_server_error:
            raise Exception('Server error. ' + str(r.status_code))
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None
Authenticate against the API. # Create a new user. Note that we can set password # to anything, because it won't be checked; the password # from settings.py will.