1<?php
2/**
3 * This file is part of the FreeDSx LDAP package.
4 *
5 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace FreeDSx\Ldap\Protocol\ServerProtocolHandler;
12
13use FreeDSx\Ldap\Exception\RuntimeException;
14use FreeDSx\Ldap\Operation\Request\AnonBindRequest;
15use FreeDSx\Ldap\Protocol\LdapMessageRequest;
16use FreeDSx\Ldap\Protocol\Queue\ServerQueue;
17use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface;
18use FreeDSx\Ldap\Server\Token\AnonToken;
19use FreeDSx\Ldap\Server\Token\TokenInterface;
20
21/**
22 * Handles anonymous bind requests.
23 *
24 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
25 */
26class ServerAnonBindHandler extends ServerBindHandler
27{
28    /**
29     * {@inheritDoc}
30     */
31    public function handleBind(LdapMessageRequest $message, RequestHandlerInterface $dispatcher, ServerQueue $queue, array $options): TokenInterface
32    {
33        $request = $message->getRequest();
34        if (!$request instanceof AnonBindRequest) {
35            throw new RuntimeException(sprintf(
36                'Expected an AnonBindRequest, got: %s',
37                get_class($request)
38            ));
39        }
40
41        $this->validateVersion($request);
42        $queue->sendMessage($this->responseFactory->getStandardResponse($message));
43
44        return new AnonToken(
45            $request->getUsername(),
46            $request->getVersion()
47        );
48    }
49}
50