001/*
002 * Copyright 2015-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2015-2020 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2015-2020 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk.unboundidds.extensions;
037
038
039
040import java.io.Serializable;
041import java.util.StringTokenizer;
042
043import com.unboundid.ldap.sdk.LDAPException;
044import com.unboundid.ldap.sdk.ResultCode;
045import com.unboundid.util.Debug;
046import com.unboundid.util.NotMutable;
047import com.unboundid.util.StaticUtils;
048import com.unboundid.util.ThreadSafety;
049import com.unboundid.util.ThreadSafetyLevel;
050import com.unboundid.util.Validator;
051
052import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
053
054
055
056/**
057 * This class defines a data structure that will provide information about
058 * warnings that may affect an account's usability.  It includes a number of
059 * predefined warning types, but also allows for the possibility of additional
060 * warning types that have not been defined.
061 * <BR>
062 * <BLOCKQUOTE>
063 *   <B>NOTE:</B>  This class, and other classes within the
064 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
065 *   supported for use against Ping Identity, UnboundID, and
066 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
067 *   for proprietary functionality or for external specifications that are not
068 *   considered stable or mature enough to be guaranteed to work in an
069 *   interoperable way with other types of LDAP servers.
070 * </BLOCKQUOTE>
071 */
072@NotMutable()
073@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
074public final class PasswordPolicyStateAccountUsabilityWarning
075       implements Serializable
076{
077  /**
078   * The numeric value for the warning type that indicates the user's account is
079   * about to expire.
080   */
081  public static final int WARNING_TYPE_ACCOUNT_EXPIRING = 1;
082
083
084
085  /**
086   * The name for the warning type that indicates the user's account is about
087   * to expire.
088   */
089  public static final String WARNING_NAME_ACCOUNT_EXPIRING = "account-expiring";
090
091
092
093  /**
094   * The numeric value for the warning type that indicates the user's password
095   * is about to expire.
096   */
097  public static final int WARNING_TYPE_PASSWORD_EXPIRING = 2;
098
099
100
101  /**
102   * The name for the warning type that indicates the user's password is about
103   * to expire.
104   */
105  public static final String WARNING_NAME_PASSWORD_EXPIRING =
106       "password-expiring";
107
108
109
110  /**
111   * The numeric value for the warning type that indicates the user has one or
112   * more failed authentication attempts since the last successful bind, and
113   * that the account may be locked if there are too many more failures.
114   */
115  public static final int WARNING_TYPE_OUTSTANDING_BIND_FAILURES = 3;
116
117
118
119  /**
120   * The name for the warning type that indicates the user has one or more
121   * failed authentication attempts since the last successful bind, and that the
122   * account may be locked if there are too many more failures.
123   */
124  public static final String WARNING_NAME_OUTSTANDING_BIND_FAILURES =
125       "outstanding-bind-failures";
126
127
128
129  /**
130   * The numeric value for the warning type that indicates the user has not
131   * authenticated in some time, and the account may be locked in the near
132   * future if it remains idle.
133   */
134  public static final int WARNING_TYPE_ACCOUNT_IDLE = 4;
135
136
137
138  /**
139   * The name for the warning type that indicates the user has not authenticated
140   * in some time, and the account may be locked in the near future if it
141   * remains idle.
142   */
143  public static final String WARNING_NAME_ACCOUNT_IDLE = "account-idle";
144
145
146
147  /**
148   * The numeric value for the warning type that indicates the user will be
149   * required to change his/her password by a specific time because the password
150   * policy requires all users to change their passwords by that time.
151   */
152  public static final int WARNING_TYPE_REQUIRE_PASSWORD_CHANGE_BY_TIME = 5;
153
154
155
156  /**
157   * The name for the warning type that indicates the user user will be required
158   * to change his/her password by a specific time because the password policy
159   * requires all users to change their passwords by that time.
160   */
161  public static final String WARNING_NAME_REQUIRE_PASSWORD_CHANGE_BY_TIME =
162       "require-password-change-by-time";
163
164
165
166  /**
167   * The serial version UID for this serializable class.
168   */
169  private static final long serialVersionUID = 4256291819633130578L;
170
171
172
173  // The integer value for this account usability warning.
174  private final int intValue;
175
176  // A human-readable message that provides specific details about this account
177  // usability warning.
178  private final String message;
179
180  // The name for this account usability warning.
181  private final String name;
182
183  // The encoded string representation for this account usability warning.
184  private final String stringRepresentation;
185
186
187
188  /**
189   * Creates a new account usability warning with the provided information.
190   *
191   * @param  intValue  The integer value for this account usability warning.
192   * @param  name      The name for this account usability warning.  It must not
193   *                   be {@code null}.
194   * @param  message   A human-readable message that provides specific details
195   *                   about this account usability warning.  It may be
196   *                   {@code null} if no message is available.
197   */
198  public PasswordPolicyStateAccountUsabilityWarning(final int intValue,
199                                                    final String name,
200                                                    final String message)
201  {
202    Validator.ensureNotNull(name);
203
204    this.intValue = intValue;
205    this.name = name;
206    this.message = message;
207
208    final StringBuilder buffer = new StringBuilder();
209    buffer.append("code=");
210    buffer.append(intValue);
211    buffer.append("\tname=");
212    buffer.append(name);
213
214    if (message != null)
215    {
216      buffer.append("\tmessage=");
217      buffer.append(message);
218    }
219
220    stringRepresentation = buffer.toString();
221  }
222
223
224
225  /**
226   * Creates a new account usability warning that is decoded from the provided
227   * string representation.
228   *
229   * @param  stringRepresentation  The string representation of the account
230   *                               usability warning to decode.  It must not be
231   *                               {@code null}.
232   *
233   * @throws  LDAPException  If the provided string cannot be decoded as a valid
234   *                         account usability warning.
235   */
236  public PasswordPolicyStateAccountUsabilityWarning(
237              final String stringRepresentation)
238         throws LDAPException
239  {
240    this.stringRepresentation = stringRepresentation;
241
242    try
243    {
244      Integer i = null;
245      String  n = null;
246      String  m = null;
247
248      final StringTokenizer tokenizer =
249           new StringTokenizer(stringRepresentation, "\t");
250      while (tokenizer.hasMoreTokens())
251      {
252        final String token = tokenizer.nextToken();
253        final int equalPos = token.indexOf('=');
254        final String fieldName = token.substring(0, equalPos);
255        final String fieldValue = token.substring(equalPos+1);
256        if (fieldName.equals("code"))
257        {
258          i = Integer.valueOf(fieldValue);
259        }
260        else if (fieldName.equals("name"))
261        {
262          n = fieldValue;
263        }
264        else if (fieldName.equals("message"))
265        {
266          m = fieldValue;
267        }
268      }
269
270      if (i == null)
271      {
272        throw new LDAPException(ResultCode.DECODING_ERROR,
273             ERR_PWP_STATE_ACCOUNT_USABILITY_WARNING_CANNOT_DECODE.get(
274                  stringRepresentation,
275                  ERR_PWP_STATE_ACCOUNT_USABILITY_WARNING_NO_CODE.get()));
276      }
277
278      if (n == null)
279      {
280        throw new LDAPException(ResultCode.DECODING_ERROR,
281             ERR_PWP_STATE_ACCOUNT_USABILITY_WARNING_CANNOT_DECODE.get(
282                  stringRepresentation,
283                  ERR_PWP_STATE_ACCOUNT_USABILITY_WARNING_NO_NAME.get()));
284      }
285
286      intValue = i;
287      name     = n;
288      message  = m;
289    }
290    catch (final LDAPException le)
291    {
292      Debug.debugException(le);
293
294      throw le;
295    }
296    catch (final Exception e)
297    {
298      Debug.debugException(e);
299
300      throw new LDAPException(ResultCode.DECODING_ERROR,
301           ERR_PWP_STATE_ACCOUNT_USABILITY_WARNING_CANNOT_DECODE.get(
302                stringRepresentation, StaticUtils.getExceptionMessage(e)),
303           e);
304    }
305  }
306
307
308
309  /**
310   * Retrieves the integer value for this account usability warning.
311   *
312   * @return  The integer value for this account usability warning.
313   */
314  public int getIntValue()
315  {
316    return intValue;
317  }
318
319
320
321  /**
322   * Retrieves the name for this account usability warning.
323   *
324   * @return  The name for this account usability warning.
325   */
326  public String getName()
327  {
328    return name;
329  }
330
331
332
333  /**
334   * Retrieves a human-readable message that provides specific details about
335   * this account usability warning.
336   *
337   * @return  A human-readable message that provides specific details about this
338   *          account usability warning, or {@code null} if no message is
339   *          available.
340   */
341  public String getMessage()
342  {
343    return message;
344  }
345
346
347
348  /**
349   * Retrieves a string representation of this account usability warning.
350   *
351   * @return  A string representation of this account usability warning.
352   */
353  @Override()
354  public String toString()
355  {
356    return stringRepresentation;
357  }
358}