/usr/lib/python2.7/dist-packages/cas/models.py is in python-django-casclient 1.2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | import logging
from datetime import datetime
try:
from xml.etree import ElementTree
except ImportError:
from elementtree import ElementTree
try:
from urlparse import urljoin
except ImportError:
from urllib.parse import urljoin
try:
from urllib import urlencode
except ImportError:
from urllib.parse import urlencode
try:
from urllib import urlopen
except ImportError:
from urllib.request import urlopen
from django.db import models
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.signals import post_save
from cas.exceptions import CasTicketException, CasConfigException
logger = logging.getLogger(__name__)
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.
:param: service
Returns username on success and None on failure.
"""
if not settings.CAS_PROXY_CALLBACK:
raise CasConfigException("No proxy callback set in settings")
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:
logger.warning('Failed to get proxy ticket')
raise CasTicketException('Failed to get proxy ticket: %s' % \
tree[0].text.strip())
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):
"""
Fetch a ticket granting ticket for a given user.
:param user: UserObj
:return: TGT or Exepction
"""
if not settings.CAS_PROXY_CALLBACK:
raise CasConfigException("No proxy callback set in settings")
try:
return Tgt.objects.get(username=user.username)
except ObjectDoesNotExist:
logger.warning('No ticket found for user {user}'.format(
user=user.username
))
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)
|