<commit_before>import base64
import logging

from django.conf import settings as django_settings
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import redirect, render
from django.utils.http import is_safe_url
from django.views.generic import View

from django_auth_adfs.config import provider_config
from django_auth_adfs.exceptions import MFARequired

logger = logging.getLogger("django_auth_adfs")


def render_failed_response(request, error_message, status):
    # Return an error message
    return render(request, 'django_auth_adfs/login_failed.html', {
        'error_message': error_message,
    }, status=status)


class OAuth2CallbackView(View):
    def get(self, request):
        """
        Handles the redirect from ADFS to our site.
        We try to process the passed authorization code and login the user.

        Args:
            request (django.http.request.HttpRequest): A Django Request object
        """
        code = request.GET.get("code")
        if not code:
            # Return an error message
            return django_settings.FAILED_RESPONSE_FUNCTION(
                request,
                error_message="No authorization code was provided.",
                status=400
            )

        redirect_to = request.GET.get("state")
        try:
            user = authenticate(request=request, authorization_code=code)
        except MFARequired:
            return redirect(provider_config.build_authorization_endpoint(request, force_mfa=True))

        if user:
            if user.is_active:
                login(request, user)
                # Redirect to the "after login" page.
                # Because we got redirected from ADFS, we can't know where the
                # user came from.
                if redirect_to:
                    redirect_to = base64.urlsafe_b64decode(redirect_to.encode()).decode()
                else:
                    redirect_to = django_settings.LOGIN_REDIRECT_URL
                url_is_safe = is_safe_url(
                    url=redirect_to,
                    allowed_hosts=[request.get_host()],
                    require_https=request.is_secure(),
                )
                redirect_to = redirect_to if url_is_safe else '/'
                return redirect(redirect_to)
            else:
                # Return a 'disabled account' error message
                return django_settings.FAILED_RESPONSE_FUNCTION(
                    request,
                    error_message="Your account is disabled.",
                    status=403
                )
        else:
            # Return an 'invalid login' error message
            return django_settings.FAILED_RESPONSE_FUNCTION(
                request,
                error_message="Login failed.",
                status=401
            )


class OAuth2LoginView(View):
    def get(self, request):<commit_msg>Initial pass at custom error function<commit_after>import base64
import logging

from django.conf import settings as django_settings
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import redirect, render
from django.utils.http import is_safe_url
from django.views.generic import View

from django_auth_adfs.config import provider_config, settings
from django_auth_adfs.exceptions import MFARequired

logger = logging.getLogger("django_auth_adfs")


def render_failed_response(request, error_message, status):
    # Return an error message
    return render(request, 'django_auth_adfs/login_failed.html', {
        'error_message': error_message,
    }, status=status)


class OAuth2CallbackView(View):
    def get(self, request):
        """
        Handles the redirect from ADFS to our site.
        We try to process the passed authorization code and login the user.

        Args:
            request (django.http.request.HttpRequest): A Django Request object
        """
        code = request.GET.get("code")
        if not code:
            # Return an error message
            return settings.FAILED_RESPONSE_FUNCTION(
                request,
                error_message="No authorization code was provided.",
                status=400
            )

        redirect_to = request.GET.get("state")
        try:
            user = authenticate(request=request, authorization_code=code)
        except MFARequired:
            return redirect(provider_config.build_authorization_endpoint(request, force_mfa=True))

        if user:
            if user.is_active:
                login(request, user)
                # Redirect to the "after login" page.
                # Because we got redirected from ADFS, we can't know where the
                # user came from.
                if redirect_to:
                    redirect_to = base64.urlsafe_b64decode(redirect_to.encode()).decode()
                else:
                    redirect_to = django_settings.LOGIN_REDIRECT_URL
                url_is_safe = is_safe_url(
                    url=redirect_to,
                    allowed_hosts=[request.get_host()],
                    require_https=request.is_secure(),
                )
                redirect_to = redirect_to if url_is_safe else '/'
                return redirect(redirect_to)
            else:
                # Return a 'disabled account' error message
                return settings.FAILED_RESPONSE_FUNCTION(
                    request,
                    error_message="Your account is disabled.",
                    status=403
                )
        else:
            # Return an 'invalid login' error message
            return settings.FAILED_RESPONSE_FUNCTION(
                request,
                error_message="Login failed.",
                status=401
            )


class OAuth2LoginView(View):
    def get(self, request):
{'fraction_non_alphanumeric': 0.06684997422237497, 'fraction_numerical': 0.00584292833820244, 'mean_word_length': 1.940879231935321, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6479067', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1425, 'n_words': 389}