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