OpenID发现方法 - Yadis VS HTML

Lev*_*ith 6 php openid lightopenid

最近,我开始编写自己的PHP OpenID消费者类,以便更好地理解openID.作为指南,我一直在引用[LightOpenID Class] [1].在大多数情况下,我理解代码以及OpenID的工作原理.在查看作者的discover功能时,我感到困惑:

function discover($url)
{
    if(!$url) throw new ErrorException('No identity supplied.');
    # We save the original url in case of Yadis discovery failure.
    # It can happen when we'll be lead to an XRDS document
    # which does not have any OpenID2 services.
    $originalUrl = $url;

    # A flag to disable yadis discovery in case of failure in headers.
    $yadis = true;

    # We'll jump a maximum of 5 times, to avoid endless redirections.
    for($i = 0; $i < 5; $i ++) {
        if($yadis) {
            $headers = explode("\n",$this->request($url, 'HEAD'));

            $next = false;
            foreach($headers as $header) {
                if(preg_match('#X-XRDS-Location\s*:\s*(.*)#', $header, $m)) {
                    $url = $this->build_url(parse_url($url), parse_url(trim($m[1])));
                    $next = true;
                }

                if(preg_match('#Content-Type\s*:\s*application/xrds\+xml#i', $header)) {
                    # Found an XRDS document, now let's find the server, and optionally delegate.
                    $content = $this->request($url, 'GET');

                    # OpenID 2
                    # We ignore it for MyOpenID, as it breaks sreg if using OpenID 2.0
                    $ns = preg_quote('http://specs.openid.net/auth/2.0/');
                    if (preg_match('#<Service.*?>(.*)<Type>\s*'.$ns.'(.*?)\s*</Type>(.*)</Service>#s', $content, $m)
                        && !preg_match('/myopenid\.com/i', $this->identity)) {
                        $content = $m[1] . $m[3];
                        if($m[2] == 'server') $this->identifier_select = true;

                        $content = preg_match('#<URI>(.*)</URI>#', $content, $server);
                        $content = preg_match('#<LocalID>(.*)</LocalID>#', $content, $delegate);
                        if(empty($server)) {
                            return false;
                        }
                        # Does the server advertise support for either AX or SREG?
                        $this->ax   = preg_match('#<Type>http://openid.net/srv/ax/1.0</Type>#', $content);
                        $this->sreg = preg_match('#<Type>http://openid.net/sreg/1.0</Type>#', $content);

                        $server = $server[1];
                        if(isset($delegate[1])) $this->identity = $delegate[1];
                        $this->version = 2;

                        $this->server = $server;
                        return $server;
                    }

                    # OpenID 1.1
                    $ns = preg_quote('http://openid.net/signon/1.1');
                    if(preg_match('#<Service.*?>(.*)<Type>\s*'.$ns.'\s*</Type>(.*)</Service>#s', $content, $m)) {
                        $content = $m[1] . $m[2];

                        $content = preg_match('#<URI>(.*)</URI>#', $content, $server);
                        $content = preg_match('#<.*?Delegate>(.*)</.*?Delegate>#', $content, $delegate);
                        if(empty($server)) {
                            return false;
                        }
                        # AX can be used only with OpenID 2.0, so checking only SREG
                        $this->sreg = preg_match('#<Type>http://openid.net/sreg/1.0</Type>#', $content);

                        $server = $server[1];
                        if(isset($delegate[1])) $this->identity = $delegate[1];
                        $this->version = 1;

                        $this->server = $server;
                        return $server;
                    }

                    $next = true;
                    $yadis = false;
                    $url = $originalUrl;
                    $content = null;
                    break;
                }
            }
            if($next) continue;

            # There are no relevant information in headers, so we search the body.
            $content = $this->request($url, 'GET');
            if($location = $this->htmlTag($content, 'meta', 'http-equiv', 'X-XRDS-Location', 'value')) {
                $url = $this->build_url(parse_url($url), parse_url($location));
                continue;
            }
        }

        if(!$content) $content = $this->request($url, 'GET');

        # At this point, the YADIS Discovery has failed, so we'll switch
        # to openid2 HTML discovery, then fallback to openid 1.1 discovery.
        $server   = $this->htmlTag($content, 'link', 'rel', 'openid2.provider', 'href');
        $delegate = $this->htmlTag($content, 'link', 'rel', 'openid2.local_id', 'href');
        $this->version = 2;

        # Another hack for myopenid.com...
        if(preg_match('/myopenid\.com/i', $server)) {
            $server = null;
        }

        if(!$server) {
            # The same with openid 1.1
            $server   = $this->htmlTag($content, 'link', 'rel', 'openid.server', 'href');
            $delegate = $this->htmlTag($content, 'link', 'rel', 'openid.delegate', 'href');
            $this->version = 1;
        }

        if($server) {
            # We found an OpenID2 OP Endpoint
            if($delegate) {
                # We have also found an OP-Local ID.
                $this->identity = $delegate;
            }
            $this->server = $server;
            return $server;
        }

        throw new ErrorException('No servers found!');
    }
    throw new ErrorException('Endless redirection!');
}


  [1]: http://gitorious.org/lightopenid
Run Code Online (Sandbox Code Playgroud)

好的,这是我理解的逻辑(基本上):

  1. 检查是否$url向您发送了一个有效的XRDS文件,然后您解析该文件以确定OpenID提供程序的端点.
    • 根据我的理解,这称为Yadis身份验证方法.
  2. 如果未找到XRDS文件,请检查包含端点网址的HTML <link>标记的响应正文.

什么.的.哎呀.

我的意思是认真?基本上屏幕刮取响应,希望您找到具有适当属性值的链接?

现在,不要误会我的意思,这个课程就像一个魅力,它真棒.我只是没有弄清楚用于发现端点的两个独立方法:XRDS(yadis)和HTML.

我的问题

  1. 这些是发现过程中使用的唯一两种方法吗?
  2. 是仅在OpenID的1.1版本和版本2的其他版本中使用的?
  3. 支持这两种方法是否至关重要?
  4. 我遇到的HTML方法的网站是雅虎.他们疯了吗?

再次感谢您的时间.如果我听起来有点大惊小怪,我道歉,但是一旦我开始明白采取什么措施来寻找终点,我就真的对这种方法感到震惊.

Mew*_*ewp 6

规格是你的朋友.

但回答你的问题:

  1. 是.这些是OpenID规范定义的唯一两种方法(至少对于URL - 还有第三种XRI方法).
  2. 不,两者都可以与两种版本的协议一起使用.仔细阅读该函数,您将看到它支持两种版本的两种方法.
  3. 如果您希望您的库与每个提供商和用户合作,您最好这样做.有些用户将HTML标记粘贴到他们的网站中,因此他们网站的网址可以用作openid.
  4. 有些提供商甚至同时使用这两种方法,以保持与未实现YADIS发现的消费者的兼容性(这不是OpenID 1.1的一部分,但可以与它一起使用).所以这确实有意义.

是的,HTML发现是关于在<link>响应主体中搜索a .这就是为什么它被称为HTML发现.