001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.controls;
028
029import static org.opends.messages.ProtocolMessages.*;
030
031import java.util.HashMap;
032import java.util.Map;
033
034import org.forgerock.i18n.LocalizableMessage;
035
036/**
037 * This enumeration defines the set of password policy warnings that may be
038 * included in the password policy response control defined in
039 * draft-behera-ldap-password-policy.
040 */
041public enum PasswordPolicyWarningType
042{
043  /**
044   * The warning type that will be used to indicate that the password will
045   * expire in the near future and to provide the length of time in seconds
046   * until expiration.
047   */
048  TIME_BEFORE_EXPIRATION((byte) 0x80,
049                     INFO_PWPWARNTYPE_DESCRIPTION_TIME_BEFORE_EXPIRATION.get()),
050
051
052
053  /**
054   * The warning type that will be used to indicate that the user is
055   * authenticating using a grace login and to provide the number of grace
056   * logins that the user has left.
057   */
058  GRACE_LOGINS_REMAINING((byte) 0x81,
059                     INFO_PWPWARNTYPE_DESCRIPTION_GRACE_LOGINS_REMAINING.get());
060
061
062
063  /** A lookup table for resolving a warning type from its BER type. */
064  private static final Map<Byte, PasswordPolicyWarningType> TABLE = new HashMap<>();
065  static
066  {
067    for (PasswordPolicyWarningType value : PasswordPolicyWarningType.values())
068    {
069      TABLE.put(value.type, value);
070      TABLE.put(value.type, value);
071    }
072  }
073
074
075
076  /**
077   * The BER type to use for the associated element in the password policy
078   * control.
079   */
080  private final byte type;
081
082  /** The message ID for the description of this password policy error type. */
083  private final LocalizableMessage description;
084
085
086
087  /**
088   * Creates a new instance of a password policy warning type with the provided
089   * BER type.
090   *
091   * @param  type           The BER type to use for the associated element in
092   *                        the password policy control.
093   * @param  description    The message for the description of this password
094   *                        policy error type.
095   */
096  private PasswordPolicyWarningType(byte type, LocalizableMessage description)
097  {
098    this.type          = type;
099    this.description   = description;
100  }
101
102
103
104  /**
105   * Retrieves the BER type to use for the associated element in the password
106   * policy control.
107   *
108   * @return  The BER type to use for the associated element in the password
109   *          policy control.
110   */
111  public byte getType()
112  {
113    return type;
114  }
115
116
117
118  /**
119   * Retrieves the password policy warning type for the provided BER type.
120   *
121   * @param  type  The BER type for which to retrieve the corresponding password
122   *               policy warning type.
123   *
124   * @return  The requested password policy warning type, or <CODE>null</CODE>
125   *          if none of the defined warning types have the provided BER type.
126   */
127  public static PasswordPolicyWarningType valueOf(byte type)
128  {
129    return TABLE.get(Byte.valueOf(type));
130  }
131
132
133
134  /**
135   * Retrieves a string representation of this password policy warning type.
136   *
137   * @return  A string representation of this password policy warning type.
138   */
139  @Override
140  public String toString()
141  {
142    return description == null ? null : description.toString();
143  }
144}
145