Question

I am in a situation where I need to make a custom authentication and custom middleware to authenticate and authorize the user. I have to chek the username password params in the POST request or if the cookie is set or not for token based authentication. Now, as I know that function overloading is not allowed in python, how could I achieve it. I am putting the code below for custom auth and 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' and request.POST.has_key('username' ) and 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 the custom auth backend :

from core.api import NcpAPI       

class CustomNCPAuthBackend(object):     
    """
    This is custom authentication backend.
    Authenticate against the webservices call.

    The method below would override authenticate() of django.contrib.auth    
    """
    def authenticate(self, username = None, password = None):           
        print "inside authenticate of username and password with username being : "+username            
        return None

    def authenticate(self,token=None):
        print "inside authenticate of token with token being : "+token
        return None

The problem is even when I am checking for username and password in post request it calls the token one as token is there, but how could I force it somehow?

I tried deleting the cookie and trying again but still it doesn't fire up the authentication function with username and password as params.

What could be solution for this please?

Was it helpful?

Solution

You are correct, Python doesn't support function overloading, because it simply doesn't need it. What happens in your case is that the second declaration of authenticate overwrites the first one, so you are left with only one version of authenticate, the one that takes token as a parameter.

What you should do instead is (just an example, there are many possible solutions):

class CustomNCPAuthBackend(object):
    """
    This is custom authentication backend.
    Authenticate against the webservices call.

    The method below would override authenticate() of django.contrib.auth    
    """
    def authenticate_password(self, username=None, password=None):
        print "inside authenticate of username and password with username being : "+username
        return None

    def authenticate_token(self,token=None):
        print "inside authenticate of token with token being : "+token
        return None

    def authenticate(self, token=None, username=None, password=None):
        if token is not None:
             return self.authenticate_token(token)
        else:
             return self.authenticate_password(username, password)

This way it will work with the AuthMiddleWare you wrote.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top