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 *     http://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 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
073     *      the SASL mechanism
074     */
075    protected SaslRequest( String saslMechanism )
076    {
077        this.saslMechanism = saslMechanism;
078    }
079
080
081    /**
082     * Adds the given controls.
083     *
084     * @param controls the controls
085     */
086    public void addAllControls( Control[] controls )
087    {
088        this.controls.addAll( Arrays.asList( controls ) );
089    }
090
091
092    /**
093     * Adds the given control.
094     *
095     * @param control the control
096     */
097    public void addControl( Control control )
098    {
099        this.controls.add( control );
100    }
101
102
103    /**
104     * Gets the authorization ID.
105     *
106     * @return the authorization ID
107     */
108    public String getAuthorizationId()
109    {
110        return authorizationId;
111    }
112
113
114    /**
115     * Gets the controls.
116     *
117     * @return the controls
118     */
119    public Control[] getControls()
120    {
121        return controls.toArray( new Control[0] );
122    }
123
124
125    /**
126     * Gets the crendentials
127     *
128     * @return the credentials
129     */
130    public byte[] getCredentials()
131    {
132        if ( credentials != null )
133        {
134            return credentials;
135        }
136        else
137        {
138            return Strings.EMPTY_BYTES;
139        }
140    }
141
142
143    /**
144     * Gets the quality of protection.
145     *
146     * @return the quality of protection
147     */
148    public SaslQoP getQualityOfProtection()
149    {
150        return qualityOfProtection;
151    }
152
153
154    /**
155     * Gets realm name.
156     *
157     * @return the realm name
158     */
159    public String getRealmName()
160    {
161        return realmName;
162    }
163
164
165    /**
166     * Gets the SASL mechanism.
167     *
168     * @return the SASL mechanism
169     */
170    public String getSaslMechanism()
171    {
172        return saslMechanism;
173    }
174
175
176    /**
177     * Gets the security strength.
178     *
179     * @return the security strength
180     */
181    public SaslSecurityStrength getSecurityStrength()
182    {
183        return securityStrength;
184    }
185
186
187    /**
188     * Gets the username.
189     *
190     * @return the username
191     */
192    public String getUsername()
193    {
194        return username;
195    }
196
197
198    /**
199     * Indicates if mutual authentication is required.
200     *
201     * @return the flag indicating if mutual authentication is required
202     */
203    public boolean isMutualAuthentication()
204    {
205        return mutualAuthentication;
206    }
207
208
209    /**
210     * Sets the Authorization ID
211     *
212     * @param authorizationId The authorization ID
213     */
214    public void setAuthorizationId( String authorizationId )
215    {
216        this.authorizationId = authorizationId;
217    }
218
219
220    /**
221     * Sets the credentials.
222     *
223     * @param credentials the credentials
224     */
225    public void setCredentials( byte[] credentials )
226    {
227        this.credentials = credentials;
228    }
229
230
231    /**
232     * Sets the credentials.
233     *
234     * @param credentials the credentials
235     */
236    public void setCredentials( String credentials )
237    {
238        this.credentials = Strings.getBytesUtf8( credentials );
239    }
240
241
242    /**
243     * Sets the flag indicating if mutual authentication is required.
244     *
245     * @param mutualAuthentication the flag indicating if mutual authentication is required
246     */
247    public void setMutualAuthentication( boolean mutualAuthentication )
248    {
249        this.mutualAuthentication = mutualAuthentication;
250    }
251
252
253    /**
254     * Sets the quality of protection.
255     *
256     * @param qualityOfProtection the quality of protection
257     */
258    public void setQualityOfProtection( SaslQoP qualityOfProtection )
259    {
260        this.qualityOfProtection = qualityOfProtection;
261    }
262
263
264    /**
265     * Sets the realm name.
266     * 
267     * @param realmName The realm name
268     */
269    protected void setRealmName( String realmName )
270    {
271        this.realmName = realmName;
272    }
273
274
275    /**
276     * Sets the SASL mechanism
277     *
278     * @param saslMechanism the SASL mechanism
279     */
280    protected void setSaslMechanism( String saslMechanism )
281    {
282        this.saslMechanism = saslMechanism;
283    }
284
285
286    /**
287     * Sets the security strength.
288     *
289     * @param securityStrength the security strength
290     */
291    public void setSecurityStrength( SaslSecurityStrength securityStrength )
292    {
293        this.securityStrength = securityStrength;
294    }
295
296
297    /**
298     * Sets the username.
299     *
300     * @param username the username
301     */
302    public void setUsername( String username )
303    {
304        this.username = username;
305    }
306}