Index: main/waeup.cas/trunk/waeup/cas/server.py
===================================================================
--- main/waeup.cas/trunk/waeup/cas/server.py	(revision 10411)
+++ main/waeup.cas/trunk/waeup/cas/server.py	(revision 10412)
@@ -133,11 +133,12 @@
 
 
-def login_redirect_service(db, service, sso=True):
+def login_redirect_service(db, service, sso=True, create_ticket=True):
     """Return a response redirecting to a service via HTTP 303 See Other.
     """
     # safely redirect to service given
-    st = create_service_ticket(service, sso)
-    db.add(st)
-    service = '%s?ticket=%s' % (service, st.ticket)
+    if create_ticket:
+        st = create_service_ticket(service, sso)
+        db.add(st)
+        service = '%s?ticket=%s' % (service, st.ticket)
     html = get_template('login_service_redirect.html')
     html = html.replace('SERVICE_URL', service)
@@ -209,4 +210,7 @@
         service = req.POST.get('service', req.GET.get('service', None))
         renew = req.POST.get('renew', req.GET.get('renew', None))
+        gateway = req.POST.get('gateway', req.GET.get('gateway', None))
+        if renew is not None and gateway is not None:
+            gateway = None
         service_field = ''
         msg = ''
@@ -215,5 +219,8 @@
         valid_lt = check_login_ticket(self.db, req.POST.get('lt'))
         tgc = check_session_cookie(self.db, req.cookies.get('cas-tgc', None))
-        if tgc and renew is None:
+        if gateway and (not tgc) and service:
+            return login_redirect_service(
+                self.db, service, sso=True, create_ticket=False)
+        if tgc and (renew is None):
             if service:
                 return login_redirect_service(self.db, service, sso=True)
Index: main/waeup.cas/trunk/waeup/cas/tests/test_server.py
===================================================================
--- main/waeup.cas/trunk/waeup/cas/tests/test_server.py	(revision 10411)
+++ main/waeup.cas/trunk/waeup/cas/tests/test_server.py	(revision 10412)
@@ -140,4 +140,12 @@
         assert 'Set-Cookie' not in resp.headers
 
+    def test_login_renew_without_cookie(self):
+        # 2.1.1: with renew and no cookie, normal auth will happen
+        app = CASServer()
+        req = Request.blank('https://localhost/login?renew=true')
+        resp = app(req)
+        assert resp.status == '200 OK'
+        assert b'username' in resp.body
+
     def test_login_renew_as_empty_string(self):
         # `renew` is handled correctly, even with empty value
@@ -149,4 +157,44 @@
         req.headers['Cookie'] = 'cas-tgc=%s' % value
         resp = app(req)
+        assert resp.status == '200 OK'
+        assert b'username' in resp.body
+        assert 'Set-Cookie' not in resp.headers
+
+    def test_login_gateway_no_cookie_with_service(self):
+        # 2.1.1: with gateway but w/o cookie we will be redirected to service
+        # no service ticket will be issued
+        app = CASServer()
+        params = 'gateway=true&service=http%3A%2F%2Fwww.service.com'
+        req = Request.blank('https://localhost/login?%s' % params)
+        resp = app(req)
+        assert resp.status == '303 See Other'
+        assert 'Location' in resp.headers
+        assert resp.headers['Location'] == 'http://www.service.com'
+
+    def test_login_gateway_with_cookie_and_service(self):
+        # 2.1.1: with cookie and gateway we will be redirected to service
+        app = CASServer()
+        tgc = create_tgc_value()
+        app.db.add(tgc)
+        value = str(tgc.value)
+        params = 'gateway=true&service=http%3A%2F%2Fwww.service.com'
+        req = Request.blank('https://localhost/login?%s' % params)
+        req.headers['Cookie'] = 'cas-tgc=%s' % value
+        resp = app(req)
+        assert resp.status == '303 See Other'
+        assert 'Location' in resp.headers
+        assert resp.headers['Location'].startswith(
+            'http://www.service.com?ticket=ST-')
+
+    def test_login_gateway_and_renew(self):
+        # 2.1.1 if both, gateway and renew are specified, only renew is valid
+        app = CASServer()
+        tgc = create_tgc_value()
+        app.db.add(tgc)
+        value = str(tgc.value)
+        req = Request.blank('https://localhost/login?renew=true&gateway=true')
+        req.headers['Cookie'] = 'cas-tgc=%s' % value
+        resp = app(req)
+        # with only gateway true, this would lead to a redirect
         assert resp.status == '200 OK'
         assert b'username' in resp.body
