2011-07-17 137 views
1

我想研究class.openid.php,因为它比
lightopenid
更简单和更小。为了我的目的,200行确实很重要。但class.openid.php不会与谷歌的OpenID https://www.google.com/accounts/o8/id工作,打印到我这样的错误:有没有人得到class.openid.php与谷歌openID?

ERROR CODE: OPENID_NOSERVERSFOUND 
ERROR DESCRIPTION: Cannot find OpenID Server TAG on Identity page. 

是有可能使class.openid.php(任何版本)与谷歌的OpenID以及如何工作做这种事?

class.openid.php可以采取here但它并没有为我工作开箱,所以我必须找到所有<?并且如果有人<?php取代艾德里安想看看代码我有:

HTML接口页面:

<?php 
require('class.openid.v3.php'); 

if ($_POST['openid_action'] == "login"){ // Get identity from user and redirect browser to OpenID Server 
    $openid = new SimpleOpenID; 
    $openid->SetIdentity($_POST['openid_url']); 
    $openid->SetTrustRoot('http://' . $_SERVER["HTTP_HOST"]); 
    $openid->SetRequiredFields(array('email','fullname')); 
    $openid->SetOptionalFields(array('dob','gender','postcode','country','language','timezone')); 
    if ($openid->GetOpenIDServer()){ 
     $openid->SetApprovedURL('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PATH_INFO"]);  // Send Response from OpenID server to this script 
     $openid->Redirect(); // This will redirect user to OpenID Server 
    }else{ 
     $error = $openid->GetError(); 
     echo "ERROR CODE: " . $error['code'] . "<br>"; 
     echo "ERROR DESCRIPTION: " . $error['description'] . "<br>"; 
    } 
    exit; 
} 
else if($_GET['openid_mode'] == 'id_res'){ // Perform HTTP Request to OpenID server to validate key 
    $openid = new SimpleOpenID; 
    $openid->SetIdentity($_GET['openid_identity']); 
    $openid_validation_result = $openid->ValidateWithServer(); 
    if ($openid_validation_result == true){   // OK HERE KEY IS VALID 
     echo "VALID"; 
    }else if($openid->IsError() == true){   // ON THE WAY, WE GOT SOME ERROR 
     $error = $openid->GetError(); 
     echo "ERROR CODE: " . $error['code'] . "<br>"; 
     echo "ERROR DESCRIPTION: " . $error['description'] . "<br>"; 
    }else{           // Signature Verification Failed 
     echo "INVALID AUTHORIZATION"; 
    } 
}else if ($_GET['openid_mode'] == 'cancel'){ // User Canceled your Request 
    echo "USER CANCELED REQUEST"; 
} 
?> 
<html> 
<head> 
    <title>OpenID Example</title> 
</head> 
<body> 
<div> 
<fieldset id="openid"> 
<legend>OpenID Login</legend> 
<form action="<?php echo 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PATH_INFO"]; ?>" method="post" onsubmit="this.login.disabled=true;"> 
<input type="hidden" name="openid_action" value="login"> 
<div><input type="text" name="openid_url" class="openid_login"><input type="submit" name="login" value="login &gt;&gt;"></div> 
<div><a href="http://www.myopenid.com/" class="link" >Get an OpenID</a></div> 
</form> 
</fieldset> 
</div> 
<div style="margin-top: 2em; font-family: arial; font-size: 0.8em; border-top:1px solid gray; padding: 4px;">Sponsored by: <a href="http://www.fivestores.com">FiveStores</a> - get your free online store; includes extensive API for developers; <i style="color: gray;">integrated with <a href="http://en.wikipedia.org/wiki/OpenID">OpenID</a></i></div> 
</body> 
</html> 

和PHP类

<?php 
/* 
    FREE TO USE Under License: GPLv3 
    Simple OpenID PHP Class 
    Some modifications by Eddie Roosenmaallen, [email protected] 
*/ 

class SimpleOpenID{ 
    var $openid_url_identity; 
    var $URLs = array(); 
    var $error = array(); 
    var $fields = array(
     'required' => array(), 
     'optional' => array(), 
    ); 

    function SimpleOpenID(){ 
     if (!function_exists('curl_exec')) { 
      die('Error: Class SimpleOpenID requires curl extension to work'); 
     } 
    } 

    function SetOpenIDServer($a){ 
     $this->URLs['openid_server'] = $a; 
    } 

    function SetTrustRoot($a){ 
     $this->URLs['trust_root'] = $a; 
    } 

    function SetCancelURL($a){ 
     $this->URLs['cancel'] = $a; 
    } 

    function SetApprovedURL($a){ 
     $this->URLs['approved'] = $a; 
    } 

    function SetRequiredFields($a){ 
     if (is_array($a)){ 
      $this->fields['required'] = $a; 
     }else{ 
      $this->fields['required'][] = $a; 
     } 
    } 

