添加 samlp:extensions 到 authsources.php SimpleSamlPHP

Add samlp:extensions to authsources.php SimpleSamlPHP

我希望完成的是将以下示例添加到 Authnrequest

<samlp:Extensions>
   <somens:TheExtensionName xmlns:somens="http://uriofextension/">
<somens:TheExtensionName Name="AttributeName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"
isRequired="true"/>
   </somens:TheExtensionName >
</samlp:Extensions>

通过使用 authsource.php,我如何才能做到这一点?

我已经阅读了文档并在

https://simplesamlphp.org/docs/stable/saml:sp

5.8 使用 samlp:Extensions

他们有:

$dom = \SAML2\DOMDocumentFactory::create();
$ce = $dom->createElementNS('http://www.example.com/XFoo', 'xfoo:test', 'Test data!');
$ext[] = new \SAML2\XML\Chunk($ce);

$auth = new \SimpleSAML\Auth\Simple('default-sp');
$auth->login(array(
    'saml:Extensions' => $ext,
));

但是这段代码包含在什么地方?由于我没有运气就将它添加到 authsources.php,并且无法弄清楚如何使用它,还要考虑我对 php 缺乏了解,所以也许我只是把事情搞砸了。

这是我在 authsources.php

中尝试过的

请忽略属于所提供示例的部分代码

<?php

$dom = \SAML2\DOMDocumentFactory::create();
$ce = $dom->createElementNS('http://www.example.com/XFoo', 'xfoo:test', 'Test data!');
$ext[] = new \SAML2\XML\Chunk($ce);

$config = array(

    'sp.name' => array(
        'saml:SP',
        'privatekey'  => '/certs/privkey.pem',
        'certificate' => '/certs/fullchain.pem',
        'entityID' => 'entityID',
        'idp' => 'idpID',
        'saml:Extensions' => $ext,

    ),

);

好吧,我还不能使用 authsources.php,但设法用我需要的 samlp:extensions authnrequest 重新创建了它。

我创建了一个 php 应用程序并将其连接到 Simplesaml 服务提供商,以便使用 samlp:Extensions 发送请求,这里是为遇到相同问题的人提供的代码:

app/app.php

<?php
    //Load SimpleSAMLphp.
    require_once('/var/www/simplesamlphp/lib/_autoload.php');

    //Initiate a SimpleSAML_Auth_Simple object.
    $as = new SimpleSAML_Auth_Simple('name-of-sp');

    //Standard PHP lib more info at -> http://php.net/manual/en/domdocument.createelementns.phphttp://php.net/manual/en/domdocument.createelementns.php
    $dom = SAML2_DOMDocumentFactory::create();

    $attributes_ext = $dom->createElementNS('namespace-uri', 'fa:RequestedAttributes');

    $item = $dom->createElementNS('namespace-uri', 'fa:RequestedAttribute');
    $attrName = $dom->createAttribute('Name');
    $attrName->value = 'attributeName';

    $attrNameFormat = $dom->createAttribute('NameFormat');
    $attrNameFormat->value = "urn:oasis:names:tc:SAML:2.0:attrname-format:uri";

    $attrRequirement = $dom->createAttribute('isRequired');
    $attrRequirement->value = "true";

    $item->appendChild($attrName);
    $item->appendChild($attrNameFormat);
    $item->appendChild($attrRequirement);

    $attributes_ext->appendChild($item);
    $ext[] = new SAML2_XML_Chunk($attributes_ext);

    $as->login(array(
        'saml:Extensions' => $ext,
    ));

    //If the user is not authenticated, authenticate the user
    $as->requireAuth();

    //Get the users attributes and print them.
    $attributes = $as->getAttributes();
    print_r($attributes);

    //Output the attributes to a file
    $myFile = "/tmp/attributes.log";
    $fh = fopen($myFile, 'a') or die("can't open file");
    $stringData = print_r($attributes, true);
    fwrite($fh, $stringData);
    fclose($fh);

    //Displays a Login and Logout link
    $url_in = $as->getLoginURL();
    $url_out = $as->getLogoutURL();
    print('<br><a href="' . htmlspecialchars($url_in) . '">Login</a>');
    print('<br><a href="' . htmlspecialchars($url_out) . '">Logout</a><br>');

    //If using PHP sessions in SimpleSAMLphp cleanup the SimpleSAMLphp session to be able to use $_SESSION
    $session = SimpleSAML_Session::getSessionFromRequest();
    $session->cleanup();

    //Display PHP information
    phpinfo()
?>
</body>
</html>

将其设置为 运行 以进行实验

php -S 0.0.0.0:5000 -t 应用程序/

导航到 localhost:5000/app.php,这将自动将您转到 IDP 的登录,使用 SP 配置。您可以尝试使用 simplesamlphp

提供的 authsources.php 个示例