SAML - 从身份提供程序获取响应


SAML - Getting response from the identity provider

我正在尝试使用 onelogin.com 实现SAML身份验证系统,我正在使用这个php库 https://github.com/simplesamlphp/saml2 和下面的代码

    // Set up an AuthnRequest
    $request = new SAML2_AuthnRequest();
    // $request->setId(SAML2_Utils::generateId());
    $request->setIssuer('http://localhost:8888/yii2/dw/advanced/frontend/web/index.php?r=site/auth');
    $request->setDestination('https://app.onelogin.com/trust/saml2/http-post/sso/418578');
    // Send it off using the HTTP-Redirect binding
    $binding = new SAML2_HTTPRedirect();
    $binding->send($request);

上面的代码成功地对我进行身份验证并将我回发到我的成功页面,但我不知道如何从发布的数据中获取用户信息。

我正在尝试下面的代码来获取用户数据

    $response = new 'SAML2_Response();
    print_r($response);

它给出了以下数据

SAML2_Response Object
(
[assertions:SAML2_Response:private] => Array
    (
    )
[inResponseTo:SAML2_StatusResponse:private] => 
[status:SAML2_StatusResponse:private] => Array
    (
        [Code] => urn:oasis:names:tc:SAML:2.0:status:Success
        [SubCode] => 
        [Message] => 
    )
[extensions:protected] => 
[tagName:SAML2_Message:private] => Response
[id:SAML2_Message:private] => _afe4d7fd7add270de7d334231e2eec68d1492363130
[issueInstant:SAML2_Message:private] => 14340322405
[destination:SAML2_Message:private] => 
[consent:SAML2_Message:private] => urn:oasis:names:tc:SAML:2.0:consent:unspecified
[issuer:SAML2_Message:private] => 
[relayState:SAML2_Message:private] => 
[document:protected] => 
[signatureKey:SAML2_Message:private] => 
[messageContainedSignatureUponConstruction:protected] => 
[certificates:SAML2_Message:private] => Array
    (
    )
[validators:SAML2_Message:private] => Array
    (
    )
 )

从上面的输出中,我无法获取用户信息,请帮助。

您的代码:

$response = new 'SAML2_Response();

实际上会创建一个新的 SAML 2.0 响应对象,而不是解析从 IDP 回发的对象。此调用应由想要创建 SAML 响应的 IDP 使用。您应该希望在断言使用者 (ACS) URL 上执行类似于以下代码的内容:

$b = SAML2_Binding::getCurrentBinding();
$response = $b->receive();

但我强烈建议您使用simpleSAMLphp本身(或其他第三方SAML实现)将SAML集成到PHP应用程序中,因为它处理所有复杂且安全敏感的SAML处理。

SAML 2.0 仅适用于希望将 SAML 用于 Web SSO 以外的其他目的的开发人员,或者由于 simpleSAMLphp 不适合他们而想要重建 SAML 实现的开发人员。假设你没有simpleSAMLphp无法满足的要求,那么你最好(和安全)使用它。此外,如果您有扩展要求,我会为 simpleSAMLphp 创建一个拉取请求,而不是从头开始重做该项目。

@HansZ是对的。直接使用 simpleSAMLphp 的 SAML 核心而不是整个框架时要小心。

我还想提出一个替代方案,既然你想将你的应用程序与 onelogin.com 连接起来,为什么不使用OneLogin的PHP SAML工具包呢? https://github.com/onelogin/php-saml