discover() static public method

This should be called statically and will build a Yadis instance if the discovery process succeeds. This implements Yadis discovery as specified in the Yadis specification.
static public discover ( string $uri, Auth_Yadis_HTTPFetcher $fetcher, array $extra_ns_map = null, integer $timeout = 20 ) : mixed
$uri string The URI on which to perform Yadis discovery.
$fetcher Auth_Yadis_HTTPFetcher An instance of a Auth_Yadis_HTTPFetcher subclass.
$extra_ns_map array An array which maps namespace names to namespace URIs to be used when parsing the Yadis XRDS document.
$timeout integer An optional fetcher timeout, in seconds.
return mixed $obj Either null or an instance of Auth_Yadis_Yadis, depending on whether the discovery succeeded.
 /**
  * Create an instance from URL
  *
  * Constructs an OMB_Yadis_XRDS object from a given URL. A full Yadis
  * discovery is performed on the URL and the XRDS is parsed.
  * Throws an OMB_InvalidYadisException when no Yadis is discovered or the
  * detected XRDS file is broken.
  *
  * @param string                 $url     The URL on which Yadis discovery
  *                                        should be performed on
  * @param Auth_Yadis_HTTPFetcher $fetcher A fetcher used to get HTTP
  *                                        resources
  *
  * @access public
  *
  * @return OMB_Yadis_XRDS The initialized object representing the given
  *                        resource
  */
 public static function fromYadisURL($url, $fetcher)
 {
     /* Perform a Yadis discovery. */
     $yadis = Auth_Yadis_Yadis::discover($url, $fetcher);
     if ($yadis->failed) {
         throw new OMB_InvalidYadisException($url);
     }
     /* Parse the XRDS file. */
     $xrds = OMB_Yadis_XRDS::parseXRDS($yadis->response_text);
     if ($xrds === null) {
         throw new OMB_InvalidYadisException($url);
     }
     $xrds->fetcher = $fetcher;
     return $xrds;
 }
Example #2
0
 function runTest()
 {
     $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
     $y = Auth_Yadis_Yadis::discover($this->input_url, $fetcher);
     $this->assertTrue($y !== null);
     // Compare parts of returned Yadis object to expected URLs.
     $this->assertEquals($this->redir_uri, $y->normalized_uri, "tried {$this->input_url}");
     if ($this->xrds_uri) {
         $this->assertEquals($this->xrds_uri, $y->xrds_uri);
         // Compare contents of actual HTTP GET with that of Yadis
         // response.
         $f = Auth_Yadis_Yadis::getHTTPFetcher();
         $http_response = $f->get($this->xrds_uri);
         $this->assertEquals($http_response->body, $y->response_text);
     } else {
         $this->assertTrue($y->xrds_uri === null);
     }
 }
Example #3
0
/**
 * Discover and cache OpenID services for a user's delegate OpenID.
 *
 * @param int $userid user ID
 * @url string URL to discover.  If not provided, user's current delegate will be used
 * @return bool true if successful
 */
