incubator-heraldry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ket...@apache.org
Subject svn commit: r493375 - in /incubator/heraldry/libraries/python/openid/trunk/openid: consumer/consumer.py test/test_consumer.py
Date Sat, 06 Jan 2007 05:26:05 GMT
Author: keturn
Date: Fri Jan  5 21:26:05 2007
New Revision: 493375

URL: http://svn.apache.org/viewvc?view=rev&rev=493375
Log:
[python-to-heraldry @ Handle OpenID 1 and 2 cancel responses to checkid_immediate requests]

Original author: Josh Hoyt <josh@janrain.com>
Date: 2006-12-27 00:02:12+00:00

Modified:
    incubator/heraldry/libraries/python/openid/trunk/openid/consumer/consumer.py
    incubator/heraldry/libraries/python/openid/trunk/openid/test/test_consumer.py

Modified: incubator/heraldry/libraries/python/openid/trunk/openid/consumer/consumer.py
URL: http://svn.apache.org/viewvc/incubator/heraldry/libraries/python/openid/trunk/openid/consumer/consumer.py?view=diff&rev=493375&r1=493374&r2=493375
==============================================================================
--- incubator/heraldry/libraries/python/openid/trunk/openid/consumer/consumer.py (original)
+++ incubator/heraldry/libraries/python/openid/trunk/openid/consumer/consumer.py Fri Jan 
5 21:26:05 2007
@@ -409,6 +409,12 @@
         mac_key64 = response.getArg(OPENID_NS, 'mac_key', no_default)
         return oidutil.fromBase64(mac_key64)
 
+class SetupNeededError(Exception):
+    """Internally-used exception that indicates that an immediate-mode
+    request cancelled."""
+    def __init__(self, user_setup_url=None):
+        Exception.__init__(self, user_setup_url)
+        self.user_setup_url = user_setup_url
 
 class ProtocolError(ValueError):
     """Exception that indicates that a message violated the
@@ -461,6 +467,11 @@
                                    reference=reference)
         elif mode == 'id_res':
             try:
+                self._checkSetupNeeded(message)
+            except SetupNeededError, why:
+                return SetupNeededResponse(endpoint, why.user_setup_url)
+
+            try:
                 response = self._doIdRes(message, endpoint)
             except fetchers.HTTPFetchingError, why:
                 message = 'HTTP request failed: %s' % (str(why),)
@@ -530,18 +541,36 @@
 
         return response_message
 
+    def _checkSetupNeeded(self, message):
+        """Check an id_res message to see if it is a
+        checkid_immediate cancel response.
+
+        @raises: SetupNeededError if it is a checkid_immediate cancellation
+        """
+        if message.isOpenID1():
+            # In OpenID 1, we check to see if this is a cancel from
+            # immediate mode by the presence of the user_setup_url
+            # parameter.
+            user_setup_url = message.getArg(OPENID1_NS, 'user_setup_url')
+            if user_setup_url is not None:
+                raise SetupNeededError(user_setup_url)
+        else:
+            # In OpenID 2, we check whether the only field present is
+            # the mode. This seems questionable, but it's the best way
+            # that I can express what it says in the spec.
+            openid_args = message.getArgs(OPENID2_NS)
+            if openid_args == {'mode':'id_res'}:
+                raise SetupNeededError()
+
     def _doIdRes(self, message, endpoint):
-        """Handle id_res responses.
+        """Handle id_res responses that are not cancellations of
+        immediate mode requests.
 
         @param message: the response paramaters.
         @param endpoint: the discovered endpoint object. May be None.
 
         @returntype: L{Response}
         """
-        user_setup_url = message.getArg(OPENID_NS, 'user_setup_url')
-        if user_setup_url is not None:
-            return SetupNeededResponse(endpoint, user_setup_url)
-
         try:
             signed_list = self._idResCheckSignature(message,
                                                     endpoint.server_url)

