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 
<http://127.0.0.1:7000/api/users/?username=%27+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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8b8fac15-a958-4194-9485-af1de580926c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to