1<?php
2
3/**
4 * This file is part of the FreeDSx SASL 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\Sasl;
13
14/**
15 * Holds SASL context specific data related to a particular mechanism challenge / response.
16 *
17 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
18 */
19class SaslContext
20{
21    /**
22     * @var bool
23     */
24    protected $isAuthenticated = false;
25
26    /**
27     * @var bool
28     */
29    protected $isComplete = false;
30
31    /**
32     * @var bool
33     */
34    protected $hasSecurityLayer = false;
35
36    /**
37     * @var bool
38     */
39    protected $isServerMode = false;
40
41    /**
42     * @var array
43     */
44    protected $data = [];
45
46    /**
47     * @var string|null
48     */
49    protected $response;
50
51    public function __construct(array $data = [])
52    {
53        $this->data = $data;
54    }
55
56    /**
57     * @param bool $isComplete
58     * @return $this
59     */
60    public function setIsComplete(bool $isComplete)
61    {
62        $this->isComplete = $isComplete;
63
64        return $this;
65    }
66
67    /**
68     * Whether or not the challenge sequence is complete.
69     */
70    public function isComplete(): bool
71    {
72        return $this->isComplete;
73    }
74
75    /**
76     * @param bool $isServerMode
77     * @return $this
78     */
79    public function setIsServerMode(bool $isServerMode)
80    {
81        $this->isServerMode = $isServerMode;
82
83        return $this;
84    }
85
86    /**
87     * Whether or not we are in the context of server mode for the exchange.
88     */
89    public function isServerMode(): bool
90    {
91        return $this->isServerMode;
92    }
93
94    /**
95     * Whether or not the message exchange has resulted is being successfully authenticated.
96     */
97    public function isAuthenticated(): bool
98    {
99        return $this->isAuthenticated;
100    }
101
102    /**
103     * Set whether or not the current context has authenticated.
104     */
105    public function setIsAuthenticated(bool $isAuthenticated)
106    {
107        $this->isAuthenticated = $isAuthenticated;
108
109        return $this;
110    }
111
112    /**
113     * Whether or not a security layer was negotiated as part of the message exchange.
114     */
115    public function hasSecurityLayer(): bool
116    {
117        return $this->hasSecurityLayer;
118    }
119
120    /**
121     * Set whether or not the current context has negotiated a security layer.
122     */
123    public function setHasSecurityLayer(bool $hasSecurityLayer): self
124    {
125        $this->hasSecurityLayer = $hasSecurityLayer;
126
127        return $this;
128    }
129
130    /**
131     * The next response, if any, to send in the challenge.
132     */
133    public function getResponse(): ?string
134    {
135        return $this->response;
136    }
137
138    /**
139     * @param string|null $response
140     * @return $this
141     */
142    public function setResponse(?string $response)
143    {
144        $this->response = $response;
145
146        return $this;
147    }
148
149    /**
150     * Get any mechanism specific data that needs to be stored as part of the message exchange.
151     */
152    public function getData(): array
153    {
154        return $this->data;
155    }
156
157    /**
158     * @param array $data
159     * @return $this
160     */
161    public function setData(array $data)
162    {
163        $this->data = $data;
164
165        return $this;
166    }
167
168    /**
169     * Check if a SASL specific data piece exists.
170     */
171    public function has(string $key): bool
172    {
173        return isset($this->data[$key]);
174    }
175
176    /**
177     * Get a SASL specific data piece. f
178     *
179     * @return mixed
180     */
181    public function get(string $key)
182    {
183        return $this->data[$key] ?? null;
184    }
185
186    /**
187     * Set the value of a SASL specific data piece.
188     */
189    public function set(string $key, $value): self
190    {
191        $this->data[$key] = $value;
192
193        return $this;
194    }
195}
196