incubator-heraldry-commits mailing list archives

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

URL: http://svn.apache.org/viewvc?view=rev&rev=493325
Log:
[python-to-heraldry @ Added OpenID 2.0 HTML discovery]

Original author: Josh Hoyt <josh@janrain.com>
Date: 2006-12-18 23:45:15+00:00

Added:
    incubator/heraldry/libraries/python/openid/trunk/openid/consumer/html_parse.py
      - copied, changed from r493324, incubator/heraldry/libraries/python/openid/trunk/openid/consumer/parse.py
Removed:
    incubator/heraldry/libraries/python/openid/trunk/openid/consumer/parse.py
Modified:
    incubator/heraldry/libraries/python/openid/trunk/openid/consumer/discover.py
    incubator/heraldry/libraries/python/openid/trunk/openid/test/linkparse.py
    incubator/heraldry/libraries/python/openid/trunk/openid/test/test_consumer.py
    incubator/heraldry/libraries/python/openid/trunk/openid/test/test_discover.py
    incubator/heraldry/libraries/python/openid/trunk/openid/test/test_htmldiscover.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=493325&r1=493324&r2=493325
==============================================================================
--- 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:21:05 2007
@@ -11,8 +11,7 @@
 from openid.yadis.discover import DiscoveryFailure
 from openid.yadis import xrires, filters
 
-from openid.consumer.parse import openIDDiscover as parseOpenIDLinkRel
-from openid.consumer.parse import ParseError
+from openid.consumer import html_parse
 
 OPENID_1_0_NS = 'http://openid.net/xmlns/1.0'
 OPENID_IDP_2_0_TYPE = 'http://openid.net/server/2.0'
@@ -110,19 +109,35 @@
         """Parse the given document as HTML looking for an OpenID <link
         rel=...>
 
-        @raises: openid.consumer.parse.ParseError
+        @rtype: [OpenIDServiceEndpoint]
         """
-        local_id, server_url = parseOpenIDLinkRel(html)
-        service = cls()
-        service.claimed_id = uri
-        service.local_id = local_id
-        service.server_url = server_url
-        service.type_uris = [OPENID_1_0_TYPE]
-        return service
+        discovery_types = [
+            (OPENID_1_1_TYPE, 'openid.server', 'openid.delegate'),
+            (OPENID_2_0_TYPE, 'openid2.provider', 'openid2.local_id'),
+            ]
+
+        link_attrs = html_parse.parseLinkAttrs(html)
+        services = []
+        for type_uri, op_endpoint_rel, local_id_rel in discovery_types:
+            op_endpoint_url = html_parse.findFirstHref(
+                link_attrs, op_endpoint_rel)
+            if op_endpoint_url is None:
+                continue
+
+            service = cls()
+            service.claimed_id = uri
+            service.delegate = html_parse.findFirstHref(
+                link_attrs, local_id_rel)
+            service.server_url = op_endpoint_url
+            service.type_uris = [type_uri]
+
+            services.append(service)
+
+        return services
 
     fromHTML = classmethod(fromHTML)
 
-def findDelegate(service_element):
+def findLocalID(service_element):
     """Extract a openid:Delegate value from a Yadis Service element
     represented as an ElementTree Element object. If no delegate is
     found, returns None."""
@@ -186,12 +201,7 @@
 
         # Try to parse the response as HTML to get OpenID 1.0/1.1
         # <link rel="...">
-        try:
-            service = OpenIDServiceEndpoint.fromHTML(yadis_url, body)
-        except ParseError:
-            pass # Parsing failed, so return an empty list
-        else:
-            openid_services = [service]
+        openid_services = OpenIDServiceEndpoint.fromHTML(claimed_id, body)
 
     return (yadis_url, openid_services)
 
@@ -221,17 +231,10 @@
         raise DiscoveryFailure(
             'HTTP Response status from identity URL host is not 200. '
             'Got status %r' % (http_resp.status,), http_resp)
