001/*
002 * Copyright 2016-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2016-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) 2016-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.unboundidds.extensions;
037
038
039
040import com.unboundid.asn1.ASN1Element;
041import com.unboundid.asn1.ASN1OctetString;
042import com.unboundid.asn1.ASN1Sequence;
043import com.unboundid.ldap.sdk.Control;
044import com.unboundid.ldap.sdk.ExtendedResult;
045import com.unboundid.ldap.sdk.LDAPException;
046import com.unboundid.ldap.sdk.ResultCode;
047import com.unboundid.util.Debug;
048import com.unboundid.util.NotMutable;
049import com.unboundid.util.NotNull;
050import com.unboundid.util.Nullable;
051import com.unboundid.util.StaticUtils;
052import com.unboundid.util.ThreadSafety;
053import com.unboundid.util.ThreadSafetyLevel;
054import com.unboundid.util.Validator;
055
056import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
057
058
059
060/**
061 * This class provides an implementation of an extended result that may be used
062 * to provide the client with a TOTP shared secret generated by the server in
063 * response to a {@link GenerateTOTPSharedSecretExtendedRequest}.
064 * <BR>
065 * <BLOCKQUOTE>
066 *   <B>NOTE:</B>  This class, and other classes within the
067 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
068 *   supported for use against Ping Identity, UnboundID, and
069 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
070 *   for proprietary functionality or for external specifications that are not
071 *   considered stable or mature enough to be guaranteed to work in an
072 *   interoperable way with other types of LDAP servers.
073 * </BLOCKQUOTE>
074 * <BR>
075 * If the extended request was processed successfully, then this result will
076 * have an OID of 1.3.6.1.4.1.30221.2.6.57 and a value with the following
077 * encoding:
078 * <BR><BR>
079 * <PRE>
080 *   GenerateTOTPSharedSecretResult ::= SEQUENCE {
081 *        totpSharedSecret     [0] OCTET STRING }
082 *        ... }
083 * </PRE>
084 */
085@NotMutable()
086@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
087public final class GenerateTOTPSharedSecretExtendedResult
088       extends ExtendedResult
089{
090  /**
091   * The OID (1.3.6.1.4.1.30221.2.6.57) for the generate TOTP shared secret
092   * extended result.
093   */
094  @NotNull public static final String GENERATE_TOTP_SHARED_SECRET_RESULT_OID =
095       "1.3.6.1.4.1.30221.2.6.57";
096
097
098
099  /**
100   * The BER type for the TOTP shared secret element of the result value
101   * sequence.
102   */
103  private static final byte TYPE_TOTP_SHARED_SECRET = (byte) 0x80;
104
105
106
107  /**
108   * The serial version UID for this serializable class.
109   */
110  private static final long serialVersionUID = 8505040895542971346L;
111
112
113
114  // The base32-encoded representation TOTP shared secret generated by the
115  // server.
116  @Nullable private final String totpSharedSecret;
117
118
119
120  /**
121   * Generates a new generate TOTP shared secret extended result for the case in
122   * which the server was able to generate the requested TOTP shared secret.
123   *
124   * @param  messageID         The message ID for the LDAP message that is
125   *                           associated with this LDAP result.
126   * @param  totpSharedSecret  The base32-encoded representation of the TOTP
127   *                            shared secret generated by the server.  It must
128   *                           not be {@code null}.
129   * @param  responseControls  The set of controls from the response, if
130   *                           available.
131   */
132  public GenerateTOTPSharedSecretExtendedResult(final int messageID,
133              @NotNull final String totpSharedSecret,
134              @Nullable final Control... responseControls)
135  {
136    this(messageID, ResultCode.SUCCESS, null, null, null, totpSharedSecret,
137         responseControls);
138  }
139
140
141
142  /**
143   * Creates a new generate TOTP shared secret extended result with the provided
144   * information.
145   *
146   * @param  messageID          The message ID for the LDAP message that is
147   *                            associated with this LDAP result.
148   * @param  resultCode         The result code from the response.
149   * @param  diagnosticMessage  The diagnostic message from the response, if
150   *                            available.
151   * @param  matchedDN          The matched DN from the response, if available.
152   * @param  referralURLs       The set of referral URLs from the response, if
153   *                            available.
154   * @param  totpSharedSecret   The base32-encoded representation of the TOTP
155   *                            shared secret generated by the server, if
156   *                            available.
157   * @param  responseControls   The set of controls from the response, if
158   *                            available.
159   */
160  public GenerateTOTPSharedSecretExtendedResult(final int messageID,
161              @NotNull final ResultCode resultCode,
162              @Nullable final String diagnosticMessage,
163              @Nullable final String matchedDN,
164              @Nullable final String[] referralURLs,
165              @Nullable final String totpSharedSecret,
166              @Nullable final Control... responseControls)
167  {
168    super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
169         ((totpSharedSecret == null)
170              ? null
171              : GENERATE_TOTP_SHARED_SECRET_RESULT_OID),
172         encodeValue(totpSharedSecret), responseControls);
173
174    this.totpSharedSecret = totpSharedSecret;
175
176    if (totpSharedSecret == null)
177    {
178      Validator.ensureTrue((resultCode != ResultCode.SUCCESS),
179           "If the result code is SUCCESS, the TOTP shared secret must be " +
180                "non-null");
181    }
182  }
183
184
185
186  /**
187   * Creates a new generate TOTP shared secret extended result from the provided
188   * extended result.
189   *
190   * @param  extendedResult  The extended result to be decoded as a generate
191   *                         TOTP shared secret extended result.  It must not be
192   *                         {@code null}.
193   *
194   * @throws  LDAPException  If the provided extended result cannot be decoded
195   *                         as a generate TOTP shared secret result.
196   */
197  public GenerateTOTPSharedSecretExtendedResult(
198              @NotNull final ExtendedResult extendedResult)
199         throws LDAPException
200  {
201    super(extendedResult);
202
203    final ASN1OctetString value = extendedResult.getValue();
204    if (value == null)
205    {
206      totpSharedSecret = null;
207    }
208    else
209    {
210      try
211      {
212        final ASN1Element[] elements =
213             ASN1Sequence.decodeAsSequence(value.getValue()).elements();
214        totpSharedSecret =
215             ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
216      }
217      catch (final Exception e)
218      {
219        Debug.debugException(e);
220        throw new LDAPException(ResultCode.DECODING_ERROR,
221             ERR_GEN_TOTP_SECRET_RESULT_ERROR_DECODING_VALUE.get(
222                  StaticUtils.getExceptionMessage(e)));
223      }
224    }
225  }
226
227
228
229  /**
230   * Encodes the provided information into an ASN.1 octet string suitable for
231   * use as the value of this extended result.
232   *
233   * @param  totpSharedSecret   The base32-encoded representation of the TOTP
234   *                            shared secret generated by the server, if
235   *                            available.
236   *
237   * @return  The ASN.1 octet string suitable for use as the value of this
238   *          extended result, or {@code null} if there should be no value.
239   */
240  @Nullable()
241  private static ASN1OctetString encodeValue(
242               @Nullable final String totpSharedSecret)
243  {
244    if (totpSharedSecret == null)
245    {
246      return null;
247    }
248
249    return new ASN1OctetString(new ASN1Sequence(new ASN1OctetString(
250         TYPE_TOTP_SHARED_SECRET, totpSharedSecret)).encode());
251  }
252
253
254
255  /**
256   * Retrieves the base32-encoded representation of the TOTP shared secret
257   * generated by the server, if available.
258   *
259   * @return  The base32-encoded representation of the TOTP shared secret
260   *          generated by the server, or {@code null} if none was provided.
261   */
262  @Nullable()
263  public String getTOTPSharedSecret()
264  {
265    return totpSharedSecret;
266  }
267
268
269
270  /**
271   * {@inheritDoc}
272   */
273  @Override()
274  @NotNull()
275  public String getExtendedResultName()
276  {
277    return INFO_GEN_TOTP_SECRET_RESULT_NAME.get();
278  }
279
280
281
282  /**
283   * Appends a string representation of this extended result to the provided
284   * buffer.
285   *
286   * @param  buffer  The buffer to which a string representation of this
287   *                 extended result will be appended.
288   */
289  @Override()
290  public void toString(@NotNull final StringBuilder buffer)
291  {
292    buffer.append("GenerateTOTPSharedSecretExtendedResult(resultCode=");
293    buffer.append(getResultCode());
294
295    final int messageID = getMessageID();
296    if (messageID >= 0)
297    {
298      buffer.append(", messageID=");
299      buffer.append(messageID);
300    }
301
302    final String diagnosticMessage = getDiagnosticMessage();
303    if (diagnosticMessage != null)
304    {
305      buffer.append(", diagnosticMessage='");
306      buffer.append(diagnosticMessage);
307      buffer.append('\'');
308    }
309
310    final String matchedDN = getMatchedDN();
311    if (matchedDN != null)
312    {
313      buffer.append(", matchedDN='");
314      buffer.append(matchedDN);
315      buffer.append('\'');
316    }
317
318    final String[] referralURLs = getReferralURLs();
319    if (referralURLs.length > 0)
320    {
321      buffer.append(", referralURLs={");
322      for (int i=0; i < referralURLs.length; i++)
323      {
324        if (i > 0)
325        {
326          buffer.append(", ");
327        }
328
329        buffer.append('\'');
330        buffer.append(referralURLs[i]);
331        buffer.append('\'');
332      }
333      buffer.append('}');
334    }
335
336    final Control[] responseControls = getResponseControls();
337    if (responseControls.length > 0)
338    {
339      buffer.append(", responseControls={");
340      for (int i=0; i < responseControls.length; i++)
341      {
342        if (i > 0)
343        {
344          buffer.append(", ");
345        }
346
347        buffer.append(responseControls[i]);
348      }
349      buffer.append('}');
350    }
351
352    buffer.append(')');
353  }
354}