You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.1 KiB

  1. from authlib.integrations.base_client import OAuthError
  2. from authlib.integrations.django_client import OAuth
  3. from authlib.oauth2.rfc6749 import OAuth2Token
  4. from django.shortcuts import redirect
  5. from django.utils.deprecation import MiddlewareMixin
  6. from foerderbarometer import settings
  7. from input import views
  8. class OAuthMiddleware(MiddlewareMixin):
  9. def __init__(self, get_response=None):
  10. super().__init__(get_response)
  11. self.oauth = OAuth()
  12. def process_request(self, request):
  13. if settings.OAUTH_URL_WHITELISTS is not None:
  14. for w in settings.OAUTH_URL_WHITELISTS:
  15. if request.path.startswith(w):
  16. return self.get_response(request)
  17. def update_token(token, refresh_token, access_token):
  18. request.session['token'] = token
  19. print('oioi')
  20. print('oi token', token)
  21. return None
  22. sso_client = self.oauth.register(
  23. settings.OAUTH_CLIENT_NAME, overwrite=True, **settings.OAUTH_CLIENT, update_token=update_token
  24. )
  25. if request.path.startswith('/oauth/callback'):
  26. print('oi')
  27. self.clear_session(request)
  28. request.session['token'] = sso_client.authorize_access_token(request)
  29. print('blub', request.session['token'])
  30. print('user', self.get_current_user(sso_client, request))
  31. if self.get_current_user(sso_client, request) is not None:
  32. redirect_uri = request.session.pop('redirect_uri', None)
  33. if redirect_uri is not None:
  34. return redirect(redirect_uri)
  35. return redirect(views.ExternView)
  36. if request.session.get('token', None) is not None:
  37. current_user = self.get_current_user(sso_client, request)
  38. if current_user is not None:
  39. return self.get_response(request)
  40. # remember redirect URI for redirecting to the original URL.
  41. request.session['redirect_uri'] = request.path
  42. return sso_client.authorize_redirect(request, settings.OAUTH_CLIENT['redirect_uri'])
  43. # fetch current login user info
  44. # 1. check if it's in cache
  45. # 2. fetch from remote API when it's not in cache
  46. @staticmethod
  47. def get_current_user(sso_client, request):
  48. token = request.session.get('token', None)
  49. if token is None or 'access_token' not in token:
  50. return None
  51. if not OAuth2Token.from_dict(token).is_expired() and 'user' in request.session:
  52. return request.session['user']
  53. try:
  54. res = sso_client.get(settings.OAUTH_CLIENT['userinfo_endpoint'], token=OAuth2Token(token))
  55. print('json oi oi' , res.json())
  56. if res.ok:
  57. request.session['user'] = res.json()
  58. return res.json()
  59. except OAuthError as e:
  60. print(e)
  61. return None
  62. @staticmethod
  63. def clear_session(request):
  64. try:
  65. del request.session['user']
  66. del request.session['token']
  67. except KeyError:
  68. pass
  69. def __del__(self):
  70. print('destroyed')