Commit 871baaac authored by Valentin Samir's avatar Valentin Samir

some code refactoring and better error handling on ticket validation

parent 7e2917e9
......@@ -32,7 +32,7 @@ class Migration(migrations.Migration):
('service', models.TextField()),
('creation', models.DateTimeField(auto_now_add=True)),
('renew', models.BooleanField(default=False)),
('value', models.CharField(default=cas_server.models._gen_pgt, unique=True, max_length=255)),
('value', models.CharField(default=cas_server.utils.gen_pgt, unique=True, max_length=255)),
],
options={
'abstract': False,
......@@ -48,7 +48,7 @@ class Migration(migrations.Migration):
('service', models.TextField()),
('creation', models.DateTimeField(auto_now_add=True)),
('renew', models.BooleanField(default=False)),
('value', models.CharField(default=cas_server.models._gen_pt, unique=True, max_length=255)),
('value', models.CharField(default=cas_server.utils.gen_pt, unique=True, max_length=255)),
],
options={
'abstract': False,
......@@ -81,7 +81,7 @@ class Migration(migrations.Migration):
('service', models.TextField()),
('creation', models.DateTimeField(auto_now_add=True)),
('renew', models.BooleanField(default=False)),
('value', models.CharField(default=cas_server.models._gen_st, unique=True, max_length=255)),
('value', models.CharField(default=cas_server.utils.gen_st, unique=True, max_length=255)),
],
options={
'abstract': False,
......
......@@ -10,9 +10,6 @@
#
# (c) 2015 Valentin Samir
"""models for the app"""
from . import default_settings
from django.conf import settings
from django.db import models
from django.contrib import messages
from picklefield.fields import PickledObjectField
......@@ -21,41 +18,12 @@ from django.utils import timezone
import re
import os
import random
import string
from concurrent.futures import ThreadPoolExecutor
from requests_futures.sessions import FuturesSession
from . import utils
def _gen_ticket(prefix):
"""Generate a ticket with prefix `prefix`"""
return '%s-%s' % (
prefix,
''.join(
random.choice(
string.ascii_letters + string.digits
) for _ in range(settings.CAS_ST_LEN)
)
)
def _gen_st():
"""Generate a Service Ticket"""
return _gen_ticket('ST')
def _gen_pt():
"""Generate a Proxy Ticket"""
return _gen_ticket('PT')
def _gen_pgt():
"""Generate a Proxy Granting Ticket"""
return _gen_ticket('PGT')
def gen_pgtiou():
"""Generate a Proxy Granting Ticket IOU"""
return _gen_ticket('PGTIOU')
class User(models.Model):
"""A user logged into the CAS"""
username = models.CharField(max_length=30, unique=True)
......@@ -83,10 +51,11 @@ class User(models.Model):
try:
future.result()
except Exception as error:
error = utils.unpack_nested_exception(error)
messages.add_message(
request,
messages.WARNING,
_(u'Error during service logout %r') % error
_(u'Error during service logout %s') % error
)
def get_ticket(self, ticket_class, service, service_pattern, renew):
......@@ -333,6 +302,7 @@ class Ticket(models.Model):
headers=headers
)
except Exception as error:
error = utils.unpack_nested_exception(error)
messages.add_message(
request,
messages.WARNING,
......@@ -342,17 +312,17 @@ class Ticket(models.Model):
class ServiceTicket(Ticket):
"""A Service Ticket"""
value = models.CharField(max_length=255, default=_gen_st, unique=True)
value = models.CharField(max_length=255, default=utils.gen_st, unique=True)
def __unicode__(self):
return u"ServiceTicket(%s, %s, %s)" % (self.user, self.value, self.service)
class ProxyTicket(Ticket):
"""A Proxy Ticket"""
value = models.CharField(max_length=255, default=_gen_pt, unique=True)
value = models.CharField(max_length=255, default=utils.gen_pt, unique=True)
def __unicode__(self):
return u"ProxyTicket(%s, %s, %s)" % (self.user, self.value, self.service)
class ProxyGrantingTicket(Ticket):
"""A Proxy Granting Ticket"""
value = models.CharField(max_length=255, default=_gen_pgt, unique=True)
value = models.CharField(max_length=255, default=utils.gen_pgt, unique=True)
def __unicode__(self):
return u"ProxyGrantingTicket(%s, %s, %s)" % (self.user, self.value, self.service)
......
......@@ -7,8 +7,7 @@
MajorVersion="1" MinorVersion="1" Recipient="{{Recipient}}"
ResponseID="{{ResponseID}}">
<Status>
<StatusCode Value="samlp:{{code}}">
</StatusCode>
<StatusCode Value="samlp:{{code}}">{{msg}}</StatusCode>
</Status>
</Response>
</SOAP-ENV:Body>
......
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationFailure code="{{code}}">
{{msg}}
</cas:authenticationFailure>
<cas:authenticationFailure code="{{code}}">{{msg}}</cas:authenticationFailure>
</cas:serviceResponse>
......@@ -9,8 +9,14 @@
#
# (c) 2015 Valentin Samir
"""Some util function for the app"""
from . import default_settings
from django.conf import settings
import urlparse
import urllib
import random
import string
def update_url(url, params):
"""update params in the `url` query string"""
......@@ -19,3 +25,46 @@ def update_url(url, params):
query.update(params)
url_parts[4] = urllib.urlencode(query)
return urlparse.urlunparse(url_parts)
def unpack_nested_exception(error):
"""If exception are stacked, return the first one"""
i = 0
while True:
if error.args[i:]:
if isinstance(error.args[i], Exception):
error = error.args[i]
i = 0
else:
i += 1
else:
break
return error
def _gen_ticket(prefix):
"""Generate a ticket with prefix `prefix`"""
return '%s-%s' % (
prefix,
''.join(
random.choice(
string.ascii_letters + string.digits
) for _ in range(settings.CAS_ST_LEN)
)
)
def gen_st():
"""Generate a Service Ticket"""
return _gen_ticket('ST')
def gen_pt():
"""Generate a Proxy Ticket"""
return _gen_ticket('PT')
def gen_pgt():
"""Generate a Proxy Granting Ticket"""
return _gen_ticket('PGT')
def gen_pgtiou():
"""Generate a Proxy Granting Ticket IOU"""
return _gen_ticket('PGTIOU')
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment