1

How can I get the django session ID that is ultimately stored in the cookie using javascript?

The reason I need to get that ID is that I'm doing everything from a REST API, including authentication. So, the guys developing the mobile app are using ionic and they want to make a request to the API to get that session ID - and then use it in future requests. Any advice?

1
  • 1
    What is the problem with getting the cookie? The default name is sessionid, but this is configurable via SESSION_COOKIE_NAME. Note that I make no comments about the security of that approach ;-)
    – dhke
    Commented Mar 11, 2015 at 21:41

3 Answers 3

5

Hey i think this approach is not the best one, your backend is having a door here for XSS attacks , i think the best you can do its use a token for authentication even normal token.auth that is well explained on django-rest-framework docs http://www.django-rest-framework.org/api-guide/authentication/ but i use a lot JWT JSONweb token auth so u dont compromise other data than token, username or other custom stuff like roles or something your app logic have, since ionic its angular JS you can take a look into http://frederiknakstad.com/2013/01/21/authentication-in-single-page-applications-with-angular-js/ to see how you can manage the authentication process on frontend mobile app.

3

Django cookies are not available by default to Javascript as the SESSION_COOKIE_HTTPONLY setting is set to True by default, for good reason. You can set this to False and then your cookie will be accessible, but then your site will be more vulnerable to XSS.

2
  • So, would you suggest any specific different approach? I could get the session id in the backend and then send it as a context variable for certain specific requests.... or hash it. Thanks for your answer btw. Commented Mar 11, 2015 at 23:37
  • Hmm, good question. I'm not sure I know enough about Ionic or your setup to give any really great advice. Commented Mar 12, 2015 at 2:40
3

Following on from codeadict's answer:

You could use JSON Web Tokens but they are a tiny bit more involved. The easiest solution is to use token authentication, you could follow the instructions here to get that setup:

http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication

A potentially better solution as you are using Ionic (which uses AngularJS under the hood) is a package called django-rest-auth built for doing exactly what you are doing.

The instructions to get that setup are here http://django-rest-auth.readthedocs.org/en/latest/ and they have also released an AngularJS module here: https://github.com/Tivix/angular-django-registration-auth

Don't forget to turn HTTPS / SSL on as using token auth without it is extremely insecure.

The angular module linked above handles getting and storing these tokens for you, you then need to include the access token on every request you make to the api (via $http or $request or whatever)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.