从Azure AD与SAML 2连接TYPO3 fe_users的最佳方法是什么?

时间:2019-01-15 07:29:13

标签: typo3 azure-active-directory saml-2.0 typo3-9.x

我需要在TYPO3内联网上实现SSO,其中fe_users是从Azure AD同步的。该平台将在V9中。 有没有我尚未找到的兼容扩展? 如果不是,那么使用SAML 2.0实现自动身份验证的最佳方法是什么? 提前致谢, 雷切尔

2 个答案:

答案 0 :(得分:0)

是的,我们解决了这个要求。遵循此出色的教程,我们使用了SimpleSAMLphp来实现身份验证: https://www.lewisroberts.com/2015/09/05/single-sign-on-to-azure-ad-using-simplesamlphp/

当您能够连接时,您只需执行一个过程即可在获得saml用户属性时自动连接fe_user。

以下是该过程的简化摘要:

如果我们在未通过身份验证的情况下访问了TYPO3网站的网址,则重定向至这样的脚本:

// SimpleSamlPHP library
require_once (dirname(__FILE__) . /../../../../../../simplesamlphp/lib/_autoload.php');

//instanciation of a simple authentication
$as = new SimpleSAML_Auth_Simple('default-sp');

//requires authentication from Office 365
$as->requireAuth();

//retrieving information from the logged-in user
$attributes = $as->getAttributes();

//retrieve original url
$returnURL = $_GET['returnURL'];

//if a user is well connected
if($attributes){

    //redirection to the TYPO3 site with the username
    header('Location: /auth/?samlUident='.$attributes['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name'][0].'&recupURL='.$returnURL);
}

这是auth页面的功能的简化摘要:

//if a get saml is in the url
if(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('samlUident')){

    //recovering username for TYPO3 authentication
    $loginData = array(
        'uname' => \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('samlUident'), //username
        'status' => 'login'
    );

    //TYPO3 session creation
    $frontendUserAuthentication = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Authentication\\FrontendUserAuthentication');
    $frontendUserAuthentication->checkPid = false;
    $info = $frontendUserAuthentication->getAuthInfoArray();

    $user_db = $frontendUserAuthentication->fetchUserRecord($info['db_user'], $loginData['uname']);

    //if a user exists
    if ($user_db){

        //authentication
        $GLOBALS['TSFE']->fe_user->forceSetCookie = TRUE;
        $GLOBALS['TSFE']->fe_user->dontSetCookie = false;
        $GLOBALS['TSFE']->fe_user->start();
        $GLOBALS['TSFE']->fe_user->createUserSession($user_db);
        $GLOBALS['TSFE']->fe_user->user = $user_db;
        $GLOBALS['TSFE']->fe_user->setAndSaveSessionData('dummy', TRUE);
        $GLOBALS['TSFE']->fe_user->loginUser = 1;

    }
}

干杯, 雷切尔

答案 1 :(得分:0)

由于@Rakel(及其他),我终于解决了SAML身份验证要求。仍然我使用了她的解决方案中所述的稍有不同和更直接的方法。

我使用身份验证服务来实现SAML登录过程。 为了处理SAML登录本身,我使用了我真正建议的库 SimpleSamlPHP 。它非常简单,并且提供的用于测试SAML配置的前端非常方便,无需使用TYPO3即可测试身份提供商(Idp)配置。

有关详细信息,请查看以下内容:https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Authentication/Index.html

首先,您需要创建一个扩展 TYPO3 \ CMS \ Core \ Authentication \ AuthenticationService 的类。此类必须实现方法“ getUser” “ authUser”

namespace Vendor\Extension\Service;

use SimpleSAML\Auth\Simple;
use TYPO3\CMS\Core\Authentication\AuthenticationService;
class SamlAuth extends AuthenticationService
{

    public function getUser() {
        // Create new Simple Auth with SimpleSamlPHP
        $as = new Simple($config['sp']);
        // Require authentication, this redirects you to the Idp and then comes back
        $as->requireAuth();
        // Get the attributes provides by your Idp
        $attributes = $as->getAttributes();
        // Please consult the API for details on fetchUserRecord
        // Also the SAML attributes may vary depending on your Idp implementation
        $user = $this->fetchUserRecord($attributes['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name'];
    }

    public function authUser(array $user): int {
        return is_array($user) ? 200 : 0;
    }
}

然后,您需要在扩展名“ ext_localconf.php”中注册该服务。

...
TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService(
      'my_extension',
      'auth',
      Vendor\Extension\Service\SamlAuth::class,
      [
          'title' => 'Saml Authentication for Frontend Users',
          'description' => 'Authenticates FeUsers via Saml',
          'subtype' => 'authUserFE,getUserFE',
          'available' => true,
          'priority' => 80,
          'quality' => 80,
          'os' => '',
          'exec' => '',
          'className' => Vendor\Extension\Service\SamlAuth::class
      ]
    );
...

请注意

  • 这只是我最终代码的简化版本。只是为了让您开始构想。
  • 此外,您还需要正确配置SimpleSamlPHP。请查看其文档以获取详细信息。

方法“ getUser” 应该返回一个数组,其中包含要在FeUser中登录的数组及其所有参数。

方法“ authUser” 仅返回200或0。请查看此链接以了解要返回的数字:https://docs.typo3.org/m/typo3/reference-services/7.6/en-us/Authentication/Index.html#authentication-service-chain 返回“ 200”后,将创建FeUser对象,并且用户已登录。您无需自己摆弄$ GLOBALS ['TSFE']。这是一个巨大的好处,因为它使您的代码更短,更易于阅读。

尽管如此,通过阅读这里的所有文档和响应以及在Slacks TYPO3频道上,我学到了很多东西。 感谢所有帮助我的人。非常感谢。