001/*
002 * Copyright 2020-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2020-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) 2020-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.controls;
037
038
039
040import com.unboundid.asn1.ASN1OctetString;
041import com.unboundid.ldap.sdk.BindResult;
042import com.unboundid.ldap.sdk.Control;
043import com.unboundid.ldap.sdk.DecodeableControl;
044import com.unboundid.ldap.sdk.LDAPException;
045import com.unboundid.ldap.sdk.ResultCode;
046import com.unboundid.util.Debug;
047import com.unboundid.util.NotMutable;
048import com.unboundid.util.NotNull;
049import com.unboundid.util.Nullable;
050import com.unboundid.util.ThreadSafety;
051import com.unboundid.util.ThreadSafetyLevel;
052import com.unboundid.util.json.JSONException;
053import com.unboundid.util.json.JSONObject;
054
055import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
056
057
058
059/**
060 * This class provides an implementation of a response control that can be
061 * included in the response to a successful bind operation to provide
062 * information about recent successful and failed authentication attempts.
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.62, a criticality of
075 * {@code false}, and a value that is a JSON object with two top-level fields:
076 * successful-attempts and failed-attempts.  The value for each of these fields
077 * will be an array of JSON objects with the following fields:
078 * <UL>
079 *   <LI>timestamp -- The timestamp of the login attempt in the ISO 8601 format
080 *       described in RFC 3339.</LI>
081 *   <LI>client-ip-address -- A string representation of the IP address of the
082 *       client that tried to authenticate.</LI>
083 *   <LI>authentication-method -- The name of the method that the client used
084 *       when trying to authenticate.</LI>
085 *   <LI>failure-reason -- A string providing a general reason that the
086 *       authentication attempt failed (only used for failed attempts).</LI>
087 *   <LI>additional-attempt-count -- An integer value that indicates how many
088 *       other attempts were made on the same date with the same settings for
089 *       all fields except the timestamp.</LI>
090 * </UL>
091 *
092 * @see  GetRecentLoginHistoryRequestControl
093 */
094@NotMutable()
095@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
096public final class GetRecentLoginHistoryResponseControl
097       extends Control
098       implements DecodeableControl
099{
100  /**
101   * The OID (1.3.6.1.4.1.30221.2.5.62) for the get password policy state issues
102   * response control.
103   */
104  @NotNull public static final String GET_RECENT_LOGIN_HISTORY_RESPONSE_OID =
105       "1.3.6.1.4.1.30221.2.5.62";
106
107
108
109  /**
110   * The serial version UID for this serializable class.
111   */
112  private static final long serialVersionUID = -4604204310334007290L;
113
114
115
116  // The recent login history contained in the response control.
117  @NotNull private final RecentLoginHistory recentLoginHistory;
118
119
120
121  /**
122   * Creates a new empty control instance that is intended to be used only for
123   * decoding controls via the {@code DecodeableControl} interface.
124   */
125  GetRecentLoginHistoryResponseControl()
126  {
127    recentLoginHistory = null;
128  }
129
130
131
132  /**
133   * Creates a new instance of this control with the provided information.
134   *
135   * @param  recentLoginHistory  The recent login history to include in the
136   *                             response control.  It must not be {@code null}.
137   */
138  public GetRecentLoginHistoryResponseControl(
139              @NotNull final RecentLoginHistory recentLoginHistory)
140  {
141    super(GET_RECENT_LOGIN_HISTORY_RESPONSE_OID, false,
142         new ASN1OctetString(recentLoginHistory.asJSONObject().toString()));
143
144    this.recentLoginHistory = recentLoginHistory;
145  }
146
147
148
149  /**
150   * Creates a new instance of this control that is decoded from the provided
151   * generic control.
152   *
153   * @param  oid         The OID for the control.
154   * @param  isCritical  Indicates whether this control should be marked
155   *                     critical.
156   * @param  value       The encoded value for the control.
157   *
158   * @throws LDAPException  If a problem is encountered while attempting to
159   *                         decode the provided control as a get recent login
160   *                         history response control.
161   */
162  public GetRecentLoginHistoryResponseControl(@NotNull final String oid,
163              final boolean isCritical, @Nullable final ASN1OctetString value)
164         throws LDAPException
165  {
166    super(oid, isCritical, value);
167
168    if (value == null)
169    {
170      throw new LDAPException(ResultCode.DECODING_ERROR,
171           ERR_GET_RECENT_LOGIN_HISTORY_RESPONSE_NO_VALUE.get());
172    }
173
174    final JSONObject jsonObject;
175    try
176    {
177      jsonObject = new JSONObject(value.stringValue());
178    }
179    catch (final JSONException e)
180    {
181      Debug.debugException(e);
182      throw new LDAPException(ResultCode.DECODING_ERROR,
183           ERR_GET_RECENT_LOGIN_HISTORY_RESPONSE_VALUE_NOT_JSON.get(
184                e.getMessage()),
185           e);
186    }
187
188    try
189    {
190      recentLoginHistory = new RecentLoginHistory(jsonObject);
191    }
192    catch (final LDAPException e)
193    {
194      Debug.debugException(e);
195      throw new LDAPException(ResultCode.DECODING_ERROR,
196           ERR_GET_RECENT_LOGIN_HISTORY_RESPONSE_CANNOT_PARSE_VALUE.get(
197                e.getMessage()),
198           e);
199    }
200  }
201
202
203
204  /**
205   * {@inheritDoc}
206   */
207  @Override()
208  @NotNull()
209  public GetRecentLoginHistoryResponseControl decodeControl(
210              @NotNull final String oid, final boolean isCritical,
211              @Nullable final ASN1OctetString value)
212          throws LDAPException
213  {
214    return new GetRecentLoginHistoryResponseControl(oid, isCritical, value);
215  }
216
217
218
219  /**
220   * Retrieves the recent login history contained in this response control.
221   *
222   * @return  The recent login history contained in this response control.
223   */
224  @NotNull()
225  public RecentLoginHistory getRecentLoginHistory()
226  {
227    return recentLoginHistory;
228  }
229
230
231
232  /**
233   * Extracts a get recent login history response control from the provided bind
234   * result.
235   *
236   * @param  bindResult  The bind result from which to retrieve the get recent
237   *                     login history response control.
238   *
239   * @return  The get recent login history response control contained in the
240   *          provided bind result, or {@code null} if the bind result did not
241   *          contain a get recent login history response control.
242   *
243   * @throws  LDAPException  If a problem is encountered while attempting to
244   *                         decode the get recent login history response
245   *                         control contained in the provided bind result.
246   */
247  @Nullable()
248  public static GetRecentLoginHistoryResponseControl get(
249                     @NotNull final BindResult bindResult)
250         throws LDAPException
251  {
252    final Control c =
253         bindResult.getResponseControl(GET_RECENT_LOGIN_HISTORY_RESPONSE_OID);
254    if (c == null)
255    {
256      return null;
257    }
258
259    if (c instanceof GetRecentLoginHistoryResponseControl)
260    {
261      return (GetRecentLoginHistoryResponseControl) c;
262    }
263    else
264    {
265      return new GetRecentLoginHistoryResponseControl(c.getOID(),
266           c.isCritical(), c.getValue());
267    }
268  }
269
270
271
272  /**
273   * {@inheritDoc}
274   */
275  @Override()
276  @NotNull()
277  public String getControlName()
278  {
279    return INFO_CONTROL_NAME_GET_RECENT_LOGIN_HISTORY_RESPONSE.get();
280  }
281
282
283
284  /**
285   * {@inheritDoc}
286   */
287  @Override()
288  public void toString(@NotNull final StringBuilder buffer)
289  {
290    buffer.append("GetRecentLoginHistoryResponseControl(recentLoginHistory=");
291    buffer.append(recentLoginHistory.toString());
292    buffer.append(')');
293  }
294}