<commit_before># -*- coding: utf-8 -*-

# This file is part of Casia - CAS server based on Django
# Copyright (C) 2013 <NAME>

# Casia is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# You should have received a copy of the GNU Affero General Public License
# along with Casia. If not, see <http://www.gnu.org/licenses/>.

from xml.etree.ElementTree import Element, SubElement

from django.contrib.auth.views import redirect_to_login
from django.core.urlresolvers import reverse
from django.http.response import HttpResponse
from django.shortcuts import get_object_or_404, redirect

from casia.cas.exceptions import Error
from casia.cas.models import TicketRequest
from casia.cas.utils import validate_ticket, issue_service_ticket
from casia.http.response import XMLResponse

def validate(request):
    st = None
    try:
        st = validate_ticket(request)
    except Error:
        pass
    return HttpResponse('yes\n%s\n' % st.user if st else 'no\n\n')

def service_validate(request):
    response = Element('cas:serviceResponse',
                       attrib={'xmlns:cas': 'http://www.yale.edu/tp/cas'})
    try:
        st = validate_ticket(request)
        auth_success = SubElement(response, 'cas:authenticationSuccess')
        user = SubElement(auth_success, 'cas:user')
        user.text = st.user.get_username()
    except Error as ex:
        auth_failure = SubElement(response, 'cas:authenticationFailure',
                                  attrib={'code': ex.code})
        auth_failure.text = ex.msg
    return XMLResponse(response)

def login(request):
    ticket_request = TicketRequest()
    ticket_request.url = request.GET.get('service')
    if ticket_request.url:
        ticket_request.session_id = request.session.session_key
        if request.user.is_authenticated():
            ticket_request.user = request.user
        ticket_request.save()
        target = reverse('cas_issue',
                     kwargs={'ticket_request_id': ticket_request.id})
        return redirect(target)
    else:
        return redirect('index')

def issue(request, ticket_request_id):
    ticket_request = get_object_or_404(TicketRequest, id=ticket_request_id)
    if ticket_request.user:
        return issue_service_ticket(ticket_request)
    else:
        target = reverse('cas_issue',
                         kwargs={'ticket_request_id': ticket_request.id})
        return redirect_to_login(target)
<commit_msg>Support for issuing tickets on logon<commit_after># -*- coding: utf-8 -*-

# This file is part of Casia - CAS server based on Django
# Copyright (C) 2013 <NAME>

# Casia is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# You should have received a copy of the GNU Affero General Public License
# along with Casia. If not, see <http://www.gnu.org/licenses/>.

from xml.etree.ElementTree import Element, SubElement

from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.signals import user_logged_in
from django.contrib.auth.views import redirect_to_login
from django.core.urlresolvers import resolve, reverse
from django.dispatch import receiver
from django.http.response import HttpResponse
from django.shortcuts import get_object_or_404, redirect

from casia.cas.exceptions import Error
from casia.cas.models import TicketRequest
from casia.cas.utils import validate_ticket, issue_service_ticket
from casia.http.response import XMLResponse

def validate(request):
    st = None
    try:
        st = validate_ticket(request)
    except Error:
        pass
    return HttpResponse('yes\n%s\n' % st.user if st else 'no\n\n')

def service_validate(request):
    response = Element('cas:serviceResponse',
                       attrib={'xmlns:cas': 'http://www.yale.edu/tp/cas'})
    try:
        st = validate_ticket(request)
        auth_success = SubElement(response, 'cas:authenticationSuccess')
        user = SubElement(auth_success, 'cas:user')
        user.text = st.user.get_username()
    except Error as ex:
        auth_failure = SubElement(response, 'cas:authenticationFailure',
                                  attrib={'code': ex.code})
        auth_failure.text = ex.msg
    return XMLResponse(response)

def login(request):
    ticket_request = TicketRequest()
    ticket_request.url = request.GET.get('service')
    if ticket_request.url:
        if request.user.is_authenticated():
            ticket_request.session_id = request.session.session_key
            ticket_request.user = request.user
        ticket_request.save()
        target = reverse('cas_issue',
                         kwargs={'ticket_request_id': ticket_request.id})
        return redirect(target)
    else:
        return redirect('index')

def issue(request, ticket_request_id):
    ticket_request = get_object_or_404(TicketRequest, id=ticket_request_id)
    if ticket_request.user:
        return issue_service_ticket(ticket_request)
    else:
        target = reverse('cas_issue',
                         kwargs={'ticket_request_id': ticket_request.id})
        return redirect_to_login(target)

@receiver(user_logged_in)
def ticket_request_updater(sender, request, user, **kwargs):
    target = request.GET.get(REDIRECT_FIELD_NAME)
    if target:
        target = resolve(target)
        if target.url_name == 'cas_issue':
            ticket_request_id = target.kwargs.get('ticket_request_id')
            ticket_request = TicketRequest.objects.get(id=ticket_request_id)
            ticket_request.user = request.user
            ticket_request.session_id = request.session.session_key
            ticket_request.save()

{'fraction_non_alphanumeric': 0.08941176470588236, 'fraction_numerical': 0.004033613445378151, 'mean_word_length': 3.238603988603989, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4694598', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1663, 'n_words': 454}