14c2294ccd57176d8538e54cc80b8a32018146cc
[django-cas-provider.git] / cas_provider / models.py
1 from __future__ import unicode_literals
2
3 from django.conf import settings
4 from django.db import models
5 from django.utils.translation import ugettext_lazy as _
6 from random import Random
7 import string
8 try:
9     from urllib.parse import urlencode, urlparse, parse_qs, ParseResult
10 except ImportError:
11     from urllib import urlencode
12     from urlparse import urlparse, ParseResult
13     from urlparse import parse_qs
14
15 __all__ = ['ServiceTicket', 'LoginTicket', 'ProxyGrantingTicket', 'ProxyTicket', 'ProxyGrantingTicketIOU']
16
17 class BaseTicket(models.Model):
18     ticket = models.CharField(_('ticket'), max_length=32)
19     created = models.DateTimeField(_('created'), auto_now=True)
20
21     class Meta:
22         abstract = True
23
24     def __init__(self, *args, **kwargs):
25         if 'ticket' not in kwargs:
26             kwargs['ticket'] = self._generate_ticket()
27         super(BaseTicket, self).__init__(*args, **kwargs)
28
29     def __unicode__(self):
30         return self.ticket
31
32     def _generate_ticket(self, length=ticket.max_length, chars=string.ascii_letters + string.digits):
33         """ Generates a random string of the requested length. Used for creation of tickets. """
34         return "%s-%s" % (self.prefix, ''.join(Random().sample(chars, length - (len(self.prefix) + 1))))
35
36
37 class ServiceTicket(BaseTicket):
38     user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'), on_delete=models.CASCADE)
39     service = models.URLField(_('service'), max_length=2048)
40
41     prefix = 'ST'
42
43     class Meta:
44         verbose_name = _('Service Ticket')
45         verbose_name_plural = _('Service Tickets')
46
47     def get_redirect_url(self):
48         parsed = urlparse(self.service)
49         query = parse_qs(parsed.query)
50         query['ticket'] = [self.ticket]
51         query = [((k, v) if len(v) > 1 else (k, v[0])) for k, v in query.items()]
52         parsed = ParseResult(parsed.scheme, parsed.netloc,
53                                       parsed.path, parsed.params,
54                                       urlencode(query), parsed.fragment)
55         return parsed.geturl()
56
57
58 class LoginTicket(BaseTicket):
59     prefix = 'LT'
60
61     class Meta:
62         verbose_name = _('Login Ticket')
63         verbose_name_plural = _('Login Tickets')
64
65
66 class ProxyGrantingTicket(BaseTicket):
67     user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'), on_delete=models.CASCADE)
68     service = models.URLField(_('service'), null=True)
69     pgt = models.ForeignKey('self', null=True, on_delete=models.CASCADE)
70     pgtiou = models.CharField(max_length=256, verbose_name=_('PGTiou'))
71     prefix = 'PGT'
72
73     def __init__(self, *args, **kwargs):
74         if 'pgtiou' not in kwargs:
75             kwargs['pgtiou'] = "PGTIOU-%s" % (''.join(Random().sample(string.ascii_letters + string.digits, 50)))
76         super(ProxyGrantingTicket, self).__init__(*args, **kwargs)
77
78     class Meta:
79         verbose_name = _('Proxy Granting Ticket')
80         verbose_name_plural = _('Proxy Granting Tickets')
81
82
83 class ProxyTicket(ServiceTicket):
84     proxyGrantingTicket = models.ForeignKey(ProxyGrantingTicket, verbose_name=_('Proxy Granting Ticket'), on_delete=models.CASCADE)
85
86     prefix = 'PT'
87
88     class Meta:
89         verbose_name = _('Proxy Ticket')
90         verbose_name_plural = _('Proxy Tickets')
91
92
93 class ProxyGrantingTicketIOU(BaseTicket):
94     proxyGrantingTicket = models.ForeignKey(ProxyGrantingTicket, verbose_name=_('Proxy Granting Ticket'), on_delete=models.CASCADE)
95
96     prefix = 'PGTIOU'
97
98     class Meta:
99         verbose_name = _('Proxy Granting Ticket IOU')
100         verbose_name_plural = _('Proxy Granting Tickets IOU')
101