001/* 002 * Copyright 2015-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2015-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) 2015-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.unboundidds.controls; 037 038 039 040import java.io.Serializable; 041import java.util.ArrayList; 042 043import com.unboundid.asn1.ASN1Boolean; 044import com.unboundid.asn1.ASN1Element; 045import com.unboundid.asn1.ASN1OctetString; 046import com.unboundid.asn1.ASN1Sequence; 047import com.unboundid.ldap.sdk.LDAPException; 048import com.unboundid.ldap.sdk.ResultCode; 049import com.unboundid.ldap.sdk.unboundidds.extensions.PasswordQualityRequirement; 050import com.unboundid.util.Debug; 051import com.unboundid.util.NotMutable; 052import com.unboundid.util.NotNull; 053import com.unboundid.util.Nullable; 054import com.unboundid.util.StaticUtils; 055import com.unboundid.util.ThreadSafety; 056import com.unboundid.util.ThreadSafetyLevel; 057import com.unboundid.util.Validator; 058 059import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*; 060 061 062 063/** 064 * This class provides a data structure that holds information about the result 065 * of attempting validation with a proposed password against a password quality 066 * requirement. 067 * <BR> 068 * <BLOCKQUOTE> 069 * <B>NOTE:</B> This class, and other classes within the 070 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 071 * supported for use against Ping Identity, UnboundID, and 072 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 073 * for proprietary functionality or for external specifications that are not 074 * considered stable or mature enough to be guaranteed to work in an 075 * interoperable way with other types of LDAP servers. 076 * </BLOCKQUOTE> 077 * <BR> 078 * If it appears in an LDAP protocol element (e.g., in a password validation 079 * details response control), then the password quality validation result object 080 * should have the following ASN.1 encoding: 081 * <PRE> 082 * PasswordQualityRequirementValidationResult ::= SEQUENCE { 083 * passwordRequirement PasswordQualityRequirement, 084 * requirementSatisfied BOOLEAN, 085 * additionalInfo [0] OCTET STRING OPTIONAL } 086 * </PRE> 087 */ 088@NotMutable() 089@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 090public final class PasswordQualityRequirementValidationResult 091 implements Serializable 092{ 093 /** 094 * The BER type for the additional info element of the value sequence. 095 */ 096 private static final byte TYPE_ADDITIONAL_INFO = (byte) 0x80; 097 098 099 100 /** 101 * The serial version UID for this serializable class. 102 */ 103 private static final long serialVersionUID = -8048878239770726375L; 104 105 106 107 // Indicates whether the proposed password satisfied the constraints of the 108 // associated password quality requirement. 109 private final boolean requirementSatisfied; 110 111 // The password quality requirement to which this validation result applies. 112 @NotNull private final PasswordQualityRequirement passwordRequirement; 113 114 // An optional message with additional information about the result of the 115 // validation for the proposed password with respect to the associated 116 // password quality requirement. 117 @Nullable private final String additionalInfo; 118 119 120 121 /** 122 * Creates a new password quality requirement validation result object with 123 * the provided information. 124 * 125 * @param passwordRequirement The password quality requirement to which 126 * this validation result applies. This must 127 * not be {@code null}. 128 * @param requirementSatisfied Indicates whether the proposed password 129 * satisfied the constraints of the associated 130 * password quality requirement. 131 * @param additionalInfo An optional message with additional 132 * information about the result of the 133 * validation for the proposed password with 134 * respect to the associated password quality 135 * requirement. 136 */ 137 public PasswordQualityRequirementValidationResult( 138 @NotNull final PasswordQualityRequirement passwordRequirement, 139 final boolean requirementSatisfied, 140 @Nullable final String additionalInfo) 141 { 142 Validator.ensureNotNull(passwordRequirement); 143 144 this.passwordRequirement = passwordRequirement; 145 this.requirementSatisfied = requirementSatisfied; 146 this.additionalInfo = additionalInfo; 147 } 148 149 150 151 /** 152 * Retrieves the password quality requirement to which this validation result 153 * applies. 154 * 155 * @return The password quality requirement to which this validation result 156 * applies. 157 */ 158 @NotNull() 159 public PasswordQualityRequirement getPasswordRequirement() 160 { 161 return passwordRequirement; 162 } 163 164 165 166 /** 167 * Indicates whether the proposed password satisfied the constraints of the 168 * associated password quality requirement. 169 * 170 * @return {@code true} if the proposed password satisfied the constraints of 171 * the associated password quality requirement, or {@code false} if 172 * not. 173 */ 174 public boolean requirementSatisfied() 175 { 176 return requirementSatisfied; 177 } 178 179 180 181 /** 182 * Retrieves a message with additional information about the result of the 183 * validation of the proposed password with respect to the associated 184 * password quality requirement. 185 * 186 * @return A message with additional information about the result of the 187 * validation, or {@code null} if no additional information is 188 * available. 189 */ 190 @Nullable() 191 public String getAdditionalInfo() 192 { 193 return additionalInfo; 194 } 195 196 197 198 /** 199 * Encodes this password quality requirement validation result object to an 200 * ASN.1 element. 201 * 202 * @return The ASN.1 element that provides an encoded representation of this 203 * object. 204 */ 205 @NotNull() 206 public ASN1Element encode() 207 { 208 final ArrayList<ASN1Element> elements = new ArrayList<>(3); 209 elements.add(passwordRequirement.encode()); 210 elements.add(new ASN1Boolean(requirementSatisfied)); 211 212 if (additionalInfo != null) 213 { 214 elements.add(new ASN1OctetString(TYPE_ADDITIONAL_INFO, additionalInfo)); 215 } 216 217 return new ASN1Sequence(elements); 218 } 219 220 221 222 /** 223 * Decodes the provided ASN.1 element as a password quality requirement 224 * validation result. 225 * 226 * @param element The ASN.1 element to be decoded as a password quality 227 * requirement validation result. 228 * 229 * @return The ASN.1 element containing the encoded password quality 230 * requirement validation result. 231 * 232 * @throws LDAPException If a problem is encountered while attempting to 233 * decode the provided ASN.1 element. 234 */ 235 @NotNull() 236 public static PasswordQualityRequirementValidationResult decode( 237 @NotNull final ASN1Element element) 238 throws LDAPException 239 { 240 try 241 { 242 final ASN1Element[] elements = 243 ASN1Sequence.decodeAsSequence(element).elements(); 244 final PasswordQualityRequirement passwordRequirement = 245 PasswordQualityRequirement.decode(elements[0]); 246 final boolean requirementSatisfied = 247 ASN1Boolean.decodeAsBoolean(elements[1]).booleanValue(); 248 249 String additionalInfo = null; 250 for (int i=2; i < elements.length; i++) 251 { 252 switch (elements[i].getType()) 253 { 254 case TYPE_ADDITIONAL_INFO: 255 additionalInfo = 256 ASN1OctetString.decodeAsOctetString(elements[i]).stringValue(); 257 break; 258 259 default: 260 throw new LDAPException(ResultCode.DECODING_ERROR, 261 ERR_PW_REQ_VALIDATION_RESULT_INVALID_ELEMENT_TYPE.get( 262 StaticUtils.toHex(elements[i].getType()))); 263 } 264 } 265 266 return new PasswordQualityRequirementValidationResult(passwordRequirement, 267 requirementSatisfied, additionalInfo); 268 } 269 catch (final LDAPException le) 270 { 271 Debug.debugException(le); 272 throw le; 273 } 274 catch (final Exception e) 275 { 276 Debug.debugException(e); 277 throw new LDAPException(ResultCode.DECODING_ERROR, 278 ERR_PW_REQ_VALIDATION_RESULT_CANNOT_DECODE.get( 279 StaticUtils.getExceptionMessage(e)), 280 e); 281 } 282 } 283 284 285 286 /** 287 * Retrieves a string representation of this password quality requirement 288 * validation result. 289 * 290 * @return A string representation of this password quality requirement 291 * validation result. 292 */ 293 @Override() 294 @NotNull() 295 public String toString() 296 { 297 final StringBuilder buffer = new StringBuilder(); 298 toString(buffer); 299 return buffer.toString(); 300 } 301 302 303 304 /** 305 * Appends a string representation of this password quality requirement 306 * validation result to the provided buffer. 307 * 308 * @param buffer The buffer to which the information should be appended. 309 */ 310 public void toString(@NotNull final StringBuilder buffer) 311 { 312 buffer.append("PasswordQualityRequirementValidationResult(requirement="); 313 passwordRequirement.toString(buffer); 314 buffer.append(", requirementSatisfied="); 315 buffer.append(requirementSatisfied); 316 317 if (additionalInfo != null) 318 { 319 buffer.append(", additionalInfo='"); 320 buffer.append(additionalInfo); 321 buffer.append('\''); 322 } 323 324 buffer.append(')'); 325 } 326}