-    claimed_id = http_resp.final_url
-
-    # Try to parse the response as HTML to get OpenID 1.0/1.1
-    # <link rel="...">
-    try:
-        service = OpenIDServiceEndpoint.fromHTML(claimed_id, http_resp.body)
-    except ParseError:
-        openid_services = []
-    else:
-        openid_services = [service]
 
+    claimed_id = http_resp.final_url
+    openid_services = OpenIDServiceEndpoint.fromHTML(
+        claimed_id, http_resp.body)
     return claimed_id, openid_services
 
 def discover(uri):

Copied: incubator/heraldry/libraries/python/openid/trunk/openid/consumer/html_parse.py (from
r493324, incubator/heraldry/libraries/python/openid/trunk/openid/consumer/parse.py)
URL: http://svn.apache.org/viewvc/incubator/heraldry/libraries/python/openid/trunk/openid/consumer/html_parse.py?view=diff&rev=493325&p1=incubator/heraldry/libraries/python/openid/trunk/openid/consumer/parse.py&r1=493324&p2=incubator/heraldry/libraries/python/openid/trunk/openid/consumer/html_parse.py&r2=493325
==============================================================================
--- incubator/heraldry/libraries/python/openid/trunk/openid/consumer/parse.py (original)
+++ incubator/heraldry/libraries/python/openid/trunk/openid/consumer/html_parse.py Fri Jan
 5 21:21:05 2007
@@ -247,23 +247,3 @@
         return None
     first = matches[0]
     return first.get('href')
-
-class ParseError(ValueError):
-    """Exception for errors in parsing the HTML text for OpenID
-    settings"""
-
-def openIDDiscover(html_text):
-    """Parse OpenID settings out of the gived HTML text
-
-    @raises: ParseError
-    # XXX: document interface
-    # XXX: TESTME
-    """
-    link_attrs = parseLinkAttrs(html_text)
-
-    server_url = findFirstHref(link_attrs, 'openid.server')
-    if server_url is None:
-        raise ParseError('No openid.server found')
-
-    delegate_url = findFirstHref(link_attrs, 'openid.delegate')
-    return delegate_url, server_url

Modified: incubator/heraldry/libraries/python/openid/trunk/openid/test/linkparse.py
URL: http://svn.apache.org/viewvc/incubator/heraldry/libraries/python/openid/trunk/openid/test/linkparse.py?view=diff&rev=493325&r1=493324&r2=493325
==============================================================================
--- incubator/heraldry/libraries/python/openid/trunk/openid/test/linkparse.py (original)
+++ incubator/heraldry/libraries/python/openid/trunk/openid/test/linkparse.py Fri Jan  5 21:21:05
2007
@@ -1,4 +1,4 @@
-from openid.consumer.parse import parseLinkAttrs
+from openid.consumer.html_parse import parseLinkAttrs
 import os.path
 import codecs
 import unittest

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=493325&r1=493324&r2=493325
==============================================================================
--- 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:21:05 2007
@@ -17,8 +17,6 @@
 from openid.yadis.discover import DiscoveryFailure
 
 
-from openid.consumer import parse
-
 from openid.fetchers import HTTPResponse, HTTPFetchingError
 from openid import fetchers
 

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=493325&r1=493324&r2=493325
==============================================================================
--- 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:21:05 2007
@@ -281,6 +281,26 @@
   </head><body><p>foo</p></body></html>
 """
 
+openid2_html = """
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+  <head>
+    <title>Identity Page for Smoker</title>
+<link rel="openid2.provider" href="http://www.myopenid.com/server" />
+<link rel="openid2.local_id" href="http://smoker.myopenid.com/" />
+  </head><body><p>foo</p></body></html>
+"""
+
+openid_1_and_2_html = """
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+  <head>
+    <title>Identity Page for Smoker</title>
+<link rel="openid2.provider openid.server" href="http://www.myopenid.com/server" />
+<link rel="openid2.local_id openid.delegate" href="http://smoker.myopenid.com/" />
+  </head><body><p>foo</p></body></html>
+"""
+
 openid_html_no_delegate = """
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html>
@@ -448,6 +468,63 @@
                         'Delegate should be None. Got %r' %
                         (services[0].delegate,))
 
