8a1452af0629f1c75d910b874a6b68cbd80d5c36
[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, initial='', 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',
32                                                     self.fields['lt'].initial()))
33         try:
34             login_ticket = LoginTicket.objects.get(ticket=lt)
35         except:
36             raise forms.ValidationError("Login ticket expired. Please try again.")
37         else:
38             login_ticket.delete()
39             
40
41 class MergeLoginForm(LoginForm):
42     email = forms.CharField(max_length=255, widget=forms.HiddenInput)