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