+        self._notUsedYadis(services[0])
+
+    def test_openid2(self):
+        self.fetcher.documents = {
+            self.id_url: ('text/html', openid2_html),
+        }
+        id_url, services = discover.discover(self.id_url)
+        self.failUnlessEqual(self.id_url, id_url)
+        self.failUnlessEqual(services[0].server_url,
+                             "http://www.myopenid.com/server")
+        self.failUnlessEqual(services[0].claimed_id, self.id_url)
+        self.failUnlessEqual('http://smoker.myopenid.com/',
+                             services[0].delegate)
+        self.failUnlessEqual([discover.OPENID_2_0_TYPE], services[0].type_uris)
+        self._notUsedYadis(services[0])
+        self.failUnlessEqual(1, len(services))
+
+    def test_openid1(self):
+        self.fetcher.documents = {
+            self.id_url: ('text/html', openid_html),
+        }
+        id_url, services = discover.discover(self.id_url)
+        self.failUnlessEqual(self.id_url, id_url)
+        self.failUnlessEqual(services[0].server_url,
+                             "http://www.myopenid.com/server")
+        self.failUnlessEqual(services[0].claimed_id, self.id_url)
+        self.failUnlessEqual('http://smoker.myopenid.com/',
+                             services[0].delegate)
+        self.failUnlessEqual([discover.OPENID_1_1_TYPE], services[0].type_uris)
+        self._notUsedYadis(services[0])
+        self.failUnlessEqual(1, len(services))
+
+    def test_openid_1_and_2(self):
+        self.fetcher.documents = {
+            self.id_url: ('text/html', openid_1_and_2_html),
+        }
+        id_url, services = discover.discover(self.id_url)
+
+        self.failUnlessEqual(2, len(services))
+        self.failUnlessEqual(self.id_url, id_url)
+
+
+        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].delegate)
+        self.failUnlessEqual([discover.OPENID_2_0_TYPE], services[1].type_uris)
+        self._notUsedYadis(services[1])
+
+
+        self.failUnlessEqual(services[0].server_url,
+                             "http://www.myopenid.com/server")
+        self.failUnlessEqual(services[0].claimed_id, self.id_url)
+        self.failUnlessEqual('http://smoker.myopenid.com/',
+                             services[0].delegate)
+        self.failUnlessEqual([discover.OPENID_1_1_TYPE], services[0].type_uris)
         self._notUsedYadis(services[0])
 
 

Modified: incubator/heraldry/libraries/python/openid/trunk/openid/test/test_htmldiscover.py
URL: http://svn.apache.org/viewvc/incubator/heraldry/libraries/python/openid/trunk/openid/test/test_htmldiscover.py?view=diff&rev=493325&r1=493324&r2=493325
==============================================================================
--- incubator/heraldry/libraries/python/openid/trunk/openid/test/test_htmldiscover.py (original)
+++ incubator/heraldry/libraries/python/openid/trunk/openid/test/test_htmldiscover.py Fri
Jan  5 21:21:05 2007
@@ -1,6 +1,4 @@
 from openid.consumer.discover import OpenIDServiceEndpoint
-from openid.consumer.parse import openIDDiscover as parseOpenIDLinkRel
-from openid.consumer.parse import ParseError
 import datadriven
 
 class BadLinksTestCase(datadriven.DataDrivenTestCase):
@@ -14,15 +12,10 @@
         datadriven.DataDrivenTestCase.__init__(self, data)
         self.data = data
 
-    def callFunc(self):
-        return parseOpenIDLinkRel(self.data)
-
     def runOneTest(self):
-        self.failUnlessRaises(ParseError, self.callFunc)
-
-class BadLinksThroughEndpoint(BadLinksTestCase):
-    def callFunc(self):
-        return OpenIDServiceEndpoint.fromHTML('http://unused.url/', self.data)
+        actual = OpenIDServiceEndpoint.fromHTML('http://unused.url/', self.data)
+        expected = []
+        self.failUnlessEqual(expected, actual)
 
 def pyUnitTests():
     return datadriven.loadTests(__name__)



Mime
View raw message