    function SetOptionalFields($a){ 
     if (is_array($a)){ 
      $this->fields['optional'] = $a; 
     }else{ 
      $this->fields['optional'][] = $a; 
     } 
    } 

    function SetIdentity($a){ // Set Identity URL 
      if ((stripos($a, 'http://') === false) 
       && (stripos($a, 'https://') === false)){ 
       $a = 'http://'.$a; 
      } 
      $this->openid_url_identity = $a; 
    } 

    function GetIdentity(){  // Get Identity 
     return $this->openid_url_identity; 
    } 

    function GetError(){ 
     $e = $this->error; 
     return array('code'=>$e[0],'description'=>$e[1]); 
    } 

    function ErrorStore($code, $desc = null){ 
     $errs['OPENID_NOSERVERSFOUND'] = 'Cannot find OpenID Server TAG on Identity page.'; 
     if ($desc == null){ 
      $desc = $errs[$code]; 
     } 
     $this->error = array($code,$desc); 
    } 

    function IsError(){ 
     if (count($this->error) > 0){ 
      return true; 
     }else{ 
      return false; 
     } 
    } 

    function splitResponse($response) { 
     $r = array(); 
     $response = explode("\n", $response); 
     foreach($response as $line) { 
      $line = trim($line); 
      if ($line != "") { 
       list($key, $value) = explode(":", $line, 2); 
       $r[trim($key)] = trim($value); 
      } 
     } 
     return $r; 
    } 

    function OpenID_Standarize($openid_identity = null){ 
     if ($openid_identity === null) 
      $openid_identity = $this->openid_url_identity; 

     $u = parse_url(strtolower(trim($openid_identity))); 

     if (!isset($u['path']) || ($u['path'] == '/')) { 
      $u['path'] = ''; 
     } 
     if(substr($u['path'],-1,1) == '/'){ 
      $u['path'] = substr($u['path'], 0, strlen($u['path'])-1); 
     } 
     if (isset($u['query'])){ // If there is a query string, then use identity as is 
      return $u['host'] . $u['path'] . '?' . $u['query']; 
     }else{ 
      return $u['host'] . $u['path']; 
     } 
    } 

    function array2url($arr){ // converts associated array to URL Query String 
     if (!is_array($arr)){ 
      return false; 
     } 
     $query = ''; 
     foreach($arr as $key => $value){ 
      $query .= $key . "=" . $value . "&"; 
     } 
     return $query; 
    } 

    function CURL_Request($url, $method="GET", $params = "") { // Remember, SSL MUST BE SUPPORTED 
      if (is_array($params)) $params = $this->array2url($params); 
      $curl = curl_init($url . ($method == "GET" && $params != "" ? "?" . $params : "")); 
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
      curl_setopt($curl, CURLOPT_HEADER, false); 
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
      curl_setopt($curl, CURLOPT_HTTPGET, ($method == "GET")); 
      curl_setopt($curl, CURLOPT_POST, ($method == "POST")); 
      if ($method == "POST") curl_setopt($curl, CURLOPT_POSTFIELDS, $params); 
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
      $response = curl_exec($curl); 

      if (curl_errno($curl) == 0){ 
       $response; 
      }else{ 
       $this->ErrorStore('OPENID_CURL', curl_error($curl)); 
      } 
      return $response; 
    } 

    function HTML2OpenIDServer($content) { 
     $get = array(); 

     // Get details of their OpenID server and (optional) delegate 
     preg_match_all('/<link[^>]*rel=[\'"]openid.server[\'"][^>]*href=[\'"]([^\'"]+)[\'"][^>]*\/?>/i', $content, $matches1); 
     preg_match_all('/<link[^>]*href=\'"([^\'"]+)[\'"][^>]*rel=[\'"]openid.server[\'"][^>]*\/?>/i', $content, $matches2); 
     $servers = array_merge($matches1[1], $matches2[1]); 

     preg_match_all('/<link[^>]*rel=[\'"]openid.delegate[\'"][^>]*href=[\'"]([^\'"]+)[\'"][^>]*\/?>/i', $content, $matches1); 

     preg_match_all('/<link[^>]*href=[\'"]([^\'"]+)[\'"][^>]*rel=[\'"]openid.delegate[\'"][^>]*\/?>/i', $content, $matches2); 

     $delegates = array_merge($matches1[1], $matches2[1]); 

     $ret = array($servers, $delegates); 
     return $ret; 
    } 

