AX 在 PHP OpenID 中的用法示例

2022-08-30 17:04:53

我正在使用JanRain的PHP OpenID库。它带有使用SReg扩展的示例脚本。但我希望它与Google一起使用(它实际上适用于身份验证),但Google使用AX(属换)而不是SReg来获取其他数据。由于某种原因,JanRain的库在示例脚本中缺少AX支持,并且AX脚本中的代码注释超出了我的理解范围,尽管SReg脚本中的注释清晰为1-2-3。

有谁知道如何在没有太多痛苦的情况下实施AX?


答案 1

遇到同样的问题。在AX中进行一些挖掘.php让我有了一个良好的开端。没有寻找任何错误,也没有测试过超出基本范围,也没有与Google以外的任何人一起测试。这并不漂亮:需要错误处理等。但这应该让你开始。如果我有一些强大的东西,将发布更新...

第一个投掷...

//  oid_request.php

// Just tested this with/for Google, needs trying with others ...
$oid_identifier = 'https://www.google.com/accounts/o8/id';

// Includes required files
require_once "Auth/OpenID/Consumer.php";
require_once "Auth/OpenID/FileStore.php";
require_once "Auth/OpenID/AX.php";

// Starts session (needed for YADIS)
session_start();

// Create file storage area for OpenID data
$store = new Auth_OpenID_FileStore('./oid_store');

// Create OpenID consumer
$consumer = new Auth_OpenID_Consumer($store);

// Create an authentication request to the OpenID provider
$auth = $consumer->begin($oid_identifier);

// Create attribute request object
// See http://code.google.com/apis/accounts/docs/OpenID.html#Parameters for parameters
// Usage: make($type_uri, $count=1, $required=false, $alias=null)
$attribute[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/contact/email',2,1, 'email');
$attribute[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/namePerson/first',1,1, 'firstname');
$attribute[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/namePerson/last',1,1, 'lastname');

// Create AX fetch request
$ax = new Auth_OpenID_AX_FetchRequest;

// Add attributes to AX fetch request
foreach($attribute as $attr){
    $ax->add($attr);
}

// Add AX fetch request to authentication request
$auth->addExtension($ax);

// Redirect to OpenID provider for authentication
$url = $auth->redirectURL('http://localhost:4001', 'http://localhost:4001/oid_catch.php');
header('Location: ' . $url);

...然后捕捉

<?php

//  oid_catch.php

// Includes required files
require_once "Auth/OpenID/Consumer.php";
require_once "Auth/OpenID/FileStore.php";
require_once "Auth/OpenID/AX.php";

// Starts session (needed for YADIS)
session_start();

// Create file storage area for OpenID data
$store = new Auth_OpenID_FileStore('./oid_store');

// Create OpenID consumer
$consumer = new Auth_OpenID_Consumer($store);

// Create an authentication request to the OpenID provider
$auth = $consumer->complete('http://localhost:4001/oid_catch.php');

if ($response->status == Auth_OpenID_SUCCESS) {
    // Get registration informations
    $ax = new Auth_OpenID_AX_FetchResponse();
    $obj = $ax->fromSuccessResponse($response);

    // Print me raw
    echo '<pre>';
    print_r($obj->data);
    echo '</pre>';
    exit;


} else {
  // Failed
}

这些应该是基础...


答案 2

请求一半正在工作,但是我在 Catch 中失败了。

如果上面的行

$auth = $consumer->complete('http://localhost:4001/oid_catch.php');

$response = $consumer->complete('http://localhost:4001/oid_catch.php');

否则,响应对象来自哪里?我没有收到openid.current_url在我的回复中检查网址?


推荐