I am stuck in django and would really appreciate it if someone could help me.
I need to have an entry point for a 3rd party API. So I created a view and decorated it with @csrf_exempt
Now the problem is I am not able to access any session variables I set before. edit - I set multiple session variables like user email to know if a user is already logged in. I was able to use the session before calling the 3rd party API. When the 3rd party API sends a response, they don't send CSRF token hence I have exempt that view from csrf. Once I receive a valid response I want to update my database. To do that, I need to know the email id of the user which I lost since I don't have session variables anymore.
ppConfirmPaymentProcess
is another function that processes the POST data sent by this 3rd party API.
Everything is working fine, csrf_exempt is also working fine but I can't do request.session["foo"]
with this request. Can someone please help?
@csrf_exempt
def ppConfirmPayment(request):
print(request.session, "=======================================")
for key, value in request.session.items():
print('{} => {}'.format(key, value))
return ppConfirmPaymentProcess(request)
I solved it using Django itself. No manipulation of session-id or interaction with the database.
Step1: call 3rd party api
@login_required
def thirdPartyAPICall(request):
#do some stuff and send a request to 3rd party
Step2: receive a call-back from 3rd party in the view. Note how I put csrf_exempt
and not login_required
so that 3rd party can send a request to my application without CSRF token and session. It's like an entry point to my APP for them.
In this callBackView
do some action and check if this indeed is a valid response from the 3rd party or someone is trying to hack your system.
E.g. check for CHECKSUM
or TXNID
etc and then create a response dictionary and sent another HTTP response to another resource using HttpResponseRedirect
with-in my app and then I passed relevant GET parameter to it.
This particular step restores my previous session and now I have the relevant data from the 3rd party to process the request I sent to them and I also got my session
back in the request.
@csrf_exempt
def callBackView(request):
if request.POST["CHECKSUM"] == myCalCulatedCheckSum:
foo = True
else:
foo = False
return HttpResponseRedirect("TEST.HTML" +"/" + str(foo))
I like this method the most because as I mentioned before, we don't need to store session, Django does it for us.