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.extensions;
037
038
039
040import com.unboundid.ldap.sdk.Control;
041import com.unboundid.ldap.sdk.ExtendedRequest;
042import com.unboundid.ldap.sdk.ExtendedResult;
043import com.unboundid.ldap.sdk.LDAPConnection;
044import com.unboundid.ldap.sdk.LDAPException;
045import com.unboundid.ldap.sdk.ResultCode;
046import com.unboundid.util.NotMutable;
047import com.unboundid.util.NotNull;
048import com.unboundid.util.Nullable;
049import com.unboundid.util.ThreadSafety;
050import com.unboundid.util.ThreadSafetyLevel;
051
052import static com.unboundid.ldap.sdk.extensions.ExtOpMessages.*;
053
054
055
056/**
057 * This class provides an implementation of the LDAP "Who Am I?" extended
058 * request as defined in
059 * <A HREF="http://www.ietf.org/rfc/rfc4532.txt">RFC 4532</A>.  It may be used
060 * to request the current authorization identity associated with the client
061 * connection.
062 * <BR><BR>
063 * The "Who Am I?" extended operation is similar to the
064 * {@link com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl}
065 * in that it can be used to request the authorization identity for the
066 * connection.  The primary difference between them is that the authorization
067 * identity request control can only be included in a bind request (and the
068 * corresponding response control will be included in the bind result), while
069 * the "Who Am I?" extended operation can be used at any time through a separate
070 * operation.
071 * <BR><BR>
072 * <H2>Example</H2>
073 * The following example demonstrates the use of the "Who Am I?" extended
074 * operation.
075 * <PRE>
076 * // Use the "Who Am I?" extended request to determine the identity of the
077 * // currently-authenticated user.
078 * WhoAmIExtendedResult whoAmIResult;
079 * try
080 * {
081 *   whoAmIResult = (WhoAmIExtendedResult)
082 *        connection.processExtendedOperation(new WhoAmIExtendedRequest());
083 *   // This doesn't necessarily mean that the operation was successful, since
084 *   // some kinds of extended operations return non-success results under
085 *   // normal conditions.
086 * }
087 * catch (LDAPException le)
088 * {
089 *   // For an extended operation, this generally means that a problem was
090 *   // encountered while trying to send the request or read the result.
091 *   whoAmIResult = new WhoAmIExtendedResult(new ExtendedResult(le));
092 * }
093 *
094 * LDAPTestUtils.assertResultCodeEquals(whoAmIResult, ResultCode.SUCCESS);
095 * String authzID = whoAmIResult.getAuthorizationID();
096 * if (authzID.equals("") || authzID.equals("dn:"))
097 * {
098 *   // The user is authenticated anonymously.
099 * }
100 * else if (authzID.startsWith("dn:"))
101 * {
102 *   // The DN of the authenticated user should be authzID.substring(3)
103 * }
104 * else if (authzID.startsWith("u:"))
105 * {
106 *   // The username of the authenticated user should be authzID.substring(2)
107 * }
108 * else
109 * {
110 *   // The authorization ID isn't in any recognizable format.  Perhaps it's
111 *   // a raw DN or a username?
112 * }
113 * </PRE>
114 */
115@NotMutable()
116@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
117public final class WhoAmIExtendedRequest
118       extends ExtendedRequest
119{
120  /**
121   * The OID (1.3.6.1.4.1.4203.1.11.3) for the "Who Am I?" extended request.
122   */
123  @NotNull public static final String WHO_AM_I_REQUEST_OID =
124       "1.3.6.1.4.1.4203.1.11.3";
125
126
127
128  /**
129   * The serial version UID for this serializable class.
130   */
131  private static final long serialVersionUID = -2936513698220673318L;
132
133
134
135  /**
136   * Creates a new "Who Am I?" extended request.
137   */
138  public WhoAmIExtendedRequest()
139  {
140    super(WHO_AM_I_REQUEST_OID);
141  }
142
143
144
145  /**
146   * Creates a new "Who Am I?" extended request.
147   *
148   * @param  controls  The set of controls to include in the request.
149   */
150  public WhoAmIExtendedRequest(@Nullable final Control[] controls)
151  {
152    super(WHO_AM_I_REQUEST_OID, controls);
153  }
154
155
156
157  /**
158   * Creates a new "Who Am I?" extended request from the provided generic
159   * extended request.
160   *
161   * @param  extendedRequest  The generic extended request to use to create this
162   *                          "Who Am I?" extended request.
163   *
164   * @throws  LDAPException  If a problem occurs while decoding the request.
165   */
166  public WhoAmIExtendedRequest(@NotNull final ExtendedRequest extendedRequest)
167         throws LDAPException
168  {
169    super(extendedRequest);
170
171    if (extendedRequest.hasValue())
172    {
173      throw new LDAPException(ResultCode.DECODING_ERROR,
174                              ERR_WHO_AM_I_REQUEST_HAS_VALUE.get());
175    }
176  }
177
178
179
180  /**
181   * {@inheritDoc}
182   */
183  @Override()
184  @NotNull()
185  public WhoAmIExtendedResult process(@NotNull final LDAPConnection connection,
186                                      final int depth)
187         throws LDAPException
188  {
189    final ExtendedResult extendedResponse = super.process(connection, depth);
190    return new WhoAmIExtendedResult(extendedResponse);
191  }
192
193
194
195  /**
196   * {@inheritDoc}
197   */
198  @Override()
199  @NotNull()
200  public WhoAmIExtendedRequest duplicate()
201  {
202    return duplicate(getControls());
203  }
204
205
206
207  /**
208   * {@inheritDoc}
209   */
210  @Override()
211  @NotNull()
212  public WhoAmIExtendedRequest duplicate(@Nullable final Control[] controls)
213  {
214    final WhoAmIExtendedRequest r = new WhoAmIExtendedRequest(controls);
215    r.setResponseTimeoutMillis(getResponseTimeoutMillis(null));
216    return r;
217  }
218
219
220
221  /**
222   * {@inheritDoc}
223   */
224  @Override()
225  @NotNull()
226  public String getExtendedRequestName()
227  {
228    return INFO_EXTENDED_REQUEST_NAME_WHO_AM_I.get();
229  }
230
231
232
233  /**
234   * {@inheritDoc}
235   */
236  @Override()
237  public void toString(@NotNull final StringBuilder buffer)
238  {
239    buffer.append("WhoAmIExtendedRequest(");
240
241    final Control[] controls = getControls();
242    if (controls.length > 0)
243    {
244      buffer.append("controls={");
245      for (int i=0; i < controls.length; i++)
246      {
247        if (i > 0)
248        {
249          buffer.append(", ");
250        }
251
252        buffer.append(controls[i]);
253      }
254      buffer.append('}');
255    }
256
257    buffer.append(')');
258  }
259}