Added support for custom attributes from CAS version 3.0 (see setting CAS_CUSTOM_ATTR...
[django-cas-provider.git] / cas_provider / models.py
index 94023b7..eae9cf6 100644 (file)
@@ -1,5 +1,9 @@
 from django.db import models
 from django.contrib.auth.models import User
+from django.conf import settings
+from django.core.urlresolvers import get_callable
+
+from cas_provider.xml import etree, register_namespace, ElementRoot
 
 class ServiceTicket(models.Model):
     user = models.ForeignKey(User)
@@ -15,4 +19,23 @@ class LoginTicket(models.Model):
     created = models.DateTimeField(auto_now=True)
     
     def __unicode__(self):
-        return "%s - %s" % (self.ticket, self.created)
\ No newline at end of file
+        return "%s - %s" % (self.ticket, self.created)
+
+CAS_URI = 'http://www.yale.edu/tp/cas'
+register_namespace('cas', CAS_URI)
+CAS = '{%s}' % CAS_URI
+
+def auth_success_response(user):
+    attrs = {}
+    if settings.CAS_CUSTOM_ATTRIBUTES_CALLBACK:
+        callback = get_callable(settings.CAS_CUSTOM_ATTRIBUTES_CALLBACK)
+        attrs = callback(user)
+    
+    response = ElementRoot(CAS + 'serviceResponse')
+    auth_success = etree.SubElement(response, CAS + 'authenticationSuccess')
+    username = etree.SubElement(auth_success, CAS + 'user')
+    username.text = user.username
+    for name, value in attrs.items():
+        element = etree.SubElement(auth_success, name)
+        element.text = value
+    return unicode(etree.tostring(response, encoding='utf-8'), 'utf-8')