gpt4 book ai didi

php - 使用 PHP 创建 LDAP 服务器

转载 作者:可可西里 更新时间:2023-10-31 22:49:44 25 4
gpt4 key购买 nike

我想用 PHP 创建一个基于 Web 的应用程序,它接收 LDAP 请求并发回 LDAP 响应,但实际上并不使用 LDAP 服务器。具体来说,我想将 MySQL 数据库中的联系人表作为 LDAP 地址簿提供给 Thunderbird。

两个问题:

  1. 是否有用于使用 PHP 实现 LDAP 服务器的现有库? (PHP_LDAP 包用于创建 LDAP 客户端,其中 PHP 应用程序连接到现有的 LDAP 服务器。)

  2. LDAP 数据实际上是如何从客户端获取到我的脚本中的? LDAP 是否通过 HTTP 传输?请求将出现在:

    $HTTP_RAW_POST_DATA

或类似的? Apache 可以处理 LDAP 请求并将它们传递到我的脚本中,还是它是一个完全不同的协议(protocol),需要不同的“监听器”应用程序来处理?

最佳答案

可以用这个库创建一个纯 PHP LDAP 服务器(我最初写它是为了 LDAP 客户端):

https://github.com/FreeDSx/LDAP

它在客户端请求的请求处理程序(只是一个接口(interface))的基础上工作。基本上,您扩展了一个类,该类将处理客户端请求并发回响应(无论如何在搜索的情况下)。一个基本的例子:

  1. 创建一个扩展库中通用请求处理程序的请求处理程序:
namespace Foo;

use FreeDSx\Ldap\Server\RequestHandler\GenericRequestHandler;

class LdapRequestHandler extends GenericRequestHandler
{
/**
* @var array
*/
protected $users = [
'user' => '12345',
];

/**
* Validates the username/password of a simple bind request
*
* @param string $username
* @param string $password
* @return bool
*/
public function bind(string $username, string $password): bool
{
return isset($this->users[$username]) && $this->users[$username] === $password;
}

/**
* Override the search request. This must send back an entries object.
*
* @param RequestContext $context
* @param SearchRequest $search
* @return Entries
*/
public function search(RequestContext $context, SearchRequest $search): Entries
{
// Do your logic here with the search request, return entries...
return new Entries(
Entry::create('cn=Foo,dc=FreeDSx,dc=local', [
'cn' => 'Foo',
'sn' => 'Bar',
'givenName' => 'Foo',
]),
Entry::create('cn=Chad,dc=FreeDSx,dc=local', [
'cn' => 'Chad',
'sn' => 'Sikorra',
'givenName' => 'Chad',
])
);
}
}
  1. 使用请求处理程序,创建一个在端口 389 上监听客户端的 LDAP 服务器进程:
use FreeDSx\Ldap\LdapServer;
use Foo\LdapRequestHandler;

$server = new LdapServer([ 'request_handler' => LdapRequestHandler::class ]);
$server->run();

这里有更多关于库的服务器组件的文档:

https://github.com/FreeDSx/LDAP/tree/master/docs/Server

一些注意事项:

  • 目前服务器不支持分页/vlv
  • 目前无法将控制权从请求处理程序返回给客户端。

关于php - 使用 PHP 创建 LDAP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6039602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com