auth.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from django.conf import settings
  2. from django.contrib import auth
  3. from django.shortcuts import redirect
  4. from django.utils.http import is_safe_url
  5. from django.utils.six.moves.urllib.parse import urlparse
  6. from django.views.decorators.cache import never_cache
  7. from django.views.decorators.csrf import csrf_protect
  8. from django.views.decorators.debug import sensitive_post_parameters
  9. from misago.core.embercli import is_ember_cli_request
  10. @sensitive_post_parameters()
  11. @never_cache
  12. @csrf_protect
  13. def login(request):
  14. if request.method == 'POST':
  15. redirect_to = request.POST.get('redirect_to')
  16. if redirect_to:
  17. is_redirect_safe = is_safe_url(
  18. url=redirect_to, host=request.get_host())
  19. if not is_redirect_safe and is_ember_cli_request(request):
  20. parsed_url = urlparse(settings.MISAGO_EMBER_CLI_ORIGIN)
  21. trusted_host = '%s:%s' % (parsed_url.hostname, parsed_url.port)
  22. is_redirect_safe = is_safe_url(
  23. url=redirect_to, host=trusted_host)
  24. if is_redirect_safe:
  25. redirect_to_path = urlparse(redirect_to).path
  26. return redirect(redirect_to_path)
  27. return redirect(settings.LOGIN_REDIRECT_URL)
  28. @never_cache
  29. @csrf_protect
  30. def logout(request):
  31. if request.method == 'POST' and request.user.is_authenticated():
  32. auth.logout(request)
  33. return redirect(settings.LOGIN_REDIRECT_URL)