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\Exception\EncoderException;
16use FreeDSx\Asn1\Exception\PartialPduException;
17use FreeDSx\Asn1\Type\AbstractType;
18use FreeDSx\Asn1\Type\SequenceType;
19use FreeDSx\Ldap\Exception\ProtocolException;
20
21/**
22 * RFC 3062. A password modify extended request.
23 *
24 * PasswdModifyRequestValue ::= SEQUENCE {
25 *     userIdentity    [0]  OCTET STRING OPTIONAL
26 *     oldPasswd       [1]  OCTET STRING OPTIONAL
27 *     newPasswd       [2]  OCTET STRING OPTIONAL }
28 *
29 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
30 */
31class PasswordModifyRequest extends ExtendedRequest
32{
33    /**
34     * @var null|string
35     */
36    protected $userIdentity;
37
38    /**
39     * @var null|string
40     */
41    protected $oldPassword;
42
43    /**
44     * @var null|string
45     */
46    protected $newPassword;
47
48    /**
49     * @param null|string $userIdentity
50     * @param null|string $oldPassword
51     * @param null|string $newPassword
52     */
53    public function __construct(?string $userIdentity = null, ?string $oldPassword = null, ?string $newPassword = null)
54    {
55        $this->userIdentity = $userIdentity;
56        $this->oldPassword = $oldPassword;
57        $this->newPassword = $newPassword;
58        parent::__construct(self::OID_PWD_MODIFY);
59    }
60
61    /**
62     * @return null|string
63     */
64    public function getUsername(): ?string
65    {
66        return $this->userIdentity;
67    }
68
69    /**
70     * @param null|string $username
71     * @return $this
72     */
73    public function setUsername(?string $username)
74    {
75        $this->userIdentity = $username;
76
77        return $this;
78    }
79
80    /**
81     * @return null|string
82     */
83    public function getNewPassword(): ?string
84    {
85        return $this->newPassword;
86    }
87
88    /**
89     * @param null|string $newPassword
90     * @return $this
91     */
92    public function setNewPassword(?string $newPassword)
93    {
94        $this->newPassword = $newPassword;
95
96        return $this;
97    }
98
99    /**
100     * @return null|string
101     */
102    public function getOldPassword(): ?string
103    {
104        return $this->oldPassword;
105    }
106
107    /**
108     * @param null|string $oldPassword
109     * @return $this
110     */
111    public function setOldPassword(?string $oldPassword)
112    {
113        $this->oldPassword = $oldPassword;
114
115        return $this;
116    }
117
118    /**
119     * {@inheritdoc}
120     */
121    public function toAsn1(): AbstractType
122    {
123        $this->requestValue = Asn1::sequence();
124
125        if ($this->userIdentity !== null) {
126            $this->requestValue->addChild(Asn1::context(0, Asn1::octetString($this->userIdentity)));
127        }
128        if ($this->oldPassword !== null) {
129            $this->requestValue->addChild(Asn1::context(1, Asn1::octetString($this->oldPassword)));
130        }
131        if ($this->newPassword !== null) {
132            $this->requestValue->addChild(Asn1::context(2, Asn1::octetString($this->newPassword)));
133        }
134
135        return parent::toAsn1();
136    }
137
138    /**
139     * {@inheritDoc}
140     * @return PasswordModifyRequest
141     * @throws EncoderException
142     * @throws PartialPduException
143     */
144    public static function fromAsn1(AbstractType $type)
145    {
146        $request = self::decodeEncodedValue($type);
147        if ($request === null) {
148            return new self();
149        }
150        if (!($request instanceof SequenceType)) {
151            throw new ProtocolException('The password modify request is malformed.');
152        }
153
154        $userIdentity = null;
155        $oldPasswd = null;
156        $newPasswd = null;
157        foreach ($request as $value) {
158            /** @var AbstractType $value */
159            if ($value->getTagClass() !== AbstractType::TAG_CLASS_CONTEXT_SPECIFIC) {
160                throw new ProtocolException('The password modify request is malformed');
161            }
162            if ($value->getTagNumber() === 0) {
163                $userIdentity = $value;
164            } elseif ($value->getTagNumber() === 1) {
165                $oldPasswd = $value;
166            } elseif ($value->getTagNumber() === 2) {
167                $newPasswd = $value;
168            }
169        }
170
171        return new self(
172            $userIdentity !== null ? $userIdentity->getValue() : null,
173            $oldPasswd !== null ? $oldPasswd->getValue() : null,
174            $newPasswd !== null ? $newPasswd->getValue() : null
175        );
176    }
177}
178