Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lighting talk on django-social-auth

9,816 views

Published on

A quick tutorial on using django-social-auth, the best registration system for Django today.

Lighting talk on django-social-auth

  1. 1. Registration &AuthenticationA story about Django and OAUTH
  2. 2. Daniel Greenfeld @pydanny Who am I? Daniel Greenfeld (@pydanny) Pythonista at Cartwheel Djangonaut at Revsys http://opencomparison.org Fiancé of Audrey Royhttp://www.flickr.com/photos/pydanny/4442245488
  3. 3. Why am I talking?
  4. 4. We have needs
  5. 5. Daniel Greenfeld @pydannyWhat we need
  6. 6. Daniel Greenfeld @pydanny What we need• Registration of new users
  7. 7. Daniel Greenfeld @pydanny What we need• Registration of new users• Authentication of existing users
  8. 8. Daniel Greenfeld @pydanny What we need• Registration of new users• Authentication of existing users• Unless we are an ad-click content farm
  9. 9. Daniel Greenfeld @pydannyUse OAUTH
  10. 10. Daniel Greenfeld @pydanny Use OAUTH• People use Twitter/Facebook/etc
  11. 11. Daniel Greenfeld @pydanny Use OAUTH• People use Twitter/Facebook/etc• Fewer passwords to memorize
  12. 12. Daniel Greenfeld @pydanny Use OAUTH• People use Twitter/Facebook/etc• Fewer passwords to memorize• Our site needn’t store passwords
  13. 13. Daniel Greenfeld @pydanny Use OAUTH• People use Twitter/Facebook/etc• Fewer passwords to memorize• Our site needn’t store passwords• Twitter/Facebook/etc gets to worry about security
  14. 14. But OAUTH is a pain
  15. 15. Everyone implements it differently
  16. 16. Daniel Greenfeld @pydannyDifferent flavors
  17. 17. Daniel Greenfeld @pydanny Different flavors• Twitter
  18. 18. Daniel Greenfeld @pydanny Different flavors• Twitter• Facebook
  19. 19. Daniel Greenfeld @pydanny Different flavors• Twitter• Facebook• Google
  20. 20. Daniel Greenfeld @pydanny Different flavors• Twitter• Facebook• Google• Linkedin
  21. 21. Daniel Greenfeld @pydanny Different flavors• Twitter• Facebook • Github (YAY!)• Google• Linkedin
  22. 22. Daniel Greenfeld @pydanny Different flavors• Twitter• Facebook • Github (YAY!)• Google • Facebook ARGH• Linkedin
  23. 23. Daniel Greenfeld @pydanny Different flavors• Twitter• Facebook • Github (YAY!)• Google • Facebook ARGH• LinkedinThe OAUTH specification is not honored well
  24. 24. Daniel Greenfeld @pydanny Different flavors • Twitter • Facebook • Github (YAY!) • Google • Facebook ARGH • Linkedin The OAUTH specification is not honored wellImplementation changes are sometimes not announced
  25. 25. You want a tool used by many people
  26. 26. Many people means lots of eyes
  27. 27. Let’s find a tool!
  28. 28. Daniel Greenfeld @pydanny Django Auth Optionshttp://djangopackages.com/grids/g/authentication/
  29. 29. Daniel Greenfeld @pydanny Django Auth Options Dozens more if you scrollhttp://djangopackages.com/grids/g/authentication/
  30. 30. Daniel Greenfeld @pydanny Many problems• django-tastypie and Piston are for APIs• Most of these lack tests• Most of these lack documentation• Bad code smell
  31. 31. They all suck for OAUTH?
  32. 32. One Good Tool!
  33. 33. Daniel Greenfeld @pydanny django-social-auth• https://github.com/omab/django-social-auth• http://django-social-auth.rtfd.org
  34. 34. Daniel Greenfeld @pydannydjango-social-auth
  35. 35. Daniel Greenfeld @pydanny django-social-auth• Tests!
  36. 36. Daniel Greenfeld @pydanny django-social-auth• Tests!• Docs!
  37. 37. Daniel Greenfeld @pydanny django-social-auth• Tests!• Docs!• Good code smell!
  38. 38. Daniel Greenfeld @pydanny Statisticshttp://djangopackages.com/packages/p/django-social-auth/
  39. 39. Daniel Greenfeld @pydanny Statistics Many downloadshttp://djangopackages.com/packages/p/django-social-auth/
  40. 40. Daniel Greenfeld @pydanny Statistics Ongoing development Many downloadshttp://djangopackages.com/packages/p/django-social-auth/
  41. 41. Daniel Greenfeld @pydanny Statistics Ongoing development Many downloads Many eyes on the problemhttp://djangopackages.com/packages/p/django-social-auth/
  42. 42. Usingdjango-social-auth
  43. 43. Daniel Greenfeld @pydanny Get the dependencypip install django-social-auth==0.5.13
  44. 44. Daniel Greenfeld @pydanny Part I: settings.pyINSTALLED_APPS = ( ... social_auth, ...)AUTHENTICATION_BACKENDS = ( social_auth.backends.contrib.github.GithubBackend,# keep this so you have that admin level backend access! django.contrib.auth.backends.ModelBackend,)
  45. 45. Daniel Greenfeld @pydanny Part II: settings.pyfrom django.template.defaultfilters import slugifySOCIAL_AUTH_ENABLED_BACKENDS = (github,)SOCIAL_AUTH_COMPLETE_URL_NAME = socialauth_completeSOCIAL_AUTH_ASSOCIATE_URL_NAME = associate_completeSOCIAL_AUTH_DEFAULT_USERNAME = lambda u: slugify(u)SOCIAL_AUTH_EXTRA_DATA = FalseSOCIAL_AUTH_CHANGE_SIGNAL_ONLY = TrueSOCIAL_AUTH_ASSOCIATE_BY_MAIL = True # associate user via email (Usually you can just go with these as your settings)
  46. 46. Daniel Greenfeld @pydannyPart III: root urls.py urlpatterns = patterns("", url(, include(social_auth.urls)), ... )
  47. 47. Daniel Greenfeld @pydanny Part IV: profile/views.pyfrom social_auth.signals import pre_updatefrom social_auth.backends.contrib.github import GithubBackendfrom profiles.models. import Profiledef github_user_update(sender, user, response, details, **kwargs): profile_instance, created = Profile.objects.get_or_create(user=user) profile_instance.save() return Truepre_update.connect(github_user_update, sender=GithubBackend)(Not specifying this view in urls - django-social-auth does it for me)
  48. 48. Daniel Greenfeld @pydanny Try it yourself!http://djangopackages.com/login/
  49. 49. Thanks!

×