001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2016 ForgeRock AS.
016 */
017package org.opends.server.types;
018
019import org.forgerock.opendj.ldap.ByteString;
020import org.forgerock.opendj.ldap.DN;
021
022import static org.forgerock.util.Reject.*;
023
024/**
025 * This class defines a data structure that may be used to store
026 * information about an authenticated user.  Note that structures in
027 * this class allow for multiple authentication types for the same
028 * user, which is not currently supported by LDAP but may be offered
029 * through some type of extension.
030 */
031@org.opends.server.types.PublicAPI(
032     stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
033     mayInstantiate=true,
034     mayExtend=false,
035     mayInvoke=true)
036public final class AuthenticationInfo
037{
038
039  /** Indicates whether this connection is currently authenticated. */
040  private boolean isAuthenticated;
041
042  /** Indicates whether this connection is authenticated as a root user. */
043  private boolean isRoot;
044
045  /**
046   * Indicates whether the user's password must be changed before any other
047   * operation will be allowed.
048   */
049  private boolean mustChangePassword;
050
051  /** The entry of the user that is currently authenticated. */
052  private Entry authenticationEntry;
053
054  /**
055   * The entry of the user that will be used as the default authorization
056   * identity.
057   */
058  private Entry authorizationEntry;
059
060  /** The type of authentication performed on this connection. */
061  private AuthenticationType authenticationType;
062
063  /** The SASL mechanism used to authenticate. */
064  private String saslMechanism;
065
066  /** The bind DN used to authenticate using simple authentication. */
067  private DN simpleBindDN;
068
069  /**
070   * Creates a new set of authentication information to be used for
071   * unauthenticated clients.
072   */
073  public AuthenticationInfo()
074  {
075    isAuthenticated     = false;
076    isRoot              = false;
077    mustChangePassword  = false;
078    authenticationType  = null;
079    authenticationEntry = null;
080    authorizationEntry  = null;
081    simpleBindDN        = null;
082    saslMechanism       = null;
083  }
084
085
086
087  /**
088   * Creates a new set of authentication information to be used for
089   * clients that are authenticated internally.
090   *
091   * @param  authenticationEntry  The entry of the user that has
092   *                              authenticated, or {@code null} to
093   *                              indicate an unauthenticated user.
094   * @param  isRoot               Indicates whether the authenticated
095   *                              user is a root user.
096   */
097  public AuthenticationInfo(Entry authenticationEntry, boolean isRoot)
098  {
099    this.authenticationEntry = authenticationEntry;
100    this.isRoot              = isRoot;
101
102    isAuthenticated     = authenticationEntry != null;
103    mustChangePassword  = false;
104    simpleBindDN        = authenticationEntry != null ?
105        authenticationEntry.getName() : null;
106    authorizationEntry  = authenticationEntry;
107    saslMechanism       = null;
108    authenticationType  = AuthenticationType.INTERNAL;
109  }
110
111  /**
112   * Creates a new set of authentication information to be used for
113   * clients that have successfully performed simple authentication.
114   *
115   * @param  authenticationEntry  The entry of the user that has
116   *                              authenticated.  It must not be
117   *                              {@code null}.
118   * @param  simpleBindDN         The bind DN that was used to
119   *                              perform the simple authentication.
120   * @param  isRoot               Indicates whether the authenticated
121   */
122  public AuthenticationInfo(Entry authenticationEntry, DN simpleBindDN,
123      boolean isRoot)
124  {
125    ifNull(authenticationEntry);
126
127    this.authenticationEntry = authenticationEntry;
128    this.simpleBindDN        = simpleBindDN;
129    this.isRoot              = isRoot;
130
131    this.isAuthenticated     = true;
132    this.mustChangePassword  = false;
133    this.authorizationEntry  = authenticationEntry;
134    this.saslMechanism       = null;
135    this.authenticationType  = AuthenticationType.SIMPLE;
136  }
137
138
139
140  /**
141   * Creates a new set of authentication information to be used for
142   * clients that have authenticated using a SASL mechanism.
143   *
144   * @param  authenticationEntry  The entry of the user that has
145   *                              authenticated.  It must not be
146   *                              {@code null}.
147   * @param  saslMechanism        The SASL mechanism used to
148   *                              authenticate.  This must be provided
149   *                              in all-uppercase characters and must
150   *                              not be {@code null}.
151   * @param  isRoot               Indicates whether the authenticated
152   *                              user is a root user.
153   */
154  public AuthenticationInfo(Entry authenticationEntry,
155                            String saslMechanism,
156                            boolean isRoot)
157  {
158    ifNull(authenticationEntry, saslMechanism);
159
160    this.authenticationEntry = authenticationEntry;
161    this.isRoot              = isRoot;
162
163    this.isAuthenticated    = true;
164    this.mustChangePassword = false;
165    this.authorizationEntry = authenticationEntry;
166    this.simpleBindDN       = null;
167    this.authenticationType = AuthenticationType.SASL;
168    this.saslMechanism      = saslMechanism;
169  }
170
171
172
173  /**
174   * Creates a new set of authentication information to be used for
175   * clients that have authenticated using a SASL mechanism.
176   *
177   * @param  authenticationEntry  The entry of the user that has
178   *                              authenticated.  It must not be
179   *                              {@code null}.
180   * @param  authorizationEntry   The entry of the user that will be
181   *                              used as the default authorization
182   *                              identity, or {@code null} to
183   *                              indicate that the authorization
184   *                              identity should be the
185   *                              unauthenticated user.
186   * @param  saslMechanism        The SASL mechanism used to
187   *                              authenticate.  This must be provided
188   *                              in all-uppercase characters and must
189   *                              not be {@code null}.
190   * @param  saslCredentials      The SASL credentials used to
191   *                              authenticate.
192   *                              It must not be {@code null}.
193   * @param  isRoot               Indicates whether the authenticated
194   *                              user is a root user.
195   */
196  public AuthenticationInfo(Entry authenticationEntry,
197                            Entry authorizationEntry,
198                            String saslMechanism,
199                            ByteString saslCredentials,
200                            boolean isRoot)
201  {
202    ifNull(authenticationEntry, saslMechanism);
203
204    this.authenticationEntry = authenticationEntry;
205    this.authorizationEntry  = authorizationEntry;
206    this.isRoot              = isRoot;
207
208    this.isAuthenticated    = true;
209    this.mustChangePassword = false;
210    this.simpleBindDN       = null;
211    this.authenticationType = AuthenticationType.SASL;
212    this.saslMechanism      = saslMechanism;
213  }
214
215
216
217  /**
218   * Indicates whether this client has successfully authenticated to
219   * the server.
220   *
221   * @return  {@code true} if this client has successfully
222   *          authenticated to the server, or {@code false} if not.
223   */
224  public boolean isAuthenticated()
225  {
226    return isAuthenticated;
227  }
228
229
230
231  /**
232   * Indicates whether this client should be considered a root user.
233   *
234   * @return  {@code true} if this client should be considered a root
235   *          user, or {@code false} if not.
236   */
237  public boolean isRoot()
238  {
239    return isRoot;
240  }
241
242
243
244  /**
245   * Indicates whether the authenticated user must change his/her
246   * password before any other operation will be allowed.
247   *
248   * @return  {@code true} if the user must change his/her password
249   *          before any other operation will be allowed, or
250   *          {@code false} if not.
251   */
252  public boolean mustChangePassword()
253  {
254    return mustChangePassword;
255  }
256
257
258
259  /**
260   * Specifies whether the authenticated user must change his/her
261   * password before any other operation will be allowed.
262   *
263   * @param  mustChangePassword  Specifies whether the authenticated
264   *                             user must change his/her password
265   *                             before any other operation will be
266   *                             allowed.
267   */
268  public void setMustChangePassword(boolean mustChangePassword)
269  {
270    this.mustChangePassword = mustChangePassword;
271  }
272
273
274
275  /**
276   * Indicates whether this client has authenticated using the
277   * specified authentication type.
278   *
279   * @param  authenticationType  The authentication type for which to
280   *                             make the determination.
281   *
282   * @return  {@code true} if the client has authenticated using the
283   *          specified authentication type, or {@code false} if not.
284   */
285  public boolean hasAuthenticationType(AuthenticationType
286                                            authenticationType)
287  {
288    return this.authenticationType == authenticationType;
289  }
290
291
292
293  /**
294   * Retrieves the entry for the user as whom the client is
295   * authenticated.
296   *
297   * @return  The entry for the user as whom the client is
298   *          authenticated, or {@code null} if the client is
299   *          unauthenticated.
300   */
301  public Entry getAuthenticationEntry()
302  {
303    return authenticationEntry;
304  }
305
306
307
308  /**
309   * Retrieves the DN of the user as whom the client is authenticated.
310   *
311   * @return  The DN of the user as whom the client is authenticated,
312   *          or {@code null} if the client is unauthenticated.
313   */
314  public DN getAuthenticationDN()
315  {
316    if (authenticationEntry != null)
317    {
318      return authenticationEntry.getName();
319    }
320    return null;
321  }
322
323
324
325  /**
326   * Sets the DN of the user as whom the client is authenticated,
327   * does nothing if the client is unauthenticated.
328   *
329   * @param dn authentication identity DN.
330   */
331  public void setAuthenticationDN(DN dn)
332  {
333    if (authenticationEntry != null)
334    {
335      authenticationEntry.setDN(dn);
336    }
337  }
338
339
340
341  /**
342   * Retrieves the entry for the user that should be used as the
343   * default authorization identity.
344   *
345   * @return  The entry for the user that should be used as the
346   *          default authorization identity, or {@code null} if the
347   *          authorization identity should be the unauthenticated
348   *          user.
349   */
350  public Entry getAuthorizationEntry()
351  {
352    return authorizationEntry;
353  }
354
355
356
357  /**
358   * Retrieves the DN for the user that should be used as the default
359   * authorization identity.
360   *
361   * @return  The DN for the user that should be used as the default
362   *          authorization identity, or {@code null} if the
363   *          authorization identity should be the unauthenticated
364   *          user.
365   */
366  public DN getAuthorizationDN()
367  {
368    if (authorizationEntry != null)
369    {
370      return authorizationEntry.getName();
371    }
372    return null;
373  }
374
375
376
377  /**
378   * Sets the DN for the user that should be used as the default
379   * authorization identity, does nothing if the client is
380   * unauthorized.
381   *
382   * @param dn authorization identity DN.
383   */
384  public void setAuthorizationDN(DN dn)
385  {
386    if (authorizationEntry != null)
387    {
388      authorizationEntry.setDN(dn);
389    }
390  }
391
392
393
394  /**
395   * Retrieves the bind DN that the client used for simple
396   * authentication.
397   *
398   * @return  The bind DN that the client used for simple
399   *          authentication, or {@code null} if the client is not
400   *          authenticated using simple authentication.
401   */
402  public DN getSimpleBindDN()
403  {
404    return simpleBindDN;
405  }
406
407
408
409  /**
410   * Indicates whether the client is currently authenticated using the
411   * specified SASL mechanism.
412   *
413   * @param  saslMechanism  The SASL mechanism for which to make the
414   *                        determination.  Note that this must be
415   *                        provided in all uppercase characters.
416   *
417   * @return  {@code true} if the client is authenticated using the
418   *          specified SASL mechanism, or {@code false} if not.
419   */
420  public boolean hasSASLMechanism(String saslMechanism)
421  {
422    return this.saslMechanism.equals(saslMechanism);
423  }
424
425  /**
426   * Retrieves a string representation of this authentication info
427   * structure.
428   *
429   * @return  A string representation of this authentication info
430   *          structure.
431   */
432  @Override
433  public String toString()
434  {
435    StringBuilder buffer = new StringBuilder();
436    toString(buffer);
437
438    return buffer.toString();
439  }
440
441
442
443  /**
444   * Appends a string representation of this authentication info
445   * structure to the provided buffer.
446   *
447   * @param  buffer  The buffer to which the information is to be
448   *                 appended.
449   */
450  public void toString(StringBuilder buffer)
451  {
452    buffer.append("AuthenticationInfo(isAuthenticated=");
453    buffer.append(isAuthenticated);
454    buffer.append(",isRoot=");
455    buffer.append(isRoot);
456    buffer.append(",mustChangePassword=");
457    buffer.append(mustChangePassword);
458    buffer.append(",authenticationDN=\"");
459
460    if (authenticationEntry != null)
461    {
462      buffer.append(authenticationEntry.getName());
463    }
464
465    if (authorizationEntry == null)
466    {
467      buffer.append("\",authorizationDN=\"\"");
468    }
469    else
470    {
471      buffer.append("\",authorizationDN=\"");
472      buffer.append(authorizationEntry.getName());
473      buffer.append("\"");
474    }
475
476    if (authenticationType != null)
477    {
478      buffer.append(",authType=");
479      buffer.append(authenticationType);
480    }
481
482    if (saslMechanism != null)
483    {
484      buffer.append(",saslMechanism=");
485      buffer.append(saslMechanism);
486    }
487
488    buffer.append(")");
489  }
490
491
492
493  /**
494   * Creates a duplicate of this {@code AuthenticationInfo} object
495   * with the new authentication and authorization entries.
496   *
497   * @param  newAuthenticationEntry  The updated entry for the user
498   *                                 as whom the associated client
499   *                                 connection is authenticated.
500   * @param  newAuthorizationEntry   The updated entry for the default
501   *                                 authorization identity for the
502   *                                 associated client connection.
503   *
504   * @return  The duplicate of this {@code AuthenticationInfo} object
505   *          with the specified authentication and authorization
506   *          entries.
507   */
508  public AuthenticationInfo duplicate(Entry newAuthenticationEntry,
509                                      Entry newAuthorizationEntry)
510  {
511    AuthenticationInfo authInfo = new AuthenticationInfo();
512
513    authInfo.isAuthenticated     = isAuthenticated;
514    authInfo.isRoot              = isRoot;
515    authInfo.mustChangePassword  = mustChangePassword;
516    authInfo.authenticationEntry = newAuthenticationEntry;
517    authInfo.authorizationEntry  = newAuthorizationEntry;
518
519    authInfo.authenticationType  = authenticationType;
520    authInfo.saslMechanism       = saslMechanism;
521
522    return authInfo;
523  }
524}
525