最近,我开始编写自己的 PHP OpenID 使用者类,以便更好地理解 openID。作为指南,我一直在引用 [LightOpenID 类][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
好的,这是我理解的逻辑(基本上):
- 检查
$url
是否向您发送了一个有效的 XRDS 文件,然后您可以解析该文件以确定 OpenID 提供商的端点。- 据我了解,这称为 Yadis 身份验证方法。
- 如果未找到 XRDS 文件,检查响应正文中是否包含包含端点 url 的 HTML 标记。
什么。这。见鬼。
我的意思是认真的?本质上是屏幕抓取响应并希望您找到具有适当属性值的链接?
现在,请不要误会我的意思,这门课很有魅力而且很棒。我只是没能理解用于发现端点的两种不同方法:XRDS (yadis) 和 HTML。
我的问题
- 是发现过程中使用的仅有的两种方法吗?
- 一个仅用于 OpenID 1.1 版,另一个用于 2 版?
- 同时支持这两种方法是否重要?
- 我遇到过 HTML 方法的站点是 Yahoo。他们疯了吗?
再次感谢您的宝贵时间。如果我听起来有点吃惊,我深表歉意,但当我开始了解正在采取哪些措施来找到终点时,我真的对这种方法感到震惊。
最佳答案
Specification是你的 friend 。
但是回答你的问题:
- 是的。这是 OpenID 规范定义的仅有的两种方法(至少,对于 URL——XRI 有第三种方法)。
- 不,两者都可以与协议(protocol)的两个版本一起使用。仔细阅读函数,您会发现它支持两个版本的两种方法。
- 如果您希望您的图书馆与每个提供者和用户合作,您最好这样做。一些用户将 HTML 标记粘贴到他们的网站中,因此他们网站的 url 可以用作 openid。
- 一些供应商甚至同时使用这两种方法,以保持与未实现 YADIS 发现(不是 OpenID 1.1 的一部分,但可以与其一起使用)的消费者的兼容性。所以这确实有道理。
是的,HTML 发现是关于搜索 <link>
在响应体中。这就是它被称为 HTML 发现的原因。
关于php - OpenID 发现方法 - Yadis VS HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3261459/