Morag
Morag

Reputation: 49

How do I authenticate remotely in Django?

Basically what I'm doing is building a desktop application that needs to connect to a web server, perform some calculations, and then have the resulting values sent back to it. The calculations change over time, and also need to stay somewhat secure, which is why I'm not just coding them into the desktop application.

The issue is that only authenticated users should be allowed to access the server. So what I'm looking for is a way to log-in to Django from my desktop application (i.e. the desktop application will pop up a window asking for a username and password, which will then be sent to the Django site, used to authenticate the user, and if valid, will return the results of the computation. It also needs to work as a session (the user enters their password at the beginning and then doesn't need to log-in again until they close the desktop application, even if multiple computations are performed).

I think I need to use session keys for this, and perhaps the RemoteUserMiddleware described here but I really have no idea where to start as far as coding this. Any suggestions or pointers in the right direction would be hugely appreciated.

Thanks, -Morag

P.S. If this is important, the desktop application is written in VB.NET.

Upvotes: 0

Views: 311

Answers (1)

A.J.Rouvoet
A.J.Rouvoet

Reputation: 1213

Interesting. I've never done anything like this, but here is, what I assume, is the basic idea:

1) Get a good view of Django sessions; the basic idea is:

  • One logs in using the django auth framework login service
  • Django will create a session for you and handle all the difficult stuff
  • Django returns a HttpResponse with a sessionid cookie. You will need to send this back with any request following to identify yourself and 'operate within the session'.
  • One logs out using the django auth logout service and the session is destroyed by Django.

2) Now, the rest is relatively easy.

  • Setup django urls.py with the appropriate urls for login/logout + computation service
  • Execute a post request to the login service with the appropriate parameters set
  • Catch the response, extract the 'sessionid' cookie and save it somewhere
  • On each subsequent request, include the sessionid

This should get you started. Good luck!

Upvotes: 1

Related Questions