python - django custom authentication backend with custom middleware (both username,password and token absed auth) -


i in situation need make custom authentication , custom middleware authenticate , authorize user. have chek username password params in post request or if cookie set or not token based authentication. now, know function overloading not allowed in python, how achieve it. putting code below custom auth , custom middleware.

custom middleware:

from django.contrib.auth import authenticate  class authmiddleware(object):     def process_request(self, request):          if request.path != '/favicon.ico':             print "inside process_request " + request.path                            if request.method == 'post' , request.post.has_key('username' ) , request.post.has_key('password'):                                      authenticate(username = request.post.get('username'),password = request.post.get('password'))              if 'spring_security_remember_me_cookie' in request.cookies:                                      authenticate(token = request.cookies.get('spring_security_remember_me_cookie'))          return none 

and custom auth backend :

from core.api import ncpapi         class customncpauthbackend(object):          """     custom authentication backend.     authenticate against webservices call.      method below override authenticate() of django.contrib.auth         """     def authenticate(self, username = none, password = none):                    print "inside authenticate of username , password username being : "+username                     return none      def authenticate(self,token=none):         print "inside authenticate of token token being : "+token         return none 

the problem when checking username , password in post request calls token 1 token there, how force somehow?

i tried deleting cookie , trying again still doesn't fire authentication function username , password params.

what solution please?

you correct, python doesn't support function overloading, because doesn't need it. happens in case second declaration of authenticate overwrites first one, left 1 version of authenticate, 1 takes token parameter.

what should instead (just example, there many possible solutions):

class customncpauthbackend(object):     """     custom authentication backend.     authenticate against webservices call.      method below override authenticate() of django.contrib.auth         """     def authenticate_password(self, username=none, password=none):         print "inside authenticate of username , password username being : "+username         return none      def authenticate_token(self,token=none):         print "inside authenticate of token token being : "+token         return none      def authenticate(self, token=none, username=none, password=none):         if token not none:              return self.authenticate_token(token)         else:              return self.authenticate_password(username, password) 

this way work authmiddleware wrote.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -