<commit_before>"""CAS login/logout replacement views"""

from __future__ import absolute_import
from __future__ import unicode_literals

from django.utils.six.moves import urllib_parse
from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponseForbidden
from django.http import HttpResponse
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import (
    logout as auth_logout,
    login as auth_login,
    authenticate
)
from django.contrib import messages
from django.contrib.sessions.models import Session

from datetime import timedelta

from .models import ProxyGrantingTicket, SessionTicket
from .utils import (get_cas_client, get_service_url,
                    get_protocol, get_redirect_url)

__all__ = ['login', 'logout', 'callback']


@csrf_exempt
def login(request, next_page=None, required=False):
    """Forwards to CAS login URL or verifies CAS ticket"""
    service_url = get_service_url(request, next_page)
    client = get_cas_client(service_url=service_url)

    if request.method == 'POST' and request.POST.get('logoutRequest'):
        for slo in client.get_saml_slos(request.POST.get('logoutRequest')):
            try:
                SessionTicket.objects.get(ticket=slo.text).session.delete()
            except SessionTicket.DoesNotExist:
                pass
    if not next_page:
        next_page = get_redirect_url(request)
    if request.user.is_authenticated():
        if settings.CAS_DISPLAY_WELCOME_MESSAGE:
            message = "You are logged in as %s." % request.user.get_username()
            messages.success(request, message)
        return HttpResponseRedirect(next_page)

    ticket = request.GET.get('ticket')
    if ticket:
        user = authenticate(ticket=ticket,
                            service=service_url,
                            request=request)
        pgtiou = request.session.get("pgtiou")
        if user is not None:
            auth_login(request, user)
            if not request.session.exists(request.session.session_key):
                request.session.create()
            session = Session.objects.get(
                session_key=request.session.session_key
            )
            SessionTicket.objects.create(
                session=session,
                ticket=ticket
            )

            if pgtiou and settings.CAS_PROXY_CALLBACK:
                # Delete old PGT
                ProxyGrantingTicket.objects.filter(
                    user=user,
                    session=session
                ).delete()
                # Set new PGT ticket
                try:
                    pgt = ProxyGrantingTicket.objects.get(pgtiou=pgtiou)
                    pgt.user = user
                    pgt.session = session
                    pgt.save()
                except ProxyGrantingTicket.DoesNotExist:
                    pass
                del request.session["pgtiou"]

            if settings.CAS_DISPLAY_WELCOME_MESSAGE:
                name = user.get_username()<commit_msg>forgot default should be true<commit_after>"""CAS login/logout replacement views"""

from __future__ import absolute_import
from __future__ import unicode_literals

from django.utils.six.moves import urllib_parse
from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponseForbidden
from django.http import HttpResponse
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import (
    logout as auth_logout,
    login as auth_login,
    authenticate
)
from django.contrib import messages
from django.contrib.sessions.models import Session

from datetime import timedelta

from .models import ProxyGrantingTicket, SessionTicket
from .utils import (get_cas_client, get_service_url,
                    get_protocol, get_redirect_url)

__all__ = ['login', 'logout', 'callback']


@csrf_exempt
def login(request, next_page=None, required=False):
    """Forwards to CAS login URL or verifies CAS ticket"""
    service_url = get_service_url(request, next_page)
    client = get_cas_client(service_url=service_url)

    if request.method == 'POST' and request.POST.get('logoutRequest'):
        for slo in client.get_saml_slos(request.POST.get('logoutRequest')):
            try:
                SessionTicket.objects.get(ticket=slo.text).session.delete()
            except SessionTicket.DoesNotExist:
                pass
    if not next_page:
        next_page = get_redirect_url(request)
    if request.user.is_authenticated():
        if getattr(settings, 'CAS_DISPLAY_WELCOME_MESSAGE', True):
            message = "You are logged in as %s." % request.user.get_username()
            messages.success(request, message)
        return HttpResponseRedirect(next_page)

    ticket = request.GET.get('ticket')
    if ticket:
        user = authenticate(ticket=ticket,
                            service=service_url,
                            request=request)
        pgtiou = request.session.get("pgtiou")
        if user is not None:
            auth_login(request, user)
            if not request.session.exists(request.session.session_key):
                request.session.create()
            session = Session.objects.get(
                session_key=request.session.session_key
            )
            SessionTicket.objects.create(
                session=session,
                ticket=ticket
            )

            if pgtiou and settings.CAS_PROXY_CALLBACK:
                # Delete old PGT
                ProxyGrantingTicket.objects.filter(
                    user=user,
                    session=session
                ).delete()
                # Set new PGT ticket
                try:
                    pgt = ProxyGrantingTicket.objects.get(pgtiou=pgtiou)
                    pgt.user = user
                    pgt.session = session
                    pgt.save()
                except ProxyGrantingTicket.DoesNotExist:
                    pass
                del request.session["pgtiou"]

            if getattr(settings, 'CAS_DISPLAY_WELCOME_MESSAGE', True):
                name = user.get_username()
{'fraction_non_alphanumeric': 0.07520981278244028, 'fraction_numerical': 0.0, 'mean_word_length': 2.1828454031843862, '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': '6849658', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1635, 'n_words': 383}