incubator-heraldry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ket...@apache.org
Subject svn commit: r493335 - in /incubator/heraldry/libraries/python/openid/trunk/openid: consumer/discover.py test/data/test_discover/yadis_2entries_idp.xml test/data/test_discover/yadis_idp_last.xml test/test_discover.py
Date Sat, 06 Jan 2007 05:22:29 GMT
Author: keturn
Date: Fri Jan  5 21:22:28 2007
New Revision: 493335

URL: http://svn.apache.org/viewvc?view=rev&rev=493335
Log:
[python-to-heraldry @ Make discoverYadis and discoverXRI return only OP Identifier services
if they exist]

Original author: cygnus@janrain.com
Date: 2006-12-20 00:11:14+00:00

Added:
    incubator/heraldry/libraries/python/openid/trunk/openid/test/data/test_discover/yadis_2entries_idp.xml
    incubator/heraldry/libraries/python/openid/trunk/openid/test/data/test_discover/yadis_idp_last.xml
Modified:
    incubator/heraldry/libraries/python/openid/trunk/openid/consumer/discover.py
    incubator/heraldry/libraries/python/openid/trunk/openid/test/test_discover.py

Modified: incubator/heraldry/libraries/python/openid/trunk/openid/consumer/discover.py
URL: http://svn.apache.org/viewvc/incubator/heraldry/libraries/python/openid/trunk/openid/consumer/discover.py?view=diff&rev=493335&r1=493334&r2=493335
==============================================================================
--- incubator/heraldry/libraries/python/openid/trunk/openid/consumer/discover.py (original)
+++ incubator/heraldry/libraries/python/openid/trunk/openid/consumer/discover.py Fri Jan 
5 21:22:28 2007
@@ -28,6 +28,9 @@
     @ivar identity_url: the verified identifier.
     @ivar canonicalID: For XRI, the persistent identifier.
     """
+
+    # OpenID service type URIs, listed in order of preference.  The
+    # ordering of this list affects yadis and XRI service discovery.
     openid_type_uris = [
         OPENID_IDP_2_0_TYPE,
 
@@ -159,6 +162,38 @@
     except ValueError, why:
         raise DiscoveryFailure('Normalizing identifier: %s' % (why[0],), None)
 
+def arrangeByType(service_list, preferred_types):
+    """Rearrange service_list in a new list so services are ordered by
+    types listed in preferred_types.  Return the new list."""
+    buckets = dict([(typ, []) for typ in preferred_types])
+
+    for typ in preferred_types:
+        for s in service_list:
+            if typ in s.type_uris:
+                buckets[typ].append(s)
+
+    new_list = []
+    for typ in preferred_types:
+        new_list += buckets[typ]
+
+    return new_list
+
+def getOPOrUserServices(openid_services):
+    """Extract OP Identifier services.  If none found, return the
+    rest, sorted with most preferred first according to
+    OpenIDServiceEndpoint.openid_type_uris.
+
+    openid_services is a list of OpenIDServiceEndpoint objects.
+
+    Returns a list of OpenIDServiceEndpoint objects."""
+
+    op_services = arrangeByType(openid_services, [OPENID_IDP_2_0_TYPE])
+
+    openid_services = arrangeByType(openid_services,
+                                    OpenIDServiceEndpoint.openid_type_uris)
+
+    return op_services or openid_services
+
 def discoverYadis(uri):
     """Discover OpenID services for a URI. Tries Yadis and falls back
     on old-style <link rel='...'> discovery if Yadis fails.
@@ -201,7 +236,7 @@
         # <link rel="...">
         openid_services = OpenIDServiceEndpoint.fromHTML(yadis_url, body)
 
-    return (yadis_url, openid_services)
+    return (yadis_url, getOPOrUserServices(openid_services))
 
 def discoverXRI(iname):
     endpoints = []
@@ -220,7 +255,7 @@
         endpoint.canonicalID = canonicalID
 
     # FIXME: returned xri should probably be in some normal form
-    return iname, endpoints
+    return iname, getOPOrUserServices(endpoints)
 
 
 def discoverNoYadis(uri):

Added: incubator/heraldry/libraries/python/openid/trunk/openid/test/data/test_discover/yadis_2entries_idp.xml
URL: http://svn.apache.org/viewvc/incubator/heraldry/libraries/python/openid/trunk/openid/test/data/test_discover/yadis_2entries_idp.xml?view=auto&rev=493335
==============================================================================
--- incubator/heraldry/libraries/python/openid/trunk/openid/test/data/test_discover/yadis_2entries_idp.xml
(added)
+++ incubator/heraldry/libraries/python/openid/trunk/openid/test/data/test_discover/yadis_2entries_idp.xml
Fri Jan  5 21:22:28 2007
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xrds:XRDS xmlns:xrds="xri://$xrds"
+           xmlns="xri://$xrd*($v*2.0)"
+           xmlns:openid="http://openid.net/xmlns/1.0"
+           >
+  <XRD>
+    <CanonicalID>=!1000</CanonicalID>
+
+    <Service priority="10">
+      <Type>http://openid.net/signon/2.0</Type>
+      <URI>http://www.myopenid.com/server</URI>
+      <openid:LocalID>http://smoker.myopenid.com/</openid:LocalID>
+    </Service>
+
+    <Service priority="20">
+      <Type>http://openid.net/server/2.0</Type>
+      <URI>http://www.livejournal.com/openid/server.bml</URI>
+    </Service>
+
+  </XRD>
+</xrds:XRDS>

