1 from django.conf import settings
2 from django.db import models
3 from django.utils.translation import ugettext_lazy as _
4 from random import Random
6 from urllib.parse import urlencode, urlparse, parse_qs, ParseResult
9 __all__ = ['ServiceTicket', 'LoginTicket', 'ProxyGrantingTicket', 'ProxyTicket', 'ProxyGrantingTicketIOU']
11 class BaseTicket(models.Model):
12 ticket = models.CharField(_('ticket'), max_length=32)
13 created = models.DateTimeField(_('created'), auto_now=True)
18 def __init__(self, *args, **kwargs):
19 if 'ticket' not in kwargs:
20 kwargs['ticket'] = self._generate_ticket()
21 super(BaseTicket, self).__init__(*args, **kwargs)
23 def __unicode__(self):
26 def _generate_ticket(self, length=ticket.max_length, chars=string.ascii_letters + string.digits):
27 """ Generates a random string of the requested length. Used for creation of tickets. """
28 return "%s-%s" % (self.prefix, ''.join(Random().sample(chars, length - (len(self.prefix) + 1))))
31 class ServiceTicket(BaseTicket):
32 user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'), on_delete=models.CASCADE)
33 service = models.URLField(_('service'), max_length=2048)
38 verbose_name = _('Service Ticket')
39 verbose_name_plural = _('Service Tickets')
41 def get_redirect_url(self):
42 parsed = urlparse(self.service)
43 query = parse_qs(parsed.query)
44 query['ticket'] = [self.ticket]
45 query = [((k, v) if len(v) > 1 else (k, v[0])) for k, v in query.items()]
46 parsed = ParseResult(parsed.scheme, parsed.netloc,
47 parsed.path, parsed.params,
48 urlencode(query), parsed.fragment)
49 return parsed.geturl()
52 class LoginTicket(BaseTicket):
56 verbose_name = _('Login Ticket')
57 verbose_name_plural = _('Login Tickets')
60 class ProxyGrantingTicket(BaseTicket):
61 user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'), on_delete=models.CASCADE)
62 service = models.URLField(_('service'), null=True)
63 pgt = models.ForeignKey('self', null=True, on_delete=models.CASCADE)
64 pgtiou = models.CharField(max_length=256, verbose_name=_('PGTiou'))
67 def __init__(self, *args, **kwargs):
68 if 'pgtiou' not in kwargs:
69 kwargs['pgtiou'] = "PGTIOU-%s" % (''.join(Random().sample(string.ascii_letters + string.digits, 50)))
70 super(ProxyGrantingTicket, self).__init__(*args, **kwargs)
73 verbose_name = _('Proxy Granting Ticket')
74 verbose_name_plural = _('Proxy Granting Tickets')
77 class ProxyTicket(ServiceTicket):
78 proxyGrantingTicket = models.ForeignKey(ProxyGrantingTicket, verbose_name=_('Proxy Granting Ticket'), on_delete=models.CASCADE)
83 verbose_name = _('Proxy Ticket')
84 verbose_name_plural = _('Proxy Tickets')
87 class ProxyGrantingTicketIOU(BaseTicket):
88 proxyGrantingTicket = models.ForeignKey(ProxyGrantingTicket, verbose_name=_('Proxy Granting Ticket'), on_delete=models.CASCADE)
93 verbose_name = _('Proxy Granting Ticket IOU')
94 verbose_name_plural = _('Proxy Granting Tickets IOU')