tastypie-user-session

User sessions on top of Tastypie, plus Facebook auth
Download

tastypie-user-session Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • Tudor Prodan
  • Publisher web site:
  • https://github.com/tudorprodan/

tastypie-user-session Tags


tastypie-user-session Description

tastypie-user-session is the ideal way to authenticate for example in a Backbone.js client application.__version__ = "0.2"Installation# grab the code from githubpip install -e git://github.com/tudorprodan/tastypie_user_session.git#egg=tastypie-user-session# or PyPIpip install tastypie-user-sessionyourapp/api.py :from tastypie_user_session import FacebookAuthUserSessionResourcev1_api = Api(api_name="v1")v1_api.register(FacebookAuthUserSessionResource())settings.py :INSTALLED_APPS += ("tastypie_user_session", )TASTYPIE_USER_RESOURCE_SETTINGS = { "facebook_app_id": "< your_app_id >", "facebook_app_secret": "< your_app_secret >"}Usage- GET /api/v1/user_session/ - see if you have an active session- PUT /api/v1/user_session/< session_key >/ - refresh your session, empty request body- DELETE /api/v1/user_session//- delete the session (logout)- POST /api/v1/user_session/ - create a new session (login) with a new or existing user for the app- using the Facebook JS SDK cookie, request body: { "facebook_use_cookie": true }- via a Facebook oauth code, request body: { "facebook_code": "< users_fb_oauth_code >" }- via a Facebook auth token, request body: { "facebook_token": "< users_fb_token >" }As long as the client keeps using the same cookiejar (the way browsers do), he is now authenticated by django.contrib.auth's middleware automatically.tastypie-user-session is the ideal way to authenticate for example in a Backbone.js client application.DjangoAuthUserSessionResourceAllows users to authenticate with any backend by POSTing credentials.User creation is not supported, because I have not implemented it, but could be added.UserSessionResourceThis is the base class, which is meant to be extended by you to achieve the behavior you want.Both FacebookAuthUserSessionResource and DjangoAuthUserSessionResource override a single method from this class:def find_or_create_user_for_new_session(self, bundle, request, **kwargs)CustomizationUsing an existing Facebook ID field on UserProfileSuppose you already have a Facebook ID associated with your users, e.g. you used it for something else:class UserProfile(models.Model): ... fb_id = models.CharField(max_length=255) ...tastypie_user_session.FacebookAuthUserSessionResource can use it:# settings.pyTASTYPIE_USER_RESOURCE_SETTINGS = "fb_id"Now, instead of using it's own FacebookAuthUser model, it will use UserProfile.fb_id to store and look up user's Facebook ID.Using Facebook's Oauth dialogAs described here, you can use Facebook's Oauth dialog to get a user authorization code, which can then be exchanged for an access token. In order to do the exchange, we need the redirect URI used by the client (FB API requirement).# settings.pyTASTYPIE_USER_RESOURCE_SETTINGS = "http://www.mysite.com/facebook_oauth_landing_page.html"NotesI'm already using FacebookAuthUserSessionResource successfully on two projects.You can very easily extend UserSessionResource to suit your needs and authenticate in any way you want. (e.g. LDAP)Product's homepage


tastypie-user-session Related Software