1
votes

I'm using django-all auth with GMail login.

There one of my view that will receive HTTP-POST from a Hotspot login page in other server (actualy it's mikrotik hotspot redirect). I need to read their posted data AFTER social login.

I read https://stackoverflow.com/a/32250781/5901318

Looks like the safest way is to store the POST data in session, and later my view will get it from request.session

but I don't know how to 'store data safely in request.session before the authentication occurs'.

def my_login_required(function):
    #https://stackoverflow.com/a/39256685/5901318
    def wrapper(request, *args, **kwargs):
        decorated_view_func = login_required(request)
        if not decorated_view_func.user.is_authenticated:
            if request.method == "POST" :
                print('my_login_required POST:',request.POST.__dict__)
                print('my_login_required ARGS:',args)
                print('my_login_required KWARGS:',kwargs)
                print('my_login_required SESSION:',request.session.__dict__)
    wrapper.__doc__ = function.__doc__
    wrapper.__name__ = function.__name__
    return wrapper


#@receiver(user_logged_in)
@csrf_exempt 
@my_login_required
def hotspotlogin(request,*args,**kwargs):
    print('HOTSPOTLOGIN')

I tried to access it using requests :

r = requests.post('http://mysite:8000/radius/hotspotlogin/', json={"NAMA": "BINO"}, headers = {'Content-type': 'application/json', 'Accept': 'text/plain'})

but in django shell I only got :

my_login_required POST: {'_encoding': 'utf-8', '_mutable': False}
my_login_required ARGS: ()
my_login_required KWARGS: {}
my_login_required SESSION: {'storage_path': '/opt/djangos/radius03/mysessions/', 'file_prefix': 'sessionid', '_SessionBase__session_key': None, 'accessed': True, 'modified': False, 'serializer': <class 'django.core.signing.JSONSerializer'>, '_session_cache': {}}

Kindly please give me any clue to do that.

Sincerely

-bino-

1

1 Answers

0
votes

Got priceless help from a friend, and here is the solution.

def my_login_required(function):
    def wrapper(request, *args, **kwargs):
        old_data=dict()
        try :
            old_data['POST'] = dict(request.POST)
        except :
            old_data['POST'] = dict()

        try :
            old_data['GET'] = dict(request.GET)
        except :
            old_data['GET'] = dict()

        old_data['method'] = request.method

        decorated_view_func = login_required(request)
        if not decorated_view_func.user.is_authenticated: #Only if user not authenticated
            request.session['old'] = old_data  #put old data in request.session['old']
            return decorated_view_func(request)  # return redirect to signin

        return function(request, *args, **kwargs)

    wrapper.__doc__ = function.__doc__
    wrapper.__name__ = function.__name__
    return wrapper


@my_login_required
def testview(request,*args,**kwargs):
    print('SESSION DATA:', request.session.get('old')) #take original post/get data from request.session['old']