001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     https://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020
021package org.apache.directory.ldap.client.api;
022
023
024import java.util.ArrayList;
025import java.util.Arrays;
026import java.util.List;
027
028import org.apache.directory.api.ldap.model.constants.SaslQoP;
029import org.apache.directory.api.ldap.model.constants.SaslSecurityStrength;
030import org.apache.directory.api.ldap.model.message.Control;
031import org.apache.directory.api.util.Strings;
032
033
034/**
035 * Holds the data required to complete the SASL operation
036 * 
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public abstract class AbstractSaslRequest implements SaslRequest
040{
041    /** The mechanism used to decode user identity */
042    protected String saslMechanism;
043
044    /** The list of controls */
045    protected List<Control> controls = new ArrayList<>();
046
047    /** The username */
048    protected String username;
049
050    /** The credentials */
051    protected byte[] credentials;
052
053    /** The realm name on the server */
054    protected String realmName;
055
056    /** The authorization ID of the entity */
057    protected String authorizationId;
058
059    /** The quality of protection */
060    protected SaslQoP qualityOfProtection;
061
062    /** The security strength */
063    protected SaslSecurityStrength securityStrength;
064
065    /** Require mutual authentication */
066    protected boolean mutualAuthentication = false;
067
068
069    /**
070     * Creates a new instance of SaslRequest.
071     *
072     * @param saslMechanism the SASL mechanism
073     */
074    protected AbstractSaslRequest( String saslMechanism )
075    {
076        this.saslMechanism = saslMechanism;
077    }
078
079
080    /**
081     * Adds the given controls.
082     *
083     * @param controls the controls
084     */
085    public void addAllControls( Control[] controls )
086    {
087        this.controls.addAll( Arrays.asList( controls ) );
088    }
089
090
091    /**
092     * Adds the given control.
093     *
094     * @param control the control
095     */
096    public void addControl( Control control )
097    {
098        this.controls.add( control );
099    }
100
101
102    /**
103     * {@inheritDoc}
104     */
105    @Override
106    public String getAuthorizationId()
107    {
108        return authorizationId;
109    }
110
111
112    /**
113     * {@inheritDoc}
114     */
115    @Override
116    public Control[] getControls()
117    {
118        return controls.toArray( new Control[0] );
119    }
120
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public byte[] getCredentials()
127    {
128        if ( credentials != null )
129        {
130            return credentials;
131        }
132        else
133        {
134            return Strings.EMPTY_BYTES;
135        }
136    }
137
138
139    /**
140     * {@inheritDoc}
141     */
142    @Override
143    public SaslQoP getQualityOfProtection()
144    {
145        return qualityOfProtection;
146    }
147
148
149    /**
150     * {@inheritDoc}
151     */
152    @Override
153    public String getRealmName()
154    {
155        return realmName;
156    }
157
158
159    /**
160     * {@inheritDoc}
161     */
162    @Override
163    public String getSaslMechanism()
164    {
165        return saslMechanism;
166    }
167
168
169    /**
170     * {@inheritDoc}
171     */
172    @Override
173    public SaslSecurityStrength getSecurityStrength()
174    {
175        return securityStrength;
176    }
177
178
179    /**
180     * {@inheritDoc}
181     */
182    @Override
183    public String getUsername()
184    {
185        return username;
186    }
187
188
189    /**
190     * {@inheritDoc}
191     */
192    @Override
193    public boolean isMutualAuthentication()
194    {
195        return mutualAuthentication;
196    }
197
198
199    /**
200     * Sets the Authorization ID
201     *
202     * @param authorizationId The authorization ID
203     */
204    public void setAuthorizationId( String authorizationId )
205    {
206        this.authorizationId = authorizationId;
207    }
208
209
210    /**
211     * Sets the credentials.
212     *
213     * @param credentials the credentials
214     */
215    public void setCredentials( byte[] credentials )
216    {
217        this.credentials = credentials;
218    }
219
220
221    /**
222     * Sets the credentials.
223     *
224     * @param credentials the credentials
225     */
226    public void setCredentials( String credentials )
227    {
228        this.credentials = Strings.getBytesUtf8( credentials );
229    }
230
231
232    /**
233     * Sets the flag indicating if mutual authentication is required.
234     *
235     * @param mutualAuthentication the flag indicating if mutual authentication is required
236     */
237    public void setMutualAuthentication( boolean mutualAuthentication )
238    {
239        this.mutualAuthentication = mutualAuthentication;
240    }
241
242
243    /**
244     * Sets the quality of protection.
245     *
246     * @param qualityOfProtection the quality of protection
247     */
248    public void setQualityOfProtection( SaslQoP qualityOfProtection )
249    {
250        this.qualityOfProtection = qualityOfProtection;
251    }
252
253
254    /**
255     * Sets the realm name.
256     * 
257     * @param realmName The realm name
258     */
259    protected void setRealmName( String realmName )
260    {
261        this.realmName = realmName;
262    }
263
264
265    /**
266     * Sets the SASL mechanism
267     *
268     * @param saslMechanism the SASL mechanism
269     */
270    protected void setSaslMechanism( String saslMechanism )
271    {
272        this.saslMechanism = saslMechanism;
273    }
274
275
276    /**
277     * Sets the security strength.
278     *
279     * @param securityStrength the security strength
280     */
281    public void setSecurityStrength( SaslSecurityStrength securityStrength )
282    {
283        this.securityStrength = securityStrength;
284    }
285
286
287    /**
288     * Sets the username.
289     *
290     * @param username the username
291     */
292    public void setUsername( String username )
293    {
294        this.username = username;
295    }
296}