-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
executable file
·71 lines (58 loc) · 2.43 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from urlparse import urljoin
from urllib import urlencode, urlopen
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from cas.exceptions import CasTicketException, CasConfigException
# Ed Crewe - add in signals to delete old tickets
from django.db.models.signals import post_save
from datetime import datetime
class Tgt(models.Model):
username = models.CharField(max_length = 255, unique = True)
tgt = models.CharField(max_length = 255)
def get_proxy_ticket_for(self, service):
"""Verifies CAS 2.0+ XML-based authentication ticket.
Returns username on success and None on failure.
"""
if not settings.CAS_PROXY_CALLBACK:
raise CasConfigException("No proxy callback set in settings")
try:
from xml.etree import ElementTree
except ImportError:
from elementtree import ElementTree
params = {'pgt': self.tgt, 'targetService': service}
url = (urljoin(settings.CAS_SERVER_URL, 'proxy') + '?' +
urlencode(params))
page = urlopen(url)
try:
response = page.read()
tree = ElementTree.fromstring(response)
if tree[0].tag.endswith('proxySuccess'):
return tree[0][0].text
else:
raise CasTicketException("Failed to get proxy ticket")
finally:
page.close()
class PgtIOU(models.Model):
""" Proxy granting ticket and IOU """
pgtIou = models.CharField(max_length = 255, unique = True)
tgt = models.CharField(max_length = 255)
created = models.DateTimeField(auto_now = True)
def get_tgt_for(user):
if not settings.CAS_PROXY_CALLBACK:
raise CasConfigException("No proxy callback set in settings")
try:
return Tgt.objects.get(username = user.username)
except ObjectDoesNotExist:
raise CasTicketException("no ticket found for user " + user.username)
def delete_old_tickets(**kwargs):
""" Delete tickets if they are over 2 days old
kwargs = ['raw', 'signal', 'instance', 'sender', 'created']
"""
sender = kwargs.get('sender', None)
now = datetime.now()
expire = datetime(now.year, now.month, now.day - 2)
sender.objects.filter(created__lt=expire).delete()
post_save.connect(delete_old_tickets, sender=PgtIOU)
#post_save.connect(delete_old_tickets, sender=Tgt)