function openid_server_update_delegation_info($userid, $url = null)
{
    if (empty($url)) {
        $url = get_usermeta($userid, 'openid_delegate');
    }
    if (empty($url)) {
        return false;
    }
    $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
    $discoveryResult = Auth_Yadis_Yadis::discover($url, $fetcher);
    $endpoints = Auth_OpenID_ServiceEndpoint::fromDiscoveryResult($discoveryResult);
    $services = array();
    if (!empty($endpoints)) {
        foreach ($endpoints as $endpoint) {
            $service = array('Type' => array(), 'URI' => $endpoint->server_url);
            foreach ($endpoint->type_uris as $type) {
                $service['Type'][] = array('content' => $type);
                if ($type == Auth_OpenID_TYPE_2_0_IDP) {
                    $service['LocalID'] = Auth_OpenID_IDENTIFIER_SELECT;
                } else {
                    if ($type == Auth_OpenID_TYPE_2_0) {
                        $service['LocalID'] = $endpoint->local_id;
                    } else {
                        if (in_array($type, array(Auth_OpenID_TYPE_1_0, Auth_OpenID_TYPE_1_1, Auth_OpenID_TYPE_1_2))) {
                            $service['openid:Delegate'] = $endpoint->local_id;
                        }
                    }
                }
            }
            $services[] = $service;
        }
    }
    if (empty($services)) {
        // resort to checking for HTML links
        $response = $fetcher->get($url);
        $html_content = $response->body;
        $p = new Auth_OpenID_Parse();
        $link_attrs = $p->parseLinkAttrs($html_content);
        // check HTML for OpenID2
        $server_url = $p->findFirstHref($link_attrs, 'openid2.provider');
        if ($server_url !== null) {
            $openid_url = $p->findFirstHref($link_attrs, 'openid2.local_id');
            if ($openid_url == null) {
                $openid_url = $url;
            }
            $services[] = array('Type' => array(array('content' => Auth_OpenID_Type_1_1)), 'URI' => $server_url, 'LocalID' => $openid_url);
        }
        // check HTML for OpenID1
        $server_url = $p->findFirstHref($link_attrs, 'openid.server');
        if ($server_url !== null) {
            $openid_url = $p->findFirstHref($link_attrs, 'openid.delegate');
            if ($openid_url == null) {
                $openid_url = $url;
            }
            $services[] = array('Type' => array(array('content' => Auth_OpenID_Type_2_0)), 'URI' => $server_url, 'openid:Delegate' => $openid_url);
        }
    }
    if (empty($services)) {
        return false;
    }
    update_usermeta($userid, 'openid_delegate', $url);
    update_usermeta($userid, 'openid_delegate_services', $services);
    return true;
}
 function test_blank_content_type()
 {
     $fetcher = new BlankContentTypeFetcher();
     $result = Auth_Yadis_Yadis::discover("http://bogus", $fetcher);
     $this->assertEquals("", $result->content_type);
 }
Example #5
0
 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);
 }
 /**
  * Check if user supplied URL is a valid OpenID and
  * get the URL provider from it.
  * 
  * @param string $identifier user supplied OpenID identifier
  * @return string
  */
 public function validate($identifier)
 {
     $oldIdentifier = get_option('openid_delegation_url');
     $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
     $response = Auth_Yadis_Yadis::discover($identifier, $fetcher);
     list($normalized_identifier, $endpoints) = Auth_OpenID_discover($identifier, $fetcher);
     if (!empty($identifier) && empty($endpoints)) {
         add_settings_error('openid_delegation_url', 'error', sprintf(__('No OpenID services discovered for %s.', 'openid-delegation'), $identifier));
         return $oldIdentifier;
     }
     update_option('openid_delegation_provider', $endpoints[0]->server_url);
     if (!empty($response->xrds_uri)) {
         update_option('openid_delegation_xrds_location', $response->xrds_uri);
     }
     return $normalized_identifier;
 }
Example #7
0
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);
}
 function runTest()
 {
     if ($this->expected === null) {
         $result = Auth_Yadis_Yadis::discover($this->input_url, $this->fetcher);
         $this->assertTrue($result->isFailure());
     } else {
         $result = Auth_Yadis_Yadis::discover($this->input_url, $this->fetcher);
         if ($result === null) {
             $this->fail("Discovery result was null");
             return;
         }
         $this->assertEquals($this->input_url, $result->request_uri);
         $msg = 'Identity URL mismatch: actual = %s, expected = %s';
         $msg = sprintf($msg, $result->normalized_uri, $this->expected->uri);
         $this->assertEquals($this->expected->uri, $result->normalized_uri, $msg);
         $msg = 'Content mismatch: actual = %s, expected = %s';
         $msg = sprintf($msg, $result->response_text, $this->expected->body);
         $this->assertEquals($this->expected->body, $result->response_text, $msg);
         $this->assertEquals($this->expected->xrds_uri, $result->xrds_uri);
         $this->assertEquals($this->expected->content_type, $result->content_type);
     }
 }
Example #9
0
/**
 * 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'));
        }
    }
}
Example #10
0
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);
}