1<?php
2
3/**
4 * This file is part of the FreeDSx LDAP package.
5 *
6 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace FreeDSx\Ldap\Operation\Request;
13
14use FreeDSx\Asn1\Asn1;
15use FreeDSx\Asn1\Type\AbstractType;
16use FreeDSx\Ldap\Exception\BindException;
17
18/**
19 * Represents a simple bind request consisting of a username (dn, etc) and a password.
20 *
21 * AuthenticationChoice ::= CHOICE {
22 *     simple                  [0] OCTET STRING,
23 *     -- 1 and 2 reserved
24 *     sasl                    [3] SaslCredentials,
25 *     ...  }
26 *
27 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
28 */
29class SimpleBindRequest extends BindRequest
30{
31    /**
32     * @var string
33     */
34    protected $password;
35
36    /**
37     * @param string $username
38     * @param string $password
39     * @param int $version
40     */
41    public function __construct(string $username, string $password, int $version = 3)
42    {
43        $this->username = $username;
44        $this->password = $password;
45        $this->version = $version;
46    }
47
48    /**
49     * @return string
50     */
51    public function getPassword(): string
52    {
53        return $this->password;
54    }
55
56    /**
57     * @param string $password
58     * @return $this
59     */
60    public function setPassword(string $password)
61    {
62        $this->password = $password;
63
64        return $this;
65    }
66
67    /**
68     * {@inheritdoc}
69     */
70    protected function getAsn1AuthChoice(): AbstractType
71    {
72        return Asn1::context(0, Asn1::octetString($this->password));
73    }
74
75    /**
76     * @throws BindException
77     */
78    protected function validate(): void
79    {
80        if ($this->username === '' || $this->password === '') {
81            throw new BindException('A simple bind must have a non-empty username and password.');
82        }
83    }
84}
85