Added: incubator/heraldry/libraries/python/openid/trunk/openid/test/data/test_discover/yadis_idp_last.xml
URL: http://svn.apache.org/viewvc/incubator/heraldry/libraries/python/openid/trunk/openid/test/data/test_discover/yadis_idp_last.xml?view=auto&rev=493335
==============================================================================
--- incubator/heraldry/libraries/python/openid/trunk/openid/test/data/test_discover/yadis_idp_last.xml
(added)
+++ incubator/heraldry/libraries/python/openid/trunk/openid/test/data/test_discover/yadis_idp_last.xml
Fri Jan  5 21:22:28 2007
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xrds:XRDS xmlns:xrds="xri://$xrds"
+           xmlns="xri://$xrd*($v*2.0)"
+           xmlns:openid="http://openid.net/xmlns/1.0"
+           >
+  <XRD>
+    <Service priority="10">
+      <Type>http://openid.net/signon/2.0</Type>
+      <URI>http://www.myopenid.com/server2_0</URI>
+    </Service>
+
+    <Service priority="20">
+      <Type>http://openid.net/server/2.0</Type>
+      <URI>http://www.myopenid.com/server_id</URI>
+    </Service>
+  </XRD>
+</xrds:XRDS>

Modified: incubator/heraldry/libraries/python/openid/trunk/openid/test/test_discover.py
URL: http://svn.apache.org/viewvc/incubator/heraldry/libraries/python/openid/trunk/openid/test/test_discover.py?view=diff&rev=493335&r1=493334&r2=493335
==============================================================================
--- incubator/heraldry/libraries/python/openid/trunk/openid/test/test_discover.py (original)
+++ incubator/heraldry/libraries/python/openid/trunk/openid/test/test_discover.py Fri Jan
 5 21:22:28 2007
@@ -335,6 +335,24 @@
         self._usedYadis(services[0])
         self._hasTypes(services[0], '2.0 OP')
 
+    def test_yadisIDPFirst(self):
+        # Make sure an OP identifier takes precedence.  The XRDS used
+        # here has two services, but we only want to get one back.
+        self.fetcher.documents = {
+            self.id_url: ('application/xrds+xml',
+                          readDataFile('yadis_idp_last.xml')),
+        }
+
+        id_url, services = discover.discover(self.id_url)
+        self.failUnlessEqual(len(services), 1,
+                             "Not 1 service in %r" % (services,))
+
+        self.failUnlessEqual(services[0].server_url,
+                             "http://www.myopenid.com/server_id")
+        self.failUnlessEqual(services[0].claimed_id, None)
+        self._usedYadis(services[0])
+
+
     def test_openidNoDelegate(self):
         services = self._discover(
             content_type='text/html',
@@ -392,8 +410,8 @@
         self.failUnlessEqual(services[1].claimed_id, self.id_url)
         self.failUnlessEqual('http://smoker.myopenid.com/',
                              services[1].local_id)
+        self.failUnlessEqual([discover.OPENID_2_0_TYPE], services[1].type_uris)
         self._notUsedYadis(services[1])
-        self._hasTypes(services[1], '2.0')
 
 
         self.failUnlessEqual(services[0].server_url,
@@ -401,11 +419,20 @@
         self.failUnlessEqual(services[0].claimed_id, self.id_url)
         self.failUnlessEqual('http://smoker.myopenid.com/',
                              services[0].local_id)
-        self.failUnlessEqual([discover.OPENID_1_1_TYPE], services[0].type_uris)
+        self.failUnlessEqual([discover.OPENID_2_0_TYPE], services[0].type_uris)
         self._notUsedYadis(services[0])
         self._hasTypes(services[0], '1.1')
 
 
+        self.failUnlessEqual(services[1].server_url,
+                             "http://www.myopenid.com/server")
+        self.failUnlessEqual(services[1].claimed_id, self.id_url)
+        self.failUnlessEqual('http://smoker.myopenid.com/',
+                             services[1].local_id)
+        self.failUnlessEqual([discover.OPENID_1_1_TYPE], services[1].type_uris)
+        self._notUsedYadis(services[1])
+
+
 class MockFetcherForXRIProxy(object):
 
     def __init__(self, documents, proxy_url=xrires.DEFAULT_PROXY):
@@ -463,6 +490,19 @@
         endpoint.claimed_id = "=example"
         endpoint.canonicalID = XRI("=!1000")
         self.failUnlessEqual(endpoint.getLocalID(), XRI("=!1000"))
+
+
+class TestXRIDiscoveryIDP(BaseTestDiscovery):
+    fetcherClass = MockFetcherForXRIProxy
+
+    documents = {'=smoker': ('application/xrds+xml',
+                             readDataFile('yadis_2entries_idp.xml')) }
+
+    def test_xri(self):
+        user_xri, services = discover.discoverXRI('=smoker')
+        self.failUnless(services, "Expected services, got zero")
+        self.failUnlessEqual(services[0].server_url,
+                             "http://www.livejournal.com/openid/server.bml")
 
 
 class TestPreferredNamespace(datadriven.DataDrivenTestCase):



Mime
View raw message