001/*
002 * Copyright 2013-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2013-2024 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) 2013-2024 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 a request control that can be included in a modify
057 * request or a password modify extended request in order to indicate that if
058 * the operation results in changing the password for a user, the user's former
059 * password should be purged from the entry rather than retired, and any
060 * existing retired password should also be purged.
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 control has an OID of "1.3.6.1.4.1.30221.2.5.32" and does not have a
073 * value.  The criticality may be either true (in which case the operation will
074 * succeed only if the user's password policy allows passwords to be retired by
075 * a request control) or false (in which case if the password policy does not
076 * allow the use of this control, the operation will be processed as if the
077 * control had not been included in the request).
078 * <BR><BR>
079 * <H2>Example</H2>
080 * The following example demonstrates the use of the purge password request
081 * control to request that a user's current password be purged in the course of
082 * a password change.
083 * <PRE>
084 * Control[] requestControls =
085 * {
086 *   new PurgePasswordRequestControl(true)
087 * };
088 *
089 * PasswordModifyExtendedRequest passwordModifyRequest =
090 *      new PasswordModifyExtendedRequest(
091 *           "uid=test.user,ou=People,dc=example,dc=com", // The user to update
092 *           null, // The current password -- we don't know it.
093 *           "newPassword", // The new password to assign to the user.
094 *           requestControls); // The controls to include in the request.
095 * PasswordModifyExtendedResult passwordModifyResult =
096 *      (PasswordModifyExtendedResult)
097 *      connection.processExtendedOperation(passwordModifyRequest);
098 * </PRE>
099 *
100 * @see  RetirePasswordRequestControl
101 */
102@NotMutable()
103@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
104public final class PurgePasswordRequestControl
105       extends Control
106{
107  /**
108   * The OID (1.3.6.1.4.1.30221.2.5.32) for the purge password request control.
109   */
110  @NotNull public static final  String PURGE_PASSWORD_REQUEST_OID =
111       "1.3.6.1.4.1.30221.2.5.32";
112
113
114
115  /**
116   * The serial version UID for this serializable class.
117   */
118  private static final long serialVersionUID = -3756801088881565921L;
119
120
121
122  /**
123   * Creates a new retire password request control with the specified
124   * criticality.
125   *
126   * @param  isCritical  Indicates whether the control should be considered
127   *                     critical.
128   */
129  public PurgePasswordRequestControl(final boolean isCritical)
130  {
131    super(PURGE_PASSWORD_REQUEST_OID, isCritical, null);
132  }
133
134
135
136  /**
137   * Creates a new retire password request control which is decoded from the
138   * provided generic control.
139   *
140   * @param  control  The generic control to be decoded as a retire password
141   *                  request control.
142   *
143   * @throws LDAPException  If the provided control cannot be decoded as a
144   *                         retire password request control.
145   */
146  public PurgePasswordRequestControl(@NotNull final Control control)
147       throws LDAPException
148  {
149    super(control);
150
151    if (control.hasValue())
152    {
153      throw new LDAPException(ResultCode.DECODING_ERROR,
154           ERR_PURGE_PASSWORD_REQUEST_CONTROL_HAS_VALUE.get());
155    }
156  }
157
158
159
160  /**
161   * {@inheritDoc}
162   */
163  @Override()
164  @NotNull()
165  public String getControlName()
166  {
167    return INFO_CONTROL_NAME_PURGE_PASSWORD_REQUEST.get();
168  }
169
170
171
172  /**
173   * Retrieves a representation of this purge password request control as a JSON
174   * object.  The JSON object uses the following fields (note that since this
175   * control does not have a value, neither the {@code value-base64} nor
176   * {@code value-json} fields may be present):
177   * <UL>
178   *   <LI>
179   *     {@code oid} -- A mandatory string field whose value is the object
180   *     identifier for this control.  For the purge password request control,
181   *     the OID is "1.3.6.1.4.1.30221.2.5.32".
182   *   </LI>
183   *   <LI>
184   *     {@code control-name} -- An optional string field whose value is a
185   *     human-readable name for this control.  This field is only intended for
186   *     descriptive purposes, and when decoding a control, the {@code oid}
187   *     field should be used to identify the type of control.
188   *   </LI>
189   *   <LI>
190   *     {@code criticality} -- A mandatory Boolean field used to indicate
191   *     whether this control is considered critical.
192   *   </LI>
193   * </UL>
194   *
195   * @return  A JSON object that contains a representation of this control.
196   */
197  @Override()
198  @NotNull()
199  public JSONObject toJSONControl()
200  {
201    return new JSONObject(
202         new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID,
203              PURGE_PASSWORD_REQUEST_OID),
204         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME,
205              INFO_CONTROL_NAME_PURGE_PASSWORD_REQUEST.get()),
206         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY,
207              isCritical()));
208  }
209
210
211
212  /**
213   * Attempts to decode the provided object as a JSON representation of a
214   * purge password request control.
215   *
216   * @param  controlObject  The JSON object to be decoded.  It must not be
217   *                        {@code null}.
218   * @param  strict         Indicates whether to use strict mode when decoding
219   *                        the provided JSON object.  If this is {@code true},
220   *                        then this method will throw an exception if the
221   *                        provided JSON object contains any unrecognized
222   *                        fields.  If this is {@code false}, then unrecognized
223   *                        fields will be ignored.
224   *
225   * @return  The purge password request control that was decoded from
226   *          the provided JSON object.
227   *
228   * @throws  LDAPException  If the provided JSON object cannot be parsed as a
229   *                         valid purge password request control.
230   */
231  @NotNull()
232  public static PurgePasswordRequestControl decodeJSONControl(
233              @NotNull final JSONObject controlObject,
234              final boolean strict)
235         throws LDAPException
236  {
237    final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper(
238         controlObject, strict, false, false);
239
240    return new PurgePasswordRequestControl(jsonControl.getCriticality());
241  }
242
243
244
245  /**
246   * {@inheritDoc}
247   */
248  @Override()
249  public void toString(@NotNull final StringBuilder buffer)
250  {
251    buffer.append("PurgePasswordRequestControl(isCritical=");
252    buffer.append(isCritical());
253    buffer.append(')');
254  }
255}