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