PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/api/authentication/OAuthService.py

https://gitlab.com/fdemian/Shelob
Python | 91 lines | 65 code | 25 blank | 1 comment | 5 complexity | 79a9bac102671f9de5db00eeadbff9a8 MD5 | raw file
  1. from tornado import gen
  2. from .FacebookService import FacebookAuthService
  3. from .GoogleService import GoogleAuthService
  4. from api.authentication.AuthExceptions import OAuthFailedException, NoSuchServiceException
  5. from sqlalchemy.orm.exc import MultipleResultsFound
  6. from api.model.sessionHelper import get_session
  7. from api.model.models import User, OAuthAccount
  8. from api.authentication.AuthExceptions import ExistingUserException
  9. from api.Utils import download_avatar, uglify_username
  10. class OAuthService:
  11. services = {
  12. "facebook": FacebookAuthService,
  13. "google": GoogleAuthService
  14. }
  15. def __init__(self, oauth_settings):
  16. self.oauth_settings = oauth_settings
  17. def get_service_instance(self, service_type):
  18. auth_service = self.services.get(service_type)
  19. if auth_service is None:
  20. raise NoSuchServiceException
  21. service_key = self.oauth_settings[service_type]["key"]
  22. service_secret = self.oauth_settings[service_type]["secret"]
  23. service_instance = auth_service(service_key, service_secret)
  24. return service_instance
  25. @gen.coroutine
  26. def get_user_by_service(self, service_type, auth_code, redirect_uri):
  27. service_instance = self.get_service_instance(service_type)
  28. user = yield service_instance.get(auth_code, redirect_uri, "login")
  29. if user is None:
  30. raise OAuthFailedException
  31. return user
  32. @gen.coroutine
  33. def register_user(self, service_type, auth_code, redirect_uri):
  34. service_instance = self.get_service_instance(service_type)
  35. oauth_user = yield service_instance.get(auth_code, redirect_uri, "register")
  36. uglified_username = uglify_username(oauth_user["username"])
  37. user_avatar = yield download_avatar(oauth_user["avatar"], uglified_username)
  38. user = User()
  39. user.username = oauth_user["username"]
  40. user.fullname = oauth_user["fullname"]
  41. user.email = oauth_user['email']
  42. user.valid = True # Identity verified by the oauth provider.
  43. user.password = None
  44. user.salt = None
  45. user.avatar = user_avatar
  46. oauth_account = OAuthAccount()
  47. oauth_account.oauth_id = oauth_user["id"]
  48. oauth_account.provider = service_type
  49. saved_user = self.save_user(user, oauth_account)
  50. oauth_user["avatar"] = user_avatar
  51. return oauth_user
  52. @staticmethod
  53. def save_user(user, oauth_account):
  54. # Save user.
  55. session_object = get_session()
  56. session = session_object()
  57. try:
  58. user_exists = session.query(User).filter(User.email == user.email).one_or_none()
  59. if user_exists is not None:
  60. raise ExistingUserException
  61. user.accounts.append(oauth_account)
  62. session.add(user)
  63. session.commit()
  64. return user
  65. except (MultipleResultsFound, ExistingUserException):
  66. return None