Remote Authentication (out-of-the-box)

25 views
Skip to first unread message

Cristian Bustos

unread,
Jan 21, 2015, 8:30:10 PM1/21/15
to django...@googlegroups.com
Hello Friends,
Here is my question, Remote Authentication (out-of-the-box) django
I have 2 projects Django:
  1. Users Api Django app (Django framework rest - token authentication) - Server
  2. Django app (I need to authenticate users and consume APIs) - Client
I can authenticate user, only use requests-python, but i need do remote authentication with django

This is my code:
settings.py

AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.RemoteUserBackend', )

MIDDLEWARE_CLASSES = ( '...', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.auth.middleware.RemoteUserMiddleware',
 '...', )


view.py
-------------------------------------------------
class LoginRCS(View):
    template_name = 'userprofiles/login.html'

    def get(self, request, *args, **kwargs):
        return render(request, self.template_name )

    def post(self, request, *args, **kwargs):
        # inputBp and inputPassword come from login form
        if 'inputBp' in request.POST and 'inputPassword' in request.POST:
            inputBp = request.POST['inputBp']
            inputPassword = request.POST['inputPassword']

            # URL to get token from api user
            URL_API = 'http://localhost:7000/api/token/'
            payload = {'username': inputBp, 'password': inputPassword}
            headers = {'content-type': 'application/json'}
            r = requests.post(URL_API, data=json.dumps(payload), headers=headers)

            if r.status_code == requests.codes.ok:
                # I have a token    
                data = json.loads(r.text)
                token_auth = 'token ' + data['token']

                URL_CHECK_USER = 'http://127.0.0.1:7000/api/users/?username='+inputBp
                payload_login = {'username': inputBp, 'password': inputPassword}
                headers_login = {
                            'content-type': 'application/json',
                            'Authorization': token_auth }
                r_user = requests.get(URL_CHECK_USER, data=json.dumps(payload_login), headers=headers_login)

                if r_user.status_code == request.codes.ok:
                    # I have a user info (unsername, first_name, last_name, email)
                    data_user = json.loads(r_user.text)

                    #################################
                    # here need authentication
                    ################################# 
                    # user = authenticate(username=my_user, password=my_bp)

            else:
                print 'Error'
        return render(request, self.template_name)

thanks in advance,
CB

Collin Anderson

unread,
Jan 22, 2015, 9:12:58 PM1/22/15
to django...@googlegroups.com
Hi,

RemoteUserBackend is for Apache or Windows IIS, I don't think it's what you want.

Do you have a local copy of the user table on hand? If so just query the matching username and call "django.contrib.auth.login()" on it.

Otherwise, you'll need to use a custom backend.

Collin
Reply all
Reply to author
Forward
0 new messages