Modified: incubator/heraldry/libraries/python/openid/trunk/openid/test/test_consumer.py
URL: http://svn.apache.org/viewvc/incubator/heraldry/libraries/python/openid/trunk/openid/test/test_consumer.py?view=diff&rev=493375&r1=493374&r2=493375
==============================================================================
--- incubator/heraldry/libraries/python/openid/trunk/openid/test/test_consumer.py (original)
+++ incubator/heraldry/libraries/python/openid/trunk/openid/test/test_consumer.py Fri Jan
 5 21:26:05 2007
@@ -11,7 +11,8 @@
 from openid.consumer.consumer import \
      AuthRequest, GenericConsumer, SUCCESS, FAILURE, CANCEL, SETUP_NEEDED, \
      SuccessResponse, FailureResponse, SetupNeededResponse, CancelResponse, \
-     DiffieHellmanSHA1ConsumerSession, Consumer, PlainTextConsumerSession
+     DiffieHellmanSHA1ConsumerSession, Consumer, PlainTextConsumerSession, \
+     SetupNeededError
 from openid import association
 from openid.server.server import \
      PlainTextServerSession, DiffieHellmanSHA1ServerSession
@@ -577,15 +578,62 @@
         r.message.index(IdResFetchFailingConsumer.message)
 
 class TestSetupNeeded(TestIdRes):
-    def test_setupNeeded(self):
+    def failUnlessSetupNeeded(self, expected_setup_url, message):
+        try:
+            self.consumer._checkSetupNeeded(message)
+        except SetupNeededError, why:
+            self.failUnlessEqual(expected_setup_url, why.user_setup_url)
+        else:
+            self.fail("Expected to find an immediate-mode response")
+
+    def test_setupNeededOpenID1(self):
+        """The minimum conditions necessary to trigger Setup Needed"""
+        setup_url = 'http://unittest/setup-here'
+        message = Message.fromPostArgs({
+            'openid.mode': 'id_res',
+            'openid.user_setup_url': setup_url,
+            })
+        self.failUnless(message.isOpenID1())
+        self.failUnlessSetupNeeded(setup_url, message)
+
+    def test_setupNeededOpenID1_extra(self):
+        """Extra stuff along with setup_url still trigger Setup Needed"""
         setup_url = 'http://unittest/setup-here'
         message = Message.fromPostArgs({
             'openid.mode': 'id_res',
             'openid.user_setup_url': setup_url,
+            'openid.identity': 'bogus',
+            })
+        self.failUnless(message.isOpenID1())
+        self.failUnlessSetupNeeded(setup_url, message)
+
+    def test_noSetupNeededOpenID1(self):
+        """When the user_setup_url is missing on an OpenID 1 message,
+        we assume that it's not a cancel response to checkid_immediate"""
+        message = Message.fromOpenIDArgs({'mode': 'id_res'})
+        self.failUnless(message.isOpenID1())
+
+        # No SetupNeededError raised
+        self.consumer._checkSetupNeeded(message)
+
+    def test_setupNeededOpenID2(self):
+        message = Message.fromOpenIDArgs({
+            'mode':'id_res',
+            'ns':OPENID2_NS,
             })
-        ret = self.consumer._doIdRes(message, self.endpoint,)
-        self.failUnlessEqual(ret.status, SETUP_NEEDED)
-        self.failUnlessEqual(ret.setup_url, setup_url)
+        self.failUnless(message.isOpenID2())
+        self.failUnlessSetupNeeded(None, message)
+
+    def test_noSetupNeededOpenID2(self):
+        message = Message.fromOpenIDArgs({
+            'mode':'id_res',
+            'game':'puerto_rico',
+            'ns':OPENID2_NS,
+            })
+        self.failUnless(message.isOpenID2())
+
+        # No SetupNeededError raised
+        self.consumer._checkSetupNeeded(message)
 
 class CheckAuthHappened(Exception): pass
 



Mime
View raw message