001/*
002 * Copyright 2007-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2007-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) 2007-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.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.controls.ControlMessages.*;
052
053
054
055/**
056 * This class provides an implementation of the authorization identity bind
057 * request control as described in
058 * <A HREF="http://www.ietf.org/rfc/rfc3829.txt">RFC 3829</A>.  It may be
059 * included in a bind request to request that the server include the
060 * authorization identity associated with the client connection in the bind
061 * response message, in the form of an
062 * {@link AuthorizationIdentityResponseControl}.
063 * <BR><BR>
064 * The authorization identity request control is similar to the "Who Am I?"
065 * extended request as implemented in the
066 * {@link com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest} class.  The
067 * primary difference between them is that the "Who Am I?" extended request can
068 * be used at any time but requires a separate operation, while the
069 * authorization identity request control can be included only with a bind
070 * request but does not require a separate operation.
071 * <BR><BR>
072 * <H2>Example</H2>
073 * The following example demonstrates the use of the authorization identity
074 * request and response controls.  It authenticates to the directory server and
075 * attempts to retrieve the authorization identity from the response:
076 * <PRE>
077 * String authzID = null;
078 * BindRequest bindRequest =
079 *      new SimpleBindRequest("uid=test.user,ou=People,dc=example,dc=com",
080 *           "password", new AuthorizationIdentityRequestControl());
081 *
082 * BindResult bindResult = connection.bind(bindRequest);
083 * AuthorizationIdentityResponseControl authzIdentityResponse =
084 *      AuthorizationIdentityResponseControl.get(bindResult);
085 * if (authzIdentityResponse != null)
086 * {
087 *   authzID = authzIdentityResponse.getAuthorizationID();
088 * }
089 * </PRE>
090 */
091@NotMutable()
092@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
093public final class AuthorizationIdentityRequestControl
094       extends Control
095{
096  /**
097   * The OID (2.16.840.1.113730.3.4.16) for the authorization identity request
098   * control.
099   */
100  @NotNull public static final String AUTHORIZATION_IDENTITY_REQUEST_OID =
101       "2.16.840.1.113730.3.4.16";
102
103
104
105  /**
106   * The serial version UID for this serializable class.
107   */
108  private static final long serialVersionUID = -4059607155175828138L;
109
110
111
112  /**
113   * Creates a new authorization identity request control.  The control will not
114   * be marked critical.
115   */
116  public AuthorizationIdentityRequestControl()
117  {
118    super(AUTHORIZATION_IDENTITY_REQUEST_OID, false, null);
119  }
120
121
122
123  /**
124   * Creates a new authorization identity request control.
125   *
126   * @param  isCritical  Indicates whether the control should be marked
127   *                     critical.
128   */
129  public AuthorizationIdentityRequestControl(final boolean isCritical)
130  {
131    super(AUTHORIZATION_IDENTITY_REQUEST_OID, isCritical, null);
132  }
133
134
135
136  /**
137   * Creates a new authorization identity request control which is decoded from
138   * the provided generic control.
139   *
140   * @param  control  The generic control to be decoded as an authorization
141   *                  identity request control.
142   *
143   * @throws  LDAPException  If the provided control cannot be decoded as an
144   *                         authorization identity request control.
145   */
146  public AuthorizationIdentityRequestControl(@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_AUTHZID_REQUEST_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_AUTHZID_REQUEST.get();
168  }
169
170
171
172  /**
173   * Retrieves a representation of this authorization identity request control
174   * as a JSON object.  The JSON object uses the following fields (note that
175   * since this control does not have a value, neither the {@code value-base64}
176   * nor {@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 authorization identity request
181   *     control, the OID is "2.16.840.1.113730.3.4.16".
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              AUTHORIZATION_IDENTITY_REQUEST_OID),
204         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME,
205              INFO_CONTROL_NAME_AUTHZID_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 an
214   * authorization identity 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 authorization identity 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 authorization identity request control.
230   */
231  @NotNull()
232  public static AuthorizationIdentityRequestControl 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 AuthorizationIdentityRequestControl(
241         jsonControl.getCriticality());
242  }
243
244
245
246  /**
247   * {@inheritDoc}
248   */
249  @Override()
250  public void toString(@NotNull final StringBuilder buffer)
251  {
252    buffer.append("AuthorizationIdentityRequestControl(isCritical=");
253    buffer.append(isCritical());
254    buffer.append(')');
255  }
256}