    function GetOpenIDServer(){ 
     $response = $this->CURL_Request($this->openid_url_identity); 
     list($servers, $delegates) = $this->HTML2OpenIDServer($response); 
     if (count($servers) == 0){ 
      $this->ErrorStore('OPENID_NOSERVERSFOUND'); 
      return false; 
     } 
     if (isset($delegates[0]) 
      && ($delegates[0] != "")){ 
      $this->SetIdentity($delegates[0]); 
     } 
     $this->SetOpenIDServer($servers[0]); 
     return $servers[0]; 
    } 

    function GetRedirectURL(){ 
     $params = array(); 
     $params['openid.return_to'] = urlencode($this->URLs['approved']); 
     $params['openid.mode'] = 'checkid_setup'; 
     $params['openid.identity'] = urlencode($this->openid_url_identity); 
     $params['openid.trust_root'] = urlencode($this->URLs['trust_root']); 

     if (isset($this->fields['required']) 
      && (count($this->fields['required']) > 0)) { 
      $params['openid.sreg.required'] = implode(',',$this->fields['required']); 
     } 
     if (isset($this->fields['optional']) 
      && (count($this->fields['optional']) > 0)) { 
      $params['openid.sreg.optional'] = implode(',',$this->fields['optional']); 
     } 
     return $this->URLs['openid_server'] . "?". $this->array2url($params); 
    } 

    function Redirect(){ 
     $redirect_to = $this->GetRedirectURL(); 
     if (headers_sent()){ // Use JavaScript to redirect if content has been previously sent (not recommended, but safe) 
      echo '<script language="JavaScript" type="text/javascript">window.location=\''; 
      echo $redirect_to; 
      echo '\';</script>'; 
     }else{ // Default Header Redirect 
      header('Location: ' . $redirect_to); 
     } 
    } 

    function ValidateWithServer(){ 
     $params = array(
      'openid.assoc_handle' => urlencode($_GET['openid_assoc_handle']), 
      'openid.signed' => urlencode($_GET['openid_signed']), 
      'openid.sig' => urlencode($_GET['openid_sig']) 
     ); 
     // Send only required parameters to confirm validity 
     $arr_signed = explode(",",str_replace('sreg.','sreg_',$_GET['openid_signed'])); 
     for ($i=0; $i<count($arr_signed); $i++){ 
      $s = str_replace('sreg_','sreg.', $arr_signed[$i]); 
      $c = $_GET['openid_' . $arr_signed[$i]]; 
      // if ($c != ""){ 
       $params['openid.' . $s] = urlencode($c); 
      // } 
     } 
     $params['openid.mode'] = "check_authentication"; 

     $openid_server = $this->GetOpenIDServer(); 
     if ($openid_server == false){ 
      return false; 
     } 
     $response = $this->CURL_Request($openid_server,'POST',$params); 
     $data = $this->splitResponse($response); 

     if ($data['is_valid'] == "true") { 
      return true; 
     }else{ 
      return false; 
     } 
    } 
} 
?> 
+0

该问题可以通过** Yes **来回答,因为您可以扩展该类并添加缺少的功能。但我认为这不是你所要求的,我认为你更喜欢新版本的下载链接,它包含你正在寻找的一切,对吧? – hakre

+1

我真的不认为这200行代码对你很重要。不过,如果你想支持OpenID 2。0和属性交换,你将不得不使用另一个库。并且为了证明行数并不重要,我已经在lightopenid中删除了一些代码(其中一半是注释,即所讨论的类明显缺乏),以使其小于您使用的类:http:// pastebin .COM/fE8qT3kW。它仍然支持两种版本的OpenID,尽管它不支持检索属性。所以总之,LightOpenID既不复杂也不臃肿。 – Mewp

回答

2

在你的问题的类不支持OpenID 2.0的。因此,如果不添加大量代码,它将不适用于Google。

4

问题是Google不仅仅提供一个OpenID端点。

OpenId端点包含用户的标识符。

我们在这里有什么被称为发现网址。

这是一个静态url,您可以指示任何用户,服务本身会识别用户并返回每个用户唯一标识的url。

然而,这不是由的OpenID客户端库,包括大多数官方OpenID的网站上链接的正确实施。

即使是Zend框架库也无法处理它。

但是我发现我从不同的角度分析了一个类,我对此非常满意。在我工作的公司,我们已经将它成功地集成到多个生产环境中,并且没有遇到任何问题。

您可能还会对我的另一篇帖子感兴趣,这篇帖子涉及到使Facebook成为openid Provider的问题。我使用的是类,也支持谷歌,也可以在那里找到:

Best way to implement Single-Sign-On with all major providers?

+0

顺便说一句:lightopenid接缝做正确的谷歌openID发现Url ...) – Rella

+0

是的,lightopenid是我也使用的类。 –

+0

我必须诚实地说,我没有真正仔细看看你的问题,因为从标题看来我似乎很清楚。现在我看到你已经开始和lightopenid一起工作了。我投入了大量的研究,只能推荐坚持下去。 –

相关问题