88d2926a80d20635868361e645ef368cdac015f3
[django-cas-provider.git] / cas_provider / forms.py
1 from django import forms
2 from django.contrib.auth.forms import AuthenticationForm
3 from django.contrib.auth import authenticate
4
5 from models import ServiceTicket, LoginTicket
6 from utils import create_login_ticket
7
8
9 class LoginForm(forms.Form):
10     email = forms.CharField(max_length=255)
11     password = forms.CharField(widget=forms.PasswordInput)
12     #warn = forms.BooleanField(required=False)  # TODO: Implement
13     lt = forms.CharField(widget=forms.HiddenInput, initial=create_login_ticket, required=False)
14     service = forms.CharField(widget=forms.HiddenInput, required=False)
15     remember_me = forms.BooleanField(required=False)
16
17     def __init__(self, *args, **kwargs):
18         # renew = kwargs.pop('renew', None)
19         # gateway = kwargs.pop('gateway', None)
20         request = kwargs.pop('request', None)
21         super(LoginForm, self).__init__(*args, **kwargs)
22         self.request = request
23
24     def clean_remember_me(self):
25         remember = self.cleaned_data['remember_me']
26         if not remember and self.request is not None:
27             self.request.session.set_expiry(0)
28
29     def clean_lt(self):
30         lt = self.cleaned_data.get('lt',
31                                    self.initial.get('lt', None))
32         if lt is None:
33             lt = self.fields['lt'].initial()
34         try:
35             login_ticket = LoginTicket.objects.get(ticket=lt)
36         except LoginTicket.DoesNotExist:
37             raise forms.ValidationError("Login ticket expired. Please try again.")
38         else:
39             login_ticket.delete()
40             
41
42 class MergeLoginForm(LoginForm):
43     email = forms.CharField(max_length=255, widget=forms.HiddenInput)