emersonfidelon/active-directory

提供与Active Directory实现的连接

1.1.1 2020-08-19 12:06 UTC

README

向所有人道歉。我没有时间维护这段代码。如果您有兴趣维护这段代码,请随意fork它或创建衍生作品。我会从这里引导人们去那里。

Magium Active Directory集成

用于与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个定义良好的获取器来帮助您进行此映射

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();