diff options
-rw-r--r-- | Auth/OpenID/Discover.php | 48 | ||||
-rw-r--r-- | Auth/OpenID/TrustRoot.php | 173 | ||||
-rw-r--r-- | Tests/Auth/OpenID/RPVerify.php | 299 | ||||
-rw-r--r-- | Tests/Auth/OpenID/Server.php | 4 | ||||
-rw-r--r-- | Tests/TestDriver.php | 1 |
5 files changed, 515 insertions, 10 deletions
diff --git a/Auth/OpenID/Discover.php b/Auth/OpenID/Discover.php index 3284c25..8f4c80b 100644 --- a/Auth/OpenID/Discover.php +++ b/Auth/OpenID/Discover.php @@ -19,6 +19,8 @@ define('Auth_OpenID_TYPE_1_1', 'http://openid.net/signon/1.1'); define('Auth_OpenID_TYPE_1_0', 'http://openid.net/signon/1.0'); define('Auth_OpenID_TYPE_2_0_IDP', 'http://specs.openid.net/auth/2.0/server'); define('Auth_OpenID_TYPE_2_0', 'http://specs.openid.net/auth/2.0/signon'); +define('Auth_OpenID_RP_RETURN_TO_URL_TYPE', + 'http://specs.openid.net/auth/2.0/return_to'); function Auth_OpenID_getOpenIDTypeURIs() { @@ -26,7 +28,8 @@ function Auth_OpenID_getOpenIDTypeURIs() Auth_OpenID_TYPE_2_0, Auth_OpenID_TYPE_1_2, Auth_OpenID_TYPE_1_1, - Auth_OpenID_TYPE_1_0); + Auth_OpenID_TYPE_1_0, + Auth_OpenID_RP_RETURN_TO_URL_TYPE); } /** @@ -58,6 +61,29 @@ class Auth_OpenID_ServiceEndpoint { } } + /* + * Query this endpoint to see if it has any of the given type + * URIs. This is useful for implementing other endpoint classes + * that e.g. need to check for the presence of multiple versions + * of a single protocol. + * + * @param $type_uris The URIs that you wish to check + * + * @return all types that are in both in type_uris and + * $this->type_uris + */ + function matchTypes($type_uris) + { + $result = array(); + foreach ($type_uris as $test_uri) { + if ($this->supportsType($test_uri)) { + $result[] = $test_uri; + } + } + + return $result; + } + function supportsType($type_uri) { // Does this endpoint support this type? @@ -367,7 +393,9 @@ function Auth_OpenID_makeOpenIDEndpoints($uri, $yadis_services) return $s; } -function Auth_OpenID_discoverWithYadis($uri, &$fetcher) +function Auth_OpenID_discoverWithYadis($uri, &$fetcher, + $endpoint_filter='Auth_OpenID_getOPOrUserServices', + $discover_function=null) { // Discover OpenID services for a URI. Tries Yadis and falls back // on old-style <link rel='...'> discovery if Yadis fails. @@ -376,8 +404,15 @@ function Auth_OpenID_discoverWithYadis($uri, &$fetcher) // came back for that URI at all. I don't think falling back to // OpenID 1.0 discovery on the same URL will help, so don't bother // to catch it. + if ($discover_function === null) { + $discover_function = array('Auth_Yadis_Yadis', 'discover'); + } + $openid_services = array(); - $response = Auth_Yadis_Yadis::discover($uri, $fetcher); + + $response = call_user_func_array($discover_function, + array($uri, &$fetcher)); + $yadis_url = $response->normalized_uri; $yadis_services = array(); @@ -402,7 +437,9 @@ function Auth_OpenID_discoverWithYadis($uri, &$fetcher) $response->response_text); } - $openid_services = Auth_OpenID_getOPOrUserServices($openid_services); + $openid_services = call_user_func_array($endpoint_filter, + array(&$openid_services)); + return array($yadis_url, $openid_services); } @@ -421,8 +458,7 @@ function Auth_OpenID_discoverURI($uri, &$fetcher) } $uri = Auth_OpenID::normalizeUrl($uri); - return Auth_OpenID_discoverWithYadis($uri, - $fetcher); + return Auth_OpenID_discoverWithYadis($uri, $fetcher); } function Auth_OpenID_discoverWithoutYadis($uri, &$fetcher) diff --git a/Auth/OpenID/TrustRoot.php b/Auth/OpenID/TrustRoot.php index 50db1d2..f0be2d6 100644 --- a/Auth/OpenID/TrustRoot.php +++ b/Auth/OpenID/TrustRoot.php @@ -12,6 +12,8 @@ * @license http://www.gnu.org/copyleft/lesser.html LGPL */ +require_once 'Auth/OpenID/Discover.php'; + /** * A regular expression that matches a domain ending in a top-level domains. * Used in checking trust roots for sanity. @@ -36,6 +38,40 @@ define('Auth_OpenID___TLDs', * A wrapper for trust-root related functions */ class Auth_OpenID_TrustRoot { + /* + * Return a discovery URL for this realm. + * + * Return null if the realm could not be parsed or was not valid. + * + * @param return_to The relying party return URL of the OpenID + * authentication request + * + * @return The URL upon which relying party discovery should be + * run in order to verify the return_to URL + */ + function buildDiscoveryURL($realm) + { + $parsed = Auth_OpenID_TrustRoot::_parse($realm); + + if ($parsed === false) { + return false; + } + + if ($parsed['wildcard']) { + // Use "www." in place of the star + if ($parsed['host'][0] != '.') { + return false; + } + + $www_domain = 'www' . $parsed['host']; + + return sprintf('%s://%s%s', $parsed['scheme'], + $www_domain, $parsed['path']); + } else { + return $parsed['unparsed']; + } + } + /** * Parse a URL into its trust_root parts. * @@ -103,16 +139,17 @@ class Auth_OpenID_TrustRoot { if (isset($parts['path'])) { $path = strtolower($parts['path']); - if (substr($path, -1) != '/') { - $path .= '/'; - } } else { $path = '/'; } + $parts['path'] = $path; if (!isset($parts['port'])) { $parts['port'] = false; } + + $parts['unparsed'] = $trust_root; + return $parts; } @@ -240,4 +277,134 @@ class Auth_OpenID_TrustRoot { $url_parsed['port'] === $trust_root_parsed['port']); } } + +/* + * If the endpoint is a relying party OpenID return_to endpoint, + * return the endpoint URL. Otherwise, return None. + * + * This function is intended to be used as a filter for the Yadis + * filtering interface. + * + * @see: C{L{openid.yadis.services}} + * @see: C{L{openid.yadis.filters}} + * + * @param endpoint: An XRDS BasicServiceEndpoint, as returned by + * performing Yadis dicovery. + * + * @returns: The endpoint URL or None if the endpoint is not a + * relying party endpoint. + */ +function filter_extractReturnURL(&$endpoint) +{ + if ($endpoint->matchTypes(array(Auth_OpenID_RP_RETURN_TO_URL_TYPE))) { + return $endpoint; + } else { + return null; + } +} + +function &Auth_OpenID_extractReturnURL(&$endpoint_list) +{ + $result = array(); + + foreach ($endpoint_list as $endpoint) { + if (filter_extractReturnURL($endpoint)) { + $result[] = $endpoint; + } + } + + return $result; +} + +/* + * Is the return_to URL under one of the supplied allowed return_to + * URLs? + */ +function Auth_OpenID_returnToMatches($allowed_return_to_urls, $return_to) +{ + foreach ($allowed_return_to_urls as $allowed_return_to) { + // A return_to pattern works the same as a realm, except that + // it's not allowed to use a wildcard. We'll model this by + // parsing it as a realm, and not trying to match it if it has + // a wildcard. + + $return_realm = Auth_OpenID_TrustRoot::_parse($allowed_return_to); + if (// Parses as a trust root + ($return_realm !== false) && + // Does not have a wildcard + (!$return_realm['wildcard']) && + // Matches the return_to that we passed in with it + (Auth_OpenID_TrustRoot::match($allowed_return_to, $return_to))) { + return true; + } + } + + // No URL in the list matched + return false; +} + +/* + * Given a relying party discovery URL return a list of return_to + * URLs. + */ +function Auth_OpenID_getAllowedReturnURLs($relying_party_url, &$fetcher, + $discover_function=null) +{ + if ($discover_function === null) { + $discover_function = array('Auth_Yadis_Yadis', 'discover'); + } + + list($rp_url_after_redirects, $endpoints) = + Auth_OpenID_discoverWithYadis($relying_party_url, + &$fetcher, + 'Auth_OpenID_extractReturnURL', + $discover_function); + + if ($rp_url_after_redirects != $relying_party_url) { + // Verification caused a redirect + return false; + } + + $return_to_urls = array(); + foreach ($endpoints as $e) { + $return_to_urls[] = $e->server_url; + } + + return $return_to_urls; +} + +/* + * Verify that a return_to URL is valid for the given realm. + * + * This function builds a discovery URL, performs Yadis discovery on + * it, makes sure that the URL does not redirect, parses out the + * return_to URLs, and finally checks to see if the current return_to + * URL matches the return_to. + * + * @return true if the return_to URL is valid for the realm + */ +function Auth_OpenID_verifyReturnTo($realm_str, $return_to, &$fetcher, + $_vrfy='Auth_OpenID_getAllowedReturnURLs') +{ + $disco_url = Auth_OpenID_TrustRoot::buildDiscoveryURL($realm_str); + + if ($disco_url === false) { + return false; + } + + $allowable_urls = call_user_func_array($_vrfy, + array($disco_url, &$fetcher)); + + // The realm_str could not be parsed. + if ($allowable_urls === false) { + return false; + } + + if (Auth_OpenID_returnToMatches($allowable_urls, $return_to)) { + return true; + } else { + return false; + } +} + ?>
\ No newline at end of file diff --git a/Tests/Auth/OpenID/RPVerify.php b/Tests/Auth/OpenID/RPVerify.php new file mode 100644 index 0000000..59c8b1a --- /dev/null +++ b/Tests/Auth/OpenID/RPVerify.php @@ -0,0 +1,299 @@ +<?php + +/* + * Unit tests for verification of return_to URLs for a realm. + */ + +require_once 'Auth/OpenID/Discover.php'; +require_once 'Auth/OpenID/TrustRoot.php'; + +require_once 'Auth/Yadis/Yadis.php'; + +require_once 'PHPUnit.php'; + +// Because "null" cannot be passed by reference. +$NULL_FETCHER = null; + +/* + * Tests for building the discovery URL from a realm and a return_to + * URL + */ +class Tests_Auth_OpenID_BuildDiscoveryURL extends PHPUnit_TestCase { + /* + * Build a discovery URL out of the realm and a return_to and make + * sure that it matches the expected discovery URL + */ + function failUnlessDiscoURL($realm, $expected_discovery_url) + { + $actual_discovery_url = Auth_OpenID_TrustRoot::buildDiscoveryURL($realm); + $this->assertEquals($expected_discovery_url, $actual_discovery_url); + } + + /* + * There is no wildcard and the realm is the same as the return_to + * URL + */ + function test_trivial() + { + $this->failUnlessDiscoURL('http://example.com/foo', + 'http://example.com/foo'); + } + + /* + * There is a wildcard + */ + function test_wildcard() + { + $this->failUnlessDiscoURL('http://*.example.com/foo', + 'http://www.example.com/foo'); + } +} + +class _MockDiscover { + function _MockDiscover(&$data) { + $this->data =& $data; + } + + function mockDiscover($uri, $fetcher, $discover_function=null) + { + $result = new Auth_Yadis_DiscoveryResult($uri); + $result->response_text = $this->data; + $result->normalized_uri = $uri; + return $result; + } +} + +class Tests_Auth_OpenID_ExtractReturnToURLs extends PHPUnit_TestCase { + var $disco_url = 'http://example.com/'; + + function failUnlessXRDSHasReturnURLs($data, $expected_return_urls) + { + $discover_object = new _MockDiscover($data); + $actual_return_urls = Auth_OpenID_getAllowedReturnURLs($this->disco_url, $NULL_FETCHER, array(&$discover_object, 'mockDiscover')); + + $this->assertEquals($expected_return_urls, $actual_return_urls); + } + + function failUnlessDiscoveryFailure($text) + { + $discover_object = new _MockDiscover($text); + $this->assertFalse(Auth_OpenID_getAllowedReturnURLs($this->disco_url, $NULL_FETCHER, array(&$discover_object, 'mockDiscover'))); + } + + function test_empty() + { + $this->failUnlessDiscoveryFailure(''); + } + + function test_badXML() + { + $this->failUnlessDiscoveryFailure('>'); + } + + function test_noEntries() + { + $this->failUnlessXRDSHasReturnURLs('<?xml version="1.0" encoding="UTF-8"?> +<xrds:XRDS xmlns:xrds="xri://$xrds" + xmlns="xri://$xrd*($v*2.0)" + > + <XRD> + </XRD> +</xrds:XRDS> +', array()); + } + + function test_noReturnToEntries() + { + $this->failUnlessXRDSHasReturnURLs('<?xml version="1.0" encoding="UTF-8"?> +<xrds:XRDS xmlns:xrds="xri://$xrds" + xmlns="xri://$xrd*($v*2.0)" + > + <XRD> + <Service priority="10"> + <Type>http://specs.openid.net/auth/2.0/server</Type> + <URI>http://www.myopenid.com/server</URI> + </Service> + </XRD> +</xrds:XRDS> +', array()); + } + + function test_oneEntry() + { + $this->failUnlessXRDSHasReturnURLs('<?xml version="1.0" encoding="UTF-8"?> +<xrds:XRDS xmlns:xrds="xri://$xrds" + xmlns="xri://$xrd*($v*2.0)" + > + <XRD> + <Service> + <Type>http://specs.openid.net/auth/2.0/return_to</Type> + <URI>http://rp.example.com/return</URI> + </Service> + </XRD> +</xrds:XRDS> +', array('http://rp.example.com/return')); + } + + function test_twoEntries() + { + $this->failUnlessXRDSHasReturnURLs('<?xml version="1.0" encoding="UTF-8"?> +<xrds:XRDS xmlns:xrds="xri://$xrds" + xmlns="xri://$xrd*($v*2.0)" + > + <XRD> + <Service priority="0"> + <Type>http://specs.openid.net/auth/2.0/return_to</Type> + <URI>http://rp.example.com/return</URI> + </Service> + <Service priority="1"> + <Type>http://specs.openid.net/auth/2.0/return_to</Type> + <URI>http://other.rp.example.com/return</URI> + </Service> + </XRD> +</xrds:XRDS> +', array('http://rp.example.com/return', + 'http://other.rp.example.com/return')); + } + + function test_twoEntries_withOther() + { + $this->failUnlessXRDSHasReturnURLs('<?xml version="1.0" encoding="UTF-8"?> +<xrds:XRDS xmlns:xrds="xri://$xrds" + xmlns="xri://$xrd*($v*2.0)" + > + <XRD> + <Service priority="0"> + <Type>http://specs.openid.net/auth/2.0/return_to</Type> + <URI>http://rp.example.com/return</URI> + </Service> + <Service priority="1"> + <Type>http://specs.openid.net/auth/2.0/return_to</Type> + <URI>http://other.rp.example.com/return</URI> + </Service> + <Service priority="0"> + <Type>http://example.com/LOLCATS</Type> + <URI>http://example.com/invisible+uri</URI> + </Service> + </XRD> +</xrds:XRDS> +', array('http://rp.example.com/return', + 'http://other.rp.example.com/return')); + } +} + +class Tests_Auth_OpenID_ReturnToMatches extends PHPUnit_TestCase { + function test_noEntries() + { + $this->assertFalse(Auth_OpenID_returnToMatches(array(), 'anything')); + } + + function test_exactMatch() + { + $r = 'http://example.com/return.to'; + $this->assertTrue(Auth_OpenID_returnToMatches(array($r), $r)); + } + + function test_garbageMatch() + { + $r = 'http://example.com/return.to'; + $this->assertTrue(Auth_OpenID_returnToMatches( + array('This is not a URL at all. In fact, it has characters, ' . + 'like "<" that are not allowed in URLs', $r), $r)); + } + + function test_descendant() + { + $r = 'http://example.com/return.to'; + $this->assertTrue(Auth_OpenID_returnToMatches(array($r), + 'http://example.com/return.to/user:joe')); + } + + function test_wildcard() + { + $this->assertFalse(Auth_OpenID_returnToMatches( + array('http://*.example.com/return.to'), + 'http://example.com/return.to')); + } + + function test_noMatch() + { + $r = 'http://example.com/return.to'; + $this->assertFalse(Auth_OpenID_returnToMatches(array($r), + 'http://example.com/xss_exploit')); + } +} + +class Verifier { + function Verifier(&$test_case, $return_to) + { + $this->tc =& $test_case; + $this->return_to = $return_to; + } + + function verify($disco_url) + { + $this->tc->assertEquals('http://www.example.com/', $disco_url); + + if ($this->return_to === false) { + return false; + } else { + return array($this->return_to); + } + } +} + +class Tests_Auth_OpenID_VerifyReturnTo extends PHPUnit_TestCase { + + function test_bogusRealm() + { + $this->assertFalse(Auth_OpenID_verifyReturnTo('', 'http://example.com/', $NULL_FETCHER)); + } + + function test_verifyWithDiscoveryCalled() + { + $realm = 'http://*.example.com/'; + $return_to = 'http://www.example.com/foo'; + + $v = new Verifier($this, $return_to); + + $this->assertTrue(Auth_OpenID_verifyReturnTo($realm, $return_to, $NULL_FETCHER, array(&$v, 'verify'))); + } + + function test_verifyFailWithDiscoveryCalled() + { + $realm = 'http://*.example.com/'; + $return_to = 'http://www.example.com/foo'; + + $v = new Verifier($this, 'http://something-else.invalid/'); + + $this->assertFalse(Auth_OpenID_verifyReturnTo($realm, $return_to, $NULL_FETCHER, array(&$v, 'verify'))); + } + + function test_verifyFailIfDiscoveryRedirects() + { + $realm = 'http://*.example.com/'; + $return_to = 'http://www.example.com/foo'; + + $v = new Verifier($this, false); + + $this->assertFalse(Auth_OpenID_verifyReturnTo($realm, $return_to, $NULL_FETCHER, array(&$v, 'verify'))); + } +} + +class Tests_Auth_OpenID_RPVerify extends PHPUnit_TestSuite { + function getName() + { + return "Tests_Auth_OpenID_RPVerify"; + } + + function Tests_Auth_OpenID_RPVerify() + { + $this->addTestSuite('Tests_Auth_OpenID_VerifyReturnTo'); + $this->addTestSuite('Tests_Auth_OpenID_ReturnToMatches'); + $this->addTestSuite('Tests_Auth_OpenID_ExtractReturnToURLs'); + $this->addTestSuite('Tests_Auth_OpenID_BuildDiscoveryURL'); + } +} + + +?>
\ No newline at end of file diff --git a/Tests/Auth/OpenID/Server.php b/Tests/Auth/OpenID/Server.php index 49e21a5..75abad6 100644 --- a/Tests/Auth/OpenID/Server.php +++ b/Tests/Auth/OpenID/Server.php @@ -133,8 +133,10 @@ class Tests_Auth_OpenID_Test_ServerError extends PHPUnit_TestCase { $this->assertTrue($e->whichEncoding() == Auth_OpenID_ENCODE_HTML_FORM); + $msg = $e->toMessage(); + $this->assertTrue($e->toFormMarkup() == - $e->toMessage()->toFormMarkup($args->getArg(Auth_OpenID_OPENID_NS, 'return_to'))); + $msg->toFormMarkup($args->getArg(Auth_OpenID_OPENID_NS, 'return_to'))); } function test_browserWithReturnTo_OpenID1_exceeds_limit() diff --git a/Tests/TestDriver.php b/Tests/TestDriver.php index 69b69ce..0b5d6b2 100644 --- a/Tests/TestDriver.php +++ b/Tests/TestDriver.php @@ -129,6 +129,7 @@ $_tests = array( 'Nonce', 'OpenID_Yadis', 'Parse', + 'RPVerify', 'Server', 'SReg', 'StoreTest', |