function query($xri, $service_types, $filters = array()) { $services = array(); $canonicalID = null; foreach ($service_types as $service_type) { $url = $this->queryURL($xri, $service_type); $response = $this->fetcher->get($url); if ($response->status != 200 and $response->status != 206) { continue; } $xrds = Auth_Yadis_XRDS::parseXRDS($response->body); if (!$xrds) { continue; } $canonicalID = Auth_Yadis_getCanonicalID($xri, $xrds); if ($canonicalID === false) { return null; } $some_services = $xrds->services($filters); $services = array_merge($services, $some_services); // TODO: // * If we do get hits for multiple service_types, we're // almost certainly going to have duplicated service // entries and broken priority ordering. } return array($canonicalID, $services); }
function runTest() { // Parse into endpoint objects that we will check $xrds_object = Auth_Yadis_XRDS::parseXRDS($this->xrds); $endpoints = array(); if ($xrds_object) { $endpoints = $xrds_object->services(array('filter_MatchesAnyOpenIDType')); $endpoints = Auth_OpenID_makeOpenIDEndpoints($this->yadis_url, $endpoints); } // make sure there are the same number of endpoints as // URIs. This assumes that the type_uris contains at least one // OpenID type. $this->assertEquals(count($this->uris), count($endpoints), "URI <-> Endpoint count"); // So that we can check equality on the endpoint types $type_uris = $this->type_uris; sort($type_uris); $seen_uris = array(); foreach ($endpoints as $endpoint) { $seen_uris[] = $endpoint->server_url; // All endpoints will have same yadis_url $this->assertEquals($this->yadis_url, $endpoint->claimed_id); // and delegate $this->assertEquals($this->local_id, $endpoint->local_id); // and types $actual_types = $endpoint->type_uris; sort($actual_types); $this->assertEquals($actual_types, $type_uris); } // So that they will compare equal, because we don't care what // order they are in sort($seen_uris); $uris = $this->uris; sort($uris); // Make sure we saw all URIs, and saw each one once $this->assertEquals($uris, $seen_uris); }
function fetch_xrds_services($authority, $url, $fetcher, $use_cache = true) { if ($url == null) { throw new GApps_Discovery_Exception("Invalid null URL"); } if ($use_cache) { $body = $this->get_cache($url); } if (!isset($body)) { $http_resp = @$fetcher->get($url); if ($http_resp->status != 200 and $http_resp->status != 206) { throw new GApps_Discovery_Exception("Received {$http_resp->status} when fetching {$url}"); } $body = $http_resp->body; $signature = $http_resp->headers["Signature"]; $signed_by = $this->verifier->verify($body, $signature); if ($signed_by != null && $signed_by != strtolower($authority) && $signed_by != GApps_OpenID_Discovery::HOSTED_ID) { throw new GApps_Discovery_Exception("Signature from {$signed_by} not valid for {$authority}"); } // Signature valid and signed by trusted root by this point. if ($use_cache) { $this->put_cache($url, $body); } } $xrds = Auth_Yadis_XRDS::parseXRDS($body); return $xrds; }
function test_services_filters() { // First, just be sure that service objects do the right // thing. $xml = Tests_Auth_Yadis_readdata("brian_priority.xrds"); $xrds = Auth_Yadis_XRDS::parseXRDS($xml, array('openid' => 'http://openid.net/xmlns/1.0')); $this->assertTrue($xrds !== null); // Get list of service objects. $services = $xrds->services(); $this->assertEquals(count($services), 2, "first service count"); // Query the two service objecs. $s1 = $services[0]; $this->assertEquals($s1->getPriority(), 1, "first priority check"); $types = $s1->getTypes(); $this->assertEquals(count($types), 1, "first type check"); $s2 = $services[1]; $this->assertEquals($s2->getPriority(), 2, "second priority check"); $types = $s2->getTypes(); $this->assertEquals(count($types), 1, "second type check"); function _DelegateFilter(&$service) { if ($service->getElements('openid:Delegate')) { return true; } return false; } // Make sure that a filter which matches both DOES match both. $this->assertEquals(count($xrds->services(array("_DelegateFilter"))), 2, "_DelegateFilter check"); // This filter should match all services in the document. function _HasTypeAndURI(&$service) { if ($service->getTypes() && $service->getURIs()) { return true; } return false; } // This filter should only match one. function _URIMatchesSchtuff(&$service) { $uris = $service->getURIs(); foreach ($uris as $uri) { if (preg_match("|schtuff|", $uri)) { return true; } } return false; } // This filter should only match one. function _URIMatchesMyOpenID(&$service) { $uris = $service->getURIs(); foreach ($uris as $uri) { if (preg_match("|myopenid|", $uri)) { return true; } } return false; } // Make sure a pair of filters in ALL mode only match one service. $this->assertEquals(count($xrds->services(array("_HasTypeAndURI", "_URIMatchesSchtuff"), SERVICES_YADIS_MATCH_ALL)), 1, "_HasTypeAndURI / _URIMatchesSchtuff check"); // Make sure a pair of filters in ALL mode only match one service. $this->assertEquals(count($xrds->services(array("_HasTypeAndURI", "_URIMatchesMyOpenID"), SERVICES_YADIS_MATCH_ALL)), 1, "_HasTypeAndURI / _URIMatchesMyOpenID check"); // Make sure a pair of filters in ANY mode matches both services. $this->assertEquals(count($xrds->services(array("_URIMatchesMyOpenID", "_URIMatchesSchtuff"))), 2, "_URIMatchesMyOpenID / _URIMatchesSchtuff check"); // Make sure the order of the services returned (when using // filters) is correct. $s = $xrds->services(array("_URIMatchesMyOpenID", "_URIMatchesSchtuff")); $this->assertTrue($s[0]->getPriority() === 1, "s[0] priority check"); $this->assertTrue($s[1]->getPriority() === 2, "s[1] priority check"); // Make sure a bad filter mode gets us a null service list. $this->assertTrue($xrds->services(array("_URIMatchesMyOpenID", "_URIMatchesSchtuff"), "bogus") === null, "bogus filter check"); }
static function fromXRDS($uri, $xrds_text) { $xrds = Auth_Yadis_XRDS::parseXRDS($xrds_text); if ($xrds) { $yadis_services = $xrds->services(array('filter_MatchesAnyOpenIDType')); return Auth_OpenID_makeOpenIDEndpoints($uri, $yadis_services); } return null; }
function remoteSubscription() { $user = $this->getUser(); if (!$user) { $this->showForm(_('No such user.')); return; } $this->profile_url = $this->trimmed('profile_url'); if (!$this->profile_url) { $this->showForm(_('No such user.')); return; } if (!Validate::uri($this->profile_url, array('allowed_schemes' => array('http', 'https')))) { $this->showForm(_('Invalid profile URL (bad format)')); return; } $fetcher = Auth_Yadis_Yadis::getHTTPFetcher(); $yadis = Auth_Yadis_Yadis::discover($this->profile_url, $fetcher); if (!$yadis || $yadis->failed) { $this->showForm(_('Not a valid profile URL (no YADIS document).')); return; } # XXX: a little liberal for sites that accidentally put whitespace before the xml declaration $xrds =& Auth_Yadis_XRDS::parseXRDS(trim($yadis->response_text)); if (!$xrds) { $this->showForm(_('Not a valid profile URL (no XRDS defined).')); return; } $omb = $this->getOmb($xrds); if (!$omb) { $this->showForm(_('Not a valid profile URL (incorrect services).')); return; } if (omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]) == common_local_url('requesttoken')) { $this->showForm(_('That\'s a local profile! Login to subscribe.')); return; } if (User::staticGet('uri', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]))) { $this->showForm(_('That\'s a local profile! Login to subscribe.')); return; } list($token, $secret) = $this->requestToken($omb); if (!$token || !$secret) { $this->showForm(_('Couldn\'t get a request token.')); return; } $this->requestAuthorization($user, $omb, $token, $secret); }
function test_multisegment_xri() { $xml = Tests_Auth_Yadis_readdata('subsegments.xrds'); $xmldoc = Auth_Yadis_XRDS::parseXRDS($xml); $result = Auth_Yadis_getCanonicalId('xri://=nishitani*masaki', $xmldoc); $this->assertEquals($result, "xri://=!E117.EF2F.454B.C707!0000.0000.3B9A.CA01"); }
function Auth_OpenID_discoverWithYadis($uri, &$fetcher) { // Discover OpenID services for a URI. Tries Yadis and falls back // on old-style <link rel='...'> discovery if Yadis fails. // Might raise a yadis.discover.DiscoveryFailure if no document // 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. $openid_services = array(); $response = Auth_Yadis_Yadis::discover($uri, $fetcher); $yadis_url = $response->normalized_uri; $yadis_services = array(); if ($response->isFailure()) { return array($uri, array()); } $xrds =& Auth_Yadis_XRDS::parseXRDS($response->response_text); if ($xrds) { $yadis_services = $xrds->services(array('filter_MatchesAnyOpenIDType')); } if (!$yadis_services) { if ($response->isXRDS()) { return Auth_OpenID_discoverWithoutYadis($uri, $fetcher); } // Try to parse the response as HTML to get OpenID 1.0/1.1 // <link rel="..."> $openid_services = Auth_OpenID_ServiceEndpoint::fromHTML($yadis_url, $response->response_text); } else { $openid_services = Auth_OpenID_makeOpenIDEndpoints($yadis_url, $yadis_services); } $openid_services = Auth_OpenID_getOPOrUserServices($openid_services); return array($yadis_url, $openid_services); }
/** * Perform XRDS discovery at the specified URI for any EAUT Services. */ function Auth_Yadis_Email_getServices($uri, $fetcher) { $uri = Auth_OpenID::normalizeUrl($uri); $response = Auth_Yadis_Yadis::discover($uri, $fetcher); if ($response->isXRDS()) { $xrds =& Auth_Yadis_XRDS::parseXRDS($response->response_text); if ($xrds) { return $xrds->services(array('filter_MatchesAnyEmailType')); } } }
function get_remote_xrds($at_url) { global $request; $wp_plugins = "wp-plugins" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "enabled"; $path = plugin_path() . $wp_plugins . DIRECTORY_SEPARATOR . 'wp-openid' . DIRECTORY_SEPARATOR; add_include_path($path); require_once "Auth/Yadis/Yadis.php"; $fetcher = Auth_Yadis_Yadis::getHTTPFetcher(); $yadis = Auth_Yadis_Yadis::discover($at_url, $fetcher); if (!$yadis || $yadis->failed) { trigger_error("Sorry but the Yadis doc was not found at the profile URL", E_USER_ERROR); } $xrds =& Auth_Yadis_XRDS::parseXRDS($yadis->response_text); if (!$xrds) { trigger_error("Sorry but the XRDS data was not found in the Yadis doc", E_USER_ERROR); } $yadis_services = $xrds->services(array('filter_MatchesAnyOMBType')); foreach ($yadis_services as $service) { $type_uris = $service->getTypes(); $uris = $service->getURIs(); if ($type_uris && $uris) { foreach ($uris as $uri) { $xrd = xrdends($uri, $xrds); $ends = $xrd->services(array('filter_MatchesAnyOMBType')); foreach ($ends as $serv) { $typ = $serv->getTypes(); global $omb_services; $end = ""; foreach ($typ as $t) { if (in_array($t, $omb_services)) { $end = $t; } if ($t == OAUTH_VERSION . '/endpoint/request') { $data = $serv->getElements('xrd:LocalID'); $localid = $serv->parser->content($data[0]); } } $req = $serv->getURIs(); $endpoints[$end] = $req[0]; } } } } return array($localid, $endpoints); }