# Path: auth/util.py
# def prepare_django_request(request):
#     # If server is behind proxys or balancers use the HTTP_X_FORWARDED fields
#     http_host = request.META.get('HTTP_HOST', None)
#     if 'HTTP_X_FORWARDED_FOR' in request.META:
#         # This is only true for my particular installations.
#         server_port = None
#         https = request.META.get('HTTP_X_FORWARDED_PROTO') == 'https'
#     else:
#         server_port = request.META.get('SERVER_PORT')
#         https = request.is_secure()
#     prepared = {
#         'https': 'on' if https else 'off',
#         'http_host': http_host,
#         'script_name': request.META['PATH_INFO'],
#         'get_data': request.GET.copy(),
#         'post_data': request.POST.copy()
#     }
#     if server_port:
#         # Empty port will make a (lonely) colon ':' appear on the URL, so
#         # it's better not to include it at all.
#         prepared['server_port'] = server_port
#     return prepared


from django.contrib.auth import login, authenticate
from django.shortcuts import render, redirect
from django.conf import settings
from django.views.generic import View
from django.http import HttpResponse, HttpResponseServerError, HttpResponseBadRequest, HttpResponseNotFound
from .util import prepare_django_request
from onelogin.saml2.settings import OneLogin_Saml2_Settings
from onelogin.saml2.auth import OneLogin_Saml2_Auth
from onelogin.saml2.utils import OneLogin_Saml2_Utils
    def get(self, request):
        return HttpResponseNotFound()

    def post(self, request):
        req = prepare_django_request(request)
        auth = OneLogin_Saml2_Auth(req, self.get_settings())

        auth.process_response()

        errors = auth.get_errors()

        if not errors:
            if auth.is_authenticated:
                user = authenticate(request, saml_authentication=auth)
                login(self.request, user)
                if 'RelayState' in req['post_data'] and OneLogin_Saml2_Utils.get_self_url(req) != req['post_data'][
                        'RelayState']:
                    return redirect(auth.redirect_to(req['post_data']['RelayState']))
                else:
                    return redirect('/')
            else:
                raise PermissionDenied()
        else:
            if settings.DEBUG:
                print(auth.get_last_error_reason())
            return HttpResponseBadRequest("Erro processing SAML response ".join(errors))


class MetadataView(SettingsMixin, View):
    def get(self, request, *args, **kwargs):

# Path: auth/util.py
# def prepare_django_request(request):
#     # If server is behind proxys or balancers use the HTTP_X_FORWARDED fields
#     http_host = request.META.get('HTTP_HOST', None)
#     if 'HTTP_X_FORWARDED_FOR' in request.META:
#         # This is only true for my particular installations.
#         server_port = None
#         https = request.META.get('HTTP_X_FORWARDED_PROTO') == 'https'
#     else:
#         server_port = request.META.get('SERVER_PORT')
#         https = request.is_secure()
#     prepared = {
#         'https': 'on' if https else 'off',
#         'http_host': http_host,
#         'script_name': request.META['PATH_INFO'],
#         'get_data': request.GET.copy(),
#         'post_data': request.POST.copy()
#     }
#     if server_port:
#         # Empty port will make a (lonely) colon ':' appear on the URL, so
#         # it's better not to include it at all.
#         prepared['server_port'] = server_port
#     return prepared


# Path: auth/views.py
from django.contrib.auth import login, authenticate
from django.shortcuts import render, redirect
from django.conf import settings
from django.views.generic import View
from django.http import HttpResponse, HttpResponseServerError, HttpResponseBadRequest, HttpResponseNotFound
from .util import prepare_django_request
from onelogin.saml2.settings import OneLogin_Saml2_Settings
from onelogin.saml2.auth import OneLogin_Saml2_Auth
from onelogin.saml2.utils import OneLogin_Saml2_Utils

    def get(self, request):
        return HttpResponseNotFound()

    def post(self, request):
        req = prepare_django_request(request)
        auth = OneLogin_Saml2_Auth(req, self.get_settings())

        auth.process_response()

        errors = auth.get_errors()

        if not errors:
            if auth.is_authenticated:
                user = authenticate(request, saml_authentication=auth)
                login(self.request, user)
                if 'RelayState' in req['post_data'] and OneLogin_Saml2_Utils.get_self_url(req) != req['post_data'][
                        'RelayState']:
                    return redirect(auth.redirect_to(req['post_data']['RelayState']))
                else:
                    return redirect('/')
            else:
                raise PermissionDenied()
        else:
            if settings.DEBUG:
                print(auth.get_last_error_reason())
            return HttpResponseBadRequest("Erro processing SAML response ".join(errors))


class MetadataView(SettingsMixin, View):
    def get(self, request, *args, **kwargs):

        req = prepare_django_request(request)