PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/social/backends/bitbucket.py

https://github.com/haftrine/python-social-auth
Python | 59 lines | 47 code | 4 blank | 8 comment | 6 complexity | b41ceacfbeea38fe56541e238bf980e1 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Bitbucket OAuth1 backend, docs at:
  3. http://psa.matiasaguirre.net/docs/backends/bitbucket.html
  4. """
  5. from social.exceptions import AuthForbidden
  6. from social.backends.oauth import BaseOAuth1
  7. class BitbucketOAuth(BaseOAuth1):
  8. """Bitbucket OAuth authentication backend"""
  9. name = 'bitbucket'
  10. ID_KEY = 'username'
  11. AUTHORIZATION_URL = 'https://bitbucket.org/api/1.0/oauth/authenticate'
  12. REQUEST_TOKEN_URL = 'https://bitbucket.org/api/1.0/oauth/request_token'
  13. ACCESS_TOKEN_URL = 'https://bitbucket.org/api/1.0/oauth/access_token'
  14. EXTRA_DATA = [
  15. ('username', 'username'),
  16. ('expires', 'expires'),
  17. ('email', 'email'),
  18. ('first_name', 'first_name'),
  19. ('last_name', 'last_name')
  20. ]
  21. def get_user_details(self, response):
  22. """Return user details from Bitbucket account"""
  23. fullname, first_name, last_name = self.get_user_names(
  24. first_name=response.get('first_name', ''),
  25. last_name=response.get('last_name', '')
  26. )
  27. return {'username': response.get('username') or '',
  28. 'email': response.get('email') or '',
  29. 'fullname': fullname,
  30. 'first_name': first_name,
  31. 'last_name': last_name}
  32. def user_data(self, access_token):
  33. """Return user data provided"""
  34. # Bitbucket has a bit of an indirect route to obtain user data from an
  35. # authenticated query: First obtain the user's email via an
  36. # authenticated GET, then retrieve the user's primary email address or
  37. # the top email
  38. emails = self.get_json('https://bitbucket.org/api/1.0/emails/',
  39. auth=self.oauth_auth(access_token))
  40. email = None
  41. for address in reversed(emails):
  42. if address['active']:
  43. email = address['email']
  44. if address['primary']:
  45. break
  46. if email:
  47. return dict(self.get_json('https://bitbucket.org/api/1.0/users/' +
  48. email)['user'],
  49. email=email)
  50. elif self.setting('VERIFIED_EMAILS_ONLY', False):
  51. raise AuthForbidden(self,
  52. 'Bitbucket account has any verified email')
  53. else:
  54. return {}