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