001/*
002 * Copyright 2007-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2007-2022 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) 2007-2022 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.controls;
037
038
039
040import com.unboundid.ldap.sdk.Control;
041import com.unboundid.ldap.sdk.JSONControlDecodeHelper;
042import com.unboundid.ldap.sdk.LDAPException;
043import com.unboundid.ldap.sdk.ResultCode;
044import com.unboundid.util.NotMutable;
045import com.unboundid.util.NotNull;
046import com.unboundid.util.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048import com.unboundid.util.json.JSONField;
049import com.unboundid.util.json.JSONObject;
050
051import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
052
053
054
055/**
056 * This class provides an implementation of the password policy request control
057 * as described in draft-behera-ldap-password-policy.  It may be used to request
058 * information related to a user's password policy.  In the Ping Identity,
059 * UnboundID, and Nokia/Alcatel-Lucent 8661 Directory Server, this control may
060 * be included with add, bind, compare, modify, and password modify requests.
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 * <BR>
072 * This request control has an OID of 1.3.6.1.4.1.42.2.27.8.5.1.  The
073 * criticality may be either true or false.  It does not have a value.
074 * <BR><BR>
075 * The corresponding {@link PasswordPolicyResponseControl} may include at most
076 * one warning from the set of {@link PasswordPolicyWarningType} values and at
077 * most one error from the set of {@link PasswordPolicyErrorType} values.  See
078 * the documentation for those classes for more information on the information
079 * that may be included.
080 * <BR><BR>
081 * <H2>Example</H2>
082 * The following example demonstrates the use of the password policy request
083 * control in conjunction with a bind operation:
084 * <PRE>
085 * SimpleBindRequest bindRequest = new SimpleBindRequest(
086 *      "uid=john.doe,ou=People,dc=example,dc=com", "password",
087 *      new PasswordPolicyRequestControl());
088 *
089 * BindResult bindResult;
090 * try
091 * {
092 *   bindResult = connection.bind(bindRequest);
093 * }
094 * catch (LDAPException le)
095 * {
096 *   // The bind failed.  There may be a password policy response control to
097 *   // help tell us why.
098 *   bindResult = new BindResult(le.toLDAPResult());
099 * }
100 *
101 * PasswordPolicyResponseControl pwpResponse =
102 *      PasswordPolicyResponseControl.get(bindResult);
103 * if (pwpResponse != null)
104 * {
105 *   PasswordPolicyErrorType errorType = pwpResponse.getErrorType();
106 *   if (errorType != null)
107 *   {
108 *     // There was a password policy-related error.
109 *   }
110 *
111 *   PasswordPolicyWarningType warningType = pwpResponse.getWarningType();
112 *   if (warningType != null)
113 *   {
114 *     // There was a password policy-related warning.
115 *     int value = pwpResponse.getWarningValue();
116 *     switch (warningType)
117 *     {
118 *       case TIME_BEFORE_EXPIRATION:
119 *         // The warning value is the number of seconds until the user's
120 *         // password expires.
121 *         break;
122 *       case GRACE_LOGINS_REMAINING:
123 *         // The warning value is the number of grace logins remaining for
124 *         // the user.
125 *     }
126 *   }
127 * }
128 * </PRE>
129 */
130@NotMutable()
131@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
132public final class PasswordPolicyRequestControl
133       extends Control
134{
135  /**
136   * The OID (1.3.6.1.4.1.42.2.27.8.5.1) for the password policy request
137   * control.
138   */
139  @NotNull public static final String PASSWORD_POLICY_REQUEST_OID =
140       "1.3.6.1.4.1.42.2.27.8.5.1";
141
142
143
144  /**
145   * The serial version UID for this serializable class.
146   */
147  private static final long serialVersionUID = 6495056761590890150L;
148
149
150
151  /**
152   * Creates a new password policy request control.  The control will not be
153   * marked critical.
154   */
155  public PasswordPolicyRequestControl()
156  {
157    super(PASSWORD_POLICY_REQUEST_OID, false, null);
158  }
159
160
161
162  /**
163   * Creates a new password policy request control.
164   *
165   * @param  isCritical  Indicates whether the control should be marked
166   * critical.
167   */
168  public PasswordPolicyRequestControl(final boolean isCritical)
169  {
170    super(PASSWORD_POLICY_REQUEST_OID, isCritical, null);
171  }
172
173
174
175  /**
176   * Creates a new password policy request control which is decoded from the
177   * provided generic control.
178   *
179   * @param  control  The generic control to be decoded as a password policy
180   *                  request control.
181   *
182   * @throws  LDAPException  If the provided control cannot be decoded as a
183   *                         password policy request control.
184   */
185  public PasswordPolicyRequestControl(@NotNull final Control control)
186         throws LDAPException
187  {
188    super(control);
189
190    if (control.hasValue())
191    {
192      throw new LDAPException(ResultCode.DECODING_ERROR,
193                              ERR_PWP_REQUEST_HAS_VALUE.get());
194    }
195  }
196
197
198
199  /**
200   * {@inheritDoc}
201   */
202  @Override()
203  @NotNull()
204  public String getControlName()
205  {
206    return INFO_CONTROL_NAME_PW_POLICY_REQUEST.get();
207  }
208
209
210
211  /**
212   * Retrieves a representation of this password policy request control as a
213   * JSON object.  The JSON object uses the following fields (note that since
214   * this control does not have a value, neither the {@code value-base64} nor
215   * {@code value-json} fields may be present):
216   * <UL>
217   *   <LI>
218   *     {@code oid} -- A mandatory string field whose value is the object
219   *     identifier for this control.  For the password policy request control,
220   *     the OID is "1.3.6.1.4.1.42.2.27.8.5.1".
221   *   </LI>
222   *   <LI>
223   *     {@code control-name} -- An optional string field whose value is a
224   *     human-readable name for this control.  This field is only intended for
225   *     descriptive purposes, and when decoding a control, the {@code oid}
226   *     field should be used to identify the type of control.
227   *   </LI>
228   *   <LI>
229   *     {@code criticality} -- A mandatory Boolean field used to indicate
230   *     whether this control is considered critical.
231   *   </LI>
232   * </UL>
233   *
234   * @return  A JSON object that contains a representation of this control.
235   */
236  @Override()
237  @NotNull()
238  public JSONObject toJSONControl()
239  {
240    return new JSONObject(
241         new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID,
242              PASSWORD_POLICY_REQUEST_OID),
243         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME,
244              INFO_CONTROL_NAME_PW_POLICY_REQUEST.get()),
245         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY,
246              isCritical()));
247  }
248
249
250
251  /**
252   * Attempts to decode the provided object as a JSON representation of a
253   * password policy request control.
254   *
255   * @param  controlObject  The JSON object to be decoded.  It must not be
256   *                        {@code null}.
257   * @param  strict         Indicates whether to use strict mode when decoding
258   *                        the provided JSON object.  If this is {@code true},
259   *                        then this method will throw an exception if the
260   *                        provided JSON object contains any unrecognized
261   *                        fields.  If this is {@code false}, then unrecognized
262   *                        fields will be ignored.
263   *
264   * @return  The password policy request control that was decoded from the
265   *          provided JSON object.
266   *
267   * @throws  LDAPException  If the provided JSON object cannot be parsed as a
268   *                         valid password policy request control.
269   */
270  @NotNull()
271  public static PasswordPolicyRequestControl decodeJSONControl(
272              @NotNull final JSONObject controlObject,
273              final boolean strict)
274         throws LDAPException
275  {
276    final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper(
277         controlObject, strict, false, false);
278
279    return new PasswordPolicyRequestControl(
280         jsonControl.getCriticality());
281  }
282
283
284
285  /**
286   * {@inheritDoc}
287   */
288  @Override()
289  public void toString(@NotNull final StringBuilder buffer)
290  {
291    buffer.append("PasswordPolicyRequestControl(isCritical=");
292    buffer.append(isCritical());
293    buffer.append(')');
294  }
295}