aleszatloukal/active-directory

提供对Active Directory实现的连接

1.2.1 2021-07-26 15:45 UTC

README

用于与Azure Active Directory轻松集成的PHP库.

这是一个简单的库,使用league/oauth2-client提供基于OAuth2的Active Directory集成。默认配置为与Azure上的Active Directory一起工作,但尽管我没有测试过,你可以提供一个不同的配置对象给主适配器,你应该能够认证任何具有OAuth2连接的Active Directory实现。

该库有两个目的(好吧,三个)。

  1. 为任何基于PHP的应用程序提供小于5分钟的安装和集成时间
  2. 为其他第三方集成到Microsoft Azure Active Directory提供启动平台,例如Magento、Drupal、Oro等。
  3. (提供使用其他Magium库的库,让人们看到所有Magium功能有多么出色)

首先,观看YouTube上的安装视频。它展示了如何在Azure Active Directory中创建应用程序。安装过程中的一个重要部分是访问https://apps.dev.microsoft.com上的Microsoft应用控制台。你将在这里获取所有的认证密钥等。

注意 Azure不会从安全URL(即他们的登录页面)重定向到不安全的页面(即你的页面)。换句话说,不是从HTTPS到HTTP。用另一种话说,如果你使用Azure,你将需要使用HTTPS。虽然世界上还有更糟糕的事情...比如使用HTTPS。

基本用法

在应用程序中需要认证的任何地方,你可以提供此代码(正确架构的,不是复制粘贴,换句话说)

$ad = new \Magium\ActiveDirectory\ActiveDirectory(
    $configuration, // shown later
    $psr7CompatibleRequest
);

$entity = $ad->authenticate();

authenticate()方法将执行以下三种操作之一。

  1. 检查会话并看到用户未登录,将此人重定向到他们的Azure Active Directory登录页面
  2. 验证来自Active Directory的返回数据
  3. 如果某人已经登录,直接返回Entity对象。

如果你想注销,只需这样做

$ad->forget();

请注意,这只会从会话中清除AD实体,不会对你的应用程序进行任何其他会话清理。

显然,这个库不是旨在成为你唯一的会话管理方式,尽管对于简单的应用程序,你可以这样使用它。你很可能会想将AD检索到的数据与本地账户链接起来。`Entity`类有3个定义的getter,可以帮助你进行此映射。

echo $entity->getName() . '<Br />'; // The user's name
echo $entity->getOid() . '<Br />'; //The user's AD object ID, useful for mapping to a local user obhect
echo $entity->getPreferredUsername() . '<Br />'; // The user's username, usually an email address.

安装

composer require magium/active-directory

完成。

配置

这稍微复杂一些,但不应过于复杂。

默认情况下,基本配置由Magium配置管理器管理。但是,话虽如此,MCM有一个非常简单的机制,允许你不必使用底层管道。我相信底层管道最终会使应用程序管理更容易,但我不想强迫你使用它。

使用Magium配置管理器进行配置

配置管理器提供了在CLI和(最终)基于Web的界面中管理和部署设置的方法。如果你使用配置管理器,你需要获取配置工厂的实例,它提供了管理器的实例,它提供了配置对象。`ActiveDirectory`适配器需要这个配置对象。

// Convert to PSR7 request object
$request = \Zend\Psr7Bridge\Psr7ServerRequest::fromZend(
    new \Zend\Http\PhpEnvironment\Request()
);

$factory = new \Magium\Configuration\MagiumConfigurationFactory();
$manager = $factory->getManager();
$configuration = $manager->getConfiguration();

$adapter = new \Magium\ActiveDirectory\ActiveDirectory($configuration, $request);

$entity = $adapter->authenticate();

首先,在您的应用程序根目录下运行 vendor/bin/magium magium:configuration:list-keys。这要在您根据GitHub链接中的说明配置了MCM之后完成。您将看到类似以下输出

Valid configuration keys
authentication/ad/enabled (default: 0)
        (Is the Magium Active Directory integration enabled?)

authentication/ad/client_id
        (You need to configure an application in Active Directory and enter its ID here)

authentication/ad/client_secret
        (When you created an application in Active Directory you should have received a one-time use key.  Enter that here.  )

