How to create third party login API in Django to save session details?

Viewed 16

I am trying to build an external API in django, such that any third party application can log into my Django application with their specific username and password, and further use all the authenticated calls in the Django application.

As I understand, in Django, there is a GET call that creates the csrf_token, and an op_browser_state token, which is saved in Cookies. Once the user enters the username and password, and selects login. Internally, the session id is added via the middleware between request and response, after the auth.login is successful, with the username and the password. The session_id, along with the csrf_token and op_browser_state are saved in the cookies, and used for all the further calls made via Django APIs, which may be using SessionAuthentication.

So, if I am able to save the cookies for the session__id, csrf_token and op_browser_state for the Django application hostname. I should be able to solve it. I tried creating an external API, which does the login for the given combination of username and password, and returns the values of csrf_token and session_id as generated by the API. However, this didn't work, since looks like it is not storing the cookies for the Django application, and instead for the third party application. Thus, I am unable to skip the login page in the Django application to get into any other page.

Is there a way such that I can write an API, that directly logs into a Django application, that is using BasicAuthentication and SessionAuthentication, and saves the related session_id and csrf_token into the cookies, and directly allows us to log into the platform? Or if there is any other custom solution possible for the same?

0 Answers
Related