magium / active-directory
提供对Active Directory实现的连接
Requires
- league/oauth2-client: ^1.4
- magium/configuration-manager: ^1.0.1
- microsoft/microsoft-graph: ^1.0
- zendframework/zend-psr7bridge: ^0.2
Requires (Dev)
- magium/magium: ~1.1
- phpunit/phpunit: ^6.0 | ^7.0
README
对所有人的道歉。我没有时间维护这段代码。如果您有兴趣维护这段代码,请随意将其分支或创建衍生作品。我会把人们从这里引导过去。
Magium Active Directory Integration
用于与Azure Active Directory简单易用的PHP集成.
这是一个简单的库,它使用league/oauth2-client
提供基于OAuth2的Active Directory集成。默认情况下,它配置为与Azure上的Active Directory一起工作,尽管我没有测试过,但您可以为主要的适配器提供一个不同的配置对象,您应该能够验证任何具有OAuth2连接的Active Directory实现。
库有两个目的(好吧,三个)。
- 为任何基于PHP的应用程序提供少于5分钟的安装和集成时间
- 为其他第三方集成到Microsoft Azure Active Directory提供跳板,例如Magento、Drupal、Oro等。
- (提供使用其他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()
方法将执行以下三件事情之一。
- 检查会话并发现用户未登录,将该用户重定向到他们的Azure Active Directory登录页面
- 验证来自Active Directory的返回数据
- 如果该人已登录,则简单地返回
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 实例进行身份验证,请确保您提供了要验证的目录的目录 ID。以下所有示例都包括目录配置密钥,其默认值为 "common"。请确保您不仅使用 client_id 验证了正确的应用程序,而且也使用目录密钥验证了正确的目录。
使用 PHP 数组进行配置
现在,我知道 MCM 是新的,你可能还没有使用它。这就是为什么我提供了一种方法,让您可以在不使用完整的 MCM 的情况下配置适配器。您可以使用 Magium\Configuration\Config\Repository\ArrayConfigurationRepository
类提供一个原始数组,该数组将被映射到两个配置设置 magium/ad/client_id
和 magium/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();