View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  
21  package org.apache.directory.ldap.client.api;
22  
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.apache.directory.api.ldap.model.constants.SaslQoP;
29  import org.apache.directory.api.ldap.model.constants.SaslSecurityStrength;
30  import org.apache.directory.api.ldap.model.message.Control;
31  import org.apache.directory.api.util.StringConstants;
32  import org.apache.directory.api.util.Strings;
33  
34  
35  /**
36   * Holds the data required to complete the SASL operation
37   * 
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public abstract class SaslRequest
41  {
42      /** The mechanism used to decode user identity */
43      protected String saslMechanism;
44  
45      /** The list of controls */
46      protected List<Control> controls = new ArrayList<Control>();
47  
48      /** The username */
49      protected String username;
50  
51      /** The credentials */
52      protected byte[] credentials;
53  
54      /** The realm name on the server */
55      protected String realmName;
56  
57      /** The authorization ID of the entity */
58      protected String authorizationId;
59  
60      /** The quality of protection */
61      protected SaslQoP qualityOfProtection;
62  
63      /** The security strength */
64      protected SaslSecurityStrength securityStrength;
65  
66      /** Require mutual authentication */
67      protected boolean mutualAuthentication = false;
68  
69  
70      /**
71       * Creates a new instance of SaslRequest.
72       *
73       * @param saslMechanism
74       *      the SASL mechanism
75       */
76      protected SaslRequest( String saslMechanism )
77      {
78          this.saslMechanism = saslMechanism;
79      }
80  
81  
82      /**
83       * Adds the given controls.
84       *
85       * @param controls the controls
86       */
87      public void addAllControls( Control[] controls )
88      {
89          this.controls.addAll( Arrays.asList( controls ) );
90      }
91  
92  
93      /**
94       * Adds the given control.
95       *
96       * @param control the control
97       */
98      public void addControl( Control control )
99      {
100         this.controls.add( control );
101     }
102 
103 
104     /**
105      * Gets the authorization ID.
106      *
107      * @return the authorization ID
108      */
109     public String getAuthorizationId()
110     {
111         return authorizationId;
112     }
113 
114 
115     /**
116      * Gets the controls.
117      *
118      * @return the controls
119      */
120     public Control[] getControls()
121     {
122         return controls.toArray( new Control[0] );
123     }
124 
125 
126     /**
127      * Gets the crendentials
128      *
129      * @return the credentials
130      */
131     public byte[] getCredentials()
132     {
133         if ( credentials != null )
134         {
135             return credentials;
136         }
137         else
138         {
139             return StringConstants.EMPTY_BYTES;
140         }
141     }
142 
143 
144     /**
145      * Gets the quality of protection.
146      *
147      * @return the quality of protection
148      */
149     public SaslQoP getQualityOfProtection()
150     {
151         return qualityOfProtection;
152     }
153 
154 
155     /**
156      * Gets realm name.
157      *
158      * @return the realm name
159      */
160     public String getRealmName()
161     {
162         return realmName;
163     }
164 
165 
166     /**
167      * Gets the SASL mechanism.
168      *
169      * @return the SASL mechanism
170      */
171     public String getSaslMechanism()
172     {
173         return saslMechanism;
174     }
175 
176 
177     /**
178      * Gets the security strength.
179      *
180      * @return the security strength
181      */
182     public SaslSecurityStrength getSecurityStrength()
183     {
184         return securityStrength;
185     }
186 
187 
188     /**
189      * Gets the username.
190      *
191      * @return the username
192      */
193     public String getUsername()
194     {
195         return username;
196     }
197 
198 
199     /**
200      * Indicates if mutual authentication is required.
201      *
202      * @return the flag indicating if mutual authentication is required
203      */
204     public boolean isMutualAuthentication()
205     {
206         return mutualAuthentication;
207     }
208 
209 
210     /**
211      * Sets the Authorization ID
212      *
213      * @param authorizationId The authorization ID
214      */
215     public void setAuthorizationId( String authorizationId )
216     {
217         this.authorizationId = authorizationId;
218     }
219 
220 
221     /**
222      * Sets the credentials.
223      *
224      * @param credentials the credentials
225      */
226     public void setCredentials( byte[] credentials )
227     {
228         this.credentials = credentials;
229     }
230 
231 
232     /**
233      * Sets the credentials.
234      *
235      * @param credentials the credentials
236      */
237     public void setCredentials( String credentials )
238     {
239         this.credentials = Strings.getBytesUtf8( credentials );
240     }
241 
242 
243     /**
244      * Sets the flag indicating if mutual authentication is required.
245      *
246      * @param mutualAuthentication the flag indicating if mutual authentication is required
247      */
248     public void setMutualAuthentication( boolean mutualAuthentication )
249     {
250         this.mutualAuthentication = mutualAuthentication;
251     }
252 
253 
254     /**
255      * Sets the quality of protection.
256      *
257      * @param qualityOfProtection the quality of protection
258      */
259     public void setQualityOfProtection( SaslQoP qualityOfProtection )
260     {
261         this.qualityOfProtection = qualityOfProtection;
262     }
263 
264 
265     /**
266      * Sets the realm name.
267      * 
268      * @param realmName The realm name
269      */
270     protected void setRealmName( String realmName )
271     {
272         this.realmName = realmName;
273     }
274 
275 
276     /**
277      * Sets the SASL mechanism
278      *
279      * @param saslMechanism the SASL mechanism
280      */
281     protected void setSaslMechanism( String saslMechanism )
282     {
283         this.saslMechanism = saslMechanism;
284     }
285 
286 
287     /**
288      * Sets the security strength.
289      *
290      * @param securityStrength the security strength
291      */
292     public void setSecurityStrength( SaslSecurityStrength securityStrength )
293     {
294         this.securityStrength = securityStrength;
295     }
296 
297 
298     /**
299      * Sets the username.
300      *
301      * @param username the username
302      */
303     public void setUsername( String username )
304     {
305         this.username = username;
306     }
307 }