commit 63efc637a2fabc8bd521a26d507054488bccdaee
Author: Georgios Gousios <gousiosg@gmail.com>
Date:   Mon Apr 18 14:56:32 2011 +0300

    Various fixes to make AAI work for real
    
    Refs: #345

diff --git a/aai/middleware.py b/aai/middleware.py
index c9d0143..95207a7 100644
--- a/aai/middleware.py
+++ b/aai/middleware.py
@@ -12,37 +12,36 @@ class SynnefoAuthMiddleware(object):
     auth_key   = "X-Auth-Key"
 
     def process_request(self, request):
-
         if self.auth_token in request.META:
             user = None
             #Retrieve user from DB or other caching mechanism
             try:
                 user = SynnefoUser.objects.get(auth_token = request.META[self.auth_token])
             except SynnefoUser.DoesNotExist:
-                return HttpResponseRedirect(settings.LOGIN_PATH)
+                return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
 
             #Check user's auth token
             if (time.time() -
                 time.mktime(user.auth_token_created.timetuple()) +
                 settings.AUTH_TOKEN_DURATION * 3600) > 0:
                 #The user's token has expired, re-login
-                return HttpResponseRedirect(settings.LOGIN_PATH)
+                return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
 
             request.user = user
             return
 
         #A user authenticated by Shibboleth, must include a uniq id
-        if Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME in request.META:
+        if Tokens.SIB_EPPN in request.META:
             #We must somehow make sure that we only process
             #SIB headers when coming from a URL whitelist,
             #or a similar form of restriction
-            if request.get_host() not in settings.SHIBBOLETH_WHITELIST.keys():
-                return HttpResponseRedirect(settings.LOGIN_PATH)
+            #if request.get_host() not in settings.SHIBBOLETH_WHITELIST.keys():
+            #    return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
 
             user = None
             try:
                 user = SynnefoUser.objects.get(
-                    uniq = request.META[Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME])
+                    uniq = request.META[Tokens.SIB_EPPN])
             except SynnefoUser.DoesNotExist:
                 pass
 
@@ -51,14 +50,14 @@ class SynnefoAuthMiddleware(object):
                 #Attempt to register the incoming user
                 if register_shibboleth_user(request.META):
                     user = SynnefoUser.objects.get(
-                        uniq = request.META[Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME])
+                        uniq = request.META[Tokens.SIB_EPPN])
                     response = HttpResponse()
                     response[self.auth_token] = user.auth_token
-                    response['Location'] = "/"
+                    response['Location'] = settings.APP_INSTALL_URL 
                     response.status_code = 302
                     return response
                 else:
-                    return HttpResponseRedirect(settings.LOGIN_PATH)
+                    return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
 
             #User and authentication token valid, user allowed to proceed
             return
@@ -87,14 +86,14 @@ class SynnefoAuthMiddleware(object):
 
         if settings.TEST:
             if 'TEST-AAI' in request.META:
-                return HttpResponseRedirect(settings.LOGIN_PATH)
+                return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
         else:
             #Avoid redirect loops
-            if 'Referer' in request.META and request.META['Referer'].endswith(settings.LOGIN_PATH):
-                return
+            if request.path.endswith(settings.LOGIN_PATH): 
+                return 
             else :
                 #No authentication info found in headers, redirect to Shibboleth
-                return HttpResponseRedirect(settings.LOGIN_PATH)
+                return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
 
     def process_response(self, request, response):
         #Tell proxies and other interested parties that the
@@ -102,3 +101,4 @@ class SynnefoAuthMiddleware(object):
         #caching of results
         response['Vary'] = self.auth_token
         return response
+