authentication/ad/directory (default: common)
        (Provide a directory ID if you are using your own Azure instance instead of Microsoft's global database.  The directory ID is usually found under the directory properties.)

authentication/ad/return_url
        (This is the URL you want Active Directory to redirect back to after authentication.)

authentication/ad/remap_https (default: 1)
        (Should the system remap HTTP-based URLs to HTTPS.  Azure Active Directory generally will not redirect to a non-secure URL.  Enabling this setting protects against that.)

您需要提供这两个值进行配置

vendor/bin/magium magium:configuration:set magium/ad/client_id '<my client id>'
Set magium/ad/client_id to <my client id> (context: default)
Don't forget to rebuild your configuration cache with magium:configuration:build

vendor/bin/magium magium:configuration:set magium/ad/client_secret '<my client secret>'
Set magium/ad/client_secret to <my client secret> (context: default)
Don't forget to rebuild your configuration cache with magium:configuration:build

vendor/bin/magium magium:configuration:build
Building context: default
Building context: production
Building context: development

然后您就可以继续了。

注意!!! 适配器的默认设置将允许任何拥有Microsoft ID的人访问您的系统,就像允许任何拥有有效Twitter账户的Twitter用户访问您的系统一样。如果您想验证自己的Active Directory实例,请确保提供要验证的目录的Directory ID。以下所有示例都包括目录配置键,默认为"common"。请确保您不仅使用client_id验证正确的应用程序,还使用目录键验证正确的目录。

使用PHP数组进行配置

现在,我知道MCM是新的,你可能还没有使用它。这就是为什么我提供了一个方法,让您可以在不使用完整的MCM的情况下配置适配器。您可以使用Magium\Configuration\Config\Repository\ArrayConfigurationRepository类提供一个原始数组,该数组将映射到两个配置设置magium/ad/client_idmagium/ad/client_secret

session_start();

$config = [
    'authentication' => [
        'ad' => [
            'client_id' => '<my client id>',
            'client_secret' => '<my client secret>',
            'enabled' => 'yes',
            'directory' => '<common or directory ID>'
        ]
    ]
];

$request = new \Zend\Http\PhpEnvironment\Request();

$ad = new \Magium\ActiveDirectory\ActiveDirectory(
    new \Magium\Configuration\Config\Repository\ArrayConfigurationRepository($config),
    Zend\Psr7Bridge\Psr7ServerRequest::fromZend(new \Zend\Http\PhpEnvironment\Request())
);

$entity = $ad->authenticate();

echo $entity->getName() . '<Br />';
echo $entity->getOid() . '<Br />';
echo $entity->getPreferredUsername() . '<Br />';

使用YAML进行配置

基本上相同,但是您将使用YamlConfigurationRepository而不是使用ArrayConfigurationRepository。它非常相似

$yaml = <<<YAML
authentication:
    ad:
        client_id: <value>
        client_secret: <value>
        enabled: yes
        directory: <common or directory ID>
YAML;

$obj = new YamlConfigurationRepository(trim($yaml));
$ad = new \Magium\ActiveDirectory\ActiveDirectory(
    $obj, $request
);

$entity = $ad->authenticate();

使用JSON进行配置

基本上相同,但是您将使用JsonConfigurationRepository而不是使用YamlConfigurationRepository。它非常相似

 $json = <<<JSON
        {
            "authentication": {
                "ad": {
                    "client_id": "<value>",
                    "client_secret": "<value>",
                    "enabled": "yes",
                    "directory": "<common or directory ID>"
                }
            }
        }
JSON;
        $obj = new JsonConfigurationRepository(trim($json));
$ad = new \Magium\ActiveDirectory\ActiveDirectory(
    $obj, $request
);

$entity = $ad->authenticate();

使用INI文件进行配置

基本上相同,但是您将使用IniConfigurationRepository而不是使用JsonConfigurationRepository。它非常相似

$ini = <<<INI
[authentication]
ad[client_id] = <value>
ad[client_srcret] = <value>
ad[enabled] = yes
ad[directory] = <common or directory ID>
INI;

$obj = new IniConfigurationRepository(trim($ini));
$ad = new \Magium\ActiveDirectory\ActiveDirectory(
    $obj, $request
);

$entity = $ad->authenticate();