|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + * |
| 8 | + * Modifications Copyright OpenSearch Contributors. See |
| 9 | + * GitHub history for details. |
| 10 | + */ |
| 11 | + |
| 12 | +package org.opensearch.security.dlic.rest.validation; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import java.util.Locale; |
| 16 | +import java.util.Objects; |
| 17 | +import java.util.StringJoiner; |
| 18 | +import java.util.function.Predicate; |
| 19 | +import java.util.regex.Pattern; |
| 20 | + |
| 21 | +import com.google.common.collect.ImmutableList; |
| 22 | +import com.nulabinc.zxcvbn.Strength; |
| 23 | +import com.nulabinc.zxcvbn.Zxcvbn; |
| 24 | +import com.nulabinc.zxcvbn.matchers.Match; |
| 25 | +import org.apache.logging.log4j.LogManager; |
| 26 | +import org.apache.logging.log4j.Logger; |
| 27 | + |
| 28 | +import org.opensearch.common.Strings; |
| 29 | +import org.opensearch.common.settings.Settings; |
| 30 | +import org.opensearch.security.dlic.rest.validation.AbstractConfigurationValidator.ErrorType; |
| 31 | + |
| 32 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_PASSWORD_MIN_LENGTH; |
| 33 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_PASSWORD_SCORE_BASED_VALIDATION_STRENGTH; |
| 34 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_PASSWORD_VALIDATION_REGEX; |
| 35 | + |
| 36 | +public class PasswordValidator { |
| 37 | + |
| 38 | + private static final int MAX_LENGTH = 100; |
| 39 | + |
| 40 | + /** |
| 41 | + * Checks a username similarity and a password |
| 42 | + * names and passwords like: |
| 43 | + * - some_user_name/456Some_uSer_Name_1234 |
| 44 | + * - some_user_name/some_user_name_Ydfge |
| 45 | + * - some_user_name/eman_resu_emos |
| 46 | + * are similar |
| 47 | + * "user_inputs" - is a default dictionary zxcvbn creates for checking similarity |
| 48 | + */ |
| 49 | + private final static Predicate<Match> USERNAME_SIMILARITY_CHECK = m -> |
| 50 | + m.pattern == com.nulabinc.zxcvbn.Pattern.Dictionary && "user_inputs".equals(m.dictionaryName); |
| 51 | + |
| 52 | + private final Logger logger = LogManager.getLogger(this.getClass()); |
| 53 | + |
| 54 | + private final int minPasswordLength; |
| 55 | + |
| 56 | + private final Pattern passwordRegexpPattern; |
| 57 | + |
| 58 | + private final ScoreStrength scoreStrength; |
| 59 | + |
| 60 | + private final Zxcvbn zxcvbn; |
| 61 | + |
| 62 | + private PasswordValidator(final int minPasswordLength, |
| 63 | + final Pattern passwordRegexpPattern, |
| 64 | + final ScoreStrength scoreStrength) { |
| 65 | + this.minPasswordLength = minPasswordLength; |
| 66 | + this.passwordRegexpPattern = passwordRegexpPattern; |
| 67 | + this.scoreStrength = scoreStrength; |
| 68 | + this.zxcvbn = new Zxcvbn(); |
| 69 | + } |
| 70 | + |
| 71 | + public static PasswordValidator of(final Settings settings) { |
| 72 | + final String passwordRegex = settings.get(SECURITY_RESTAPI_PASSWORD_VALIDATION_REGEX, null); |
| 73 | + final ScoreStrength scoreStrength = ScoreStrength.fromConfiguration( |
| 74 | + settings.get(SECURITY_RESTAPI_PASSWORD_SCORE_BASED_VALIDATION_STRENGTH, ScoreStrength.STRONG.name()) |
| 75 | + ); |
| 76 | + final int minPasswordLength = settings.getAsInt(SECURITY_RESTAPI_PASSWORD_MIN_LENGTH, -1); |
| 77 | + return new PasswordValidator( |
| 78 | + minPasswordLength, |
| 79 | + !Strings.isNullOrEmpty(passwordRegex) ? Pattern.compile(String.format("^%s$", passwordRegex)) : null, |
| 80 | + scoreStrength); |
| 81 | + } |
| 82 | + |
| 83 | + ErrorType validate(final String username, final String password) { |
| 84 | + if (minPasswordLength > 0 && password.length() < minPasswordLength) { |
| 85 | + logger.debug( |
| 86 | + "Password is too short, the minimum required length is {}, but current length is {}", |
| 87 | + minPasswordLength, |
| 88 | + password.length() |
| 89 | + ); |
| 90 | + return ErrorType.INVALID_PASSWORD; |
| 91 | + } |
| 92 | + if (password.length() > MAX_LENGTH) { |
| 93 | + logger.debug( |
| 94 | + "Password is too long, the maximum required length is {}, but current length is {}", |
| 95 | + MAX_LENGTH, |
| 96 | + password.length() |
| 97 | + ); |
| 98 | + return ErrorType.INVALID_PASSWORD; |
| 99 | + } |
| 100 | + if (Objects.nonNull(passwordRegexpPattern) |
| 101 | + && !passwordRegexpPattern.matcher(password).matches()) { |
| 102 | + logger.debug("Regex does not match password"); |
| 103 | + return ErrorType.INVALID_PASSWORD; |
| 104 | + } |
| 105 | + final Strength strength = zxcvbn.measure(password, ImmutableList.of(username)); |
| 106 | + if (strength.getScore() < scoreStrength.score()) { |
| 107 | + logger.debug( |
| 108 | + "Password is weak the required score is {}, but current is {}", |
| 109 | + scoreStrength, |
| 110 | + ScoreStrength.fromScore(strength.getScore()) |
| 111 | + ); |
| 112 | + return ErrorType.WEAK_PASSWORD; |
| 113 | + } |
| 114 | + final boolean similar = strength.getSequence() |
| 115 | + .stream() |
| 116 | + .anyMatch(USERNAME_SIMILARITY_CHECK); |
| 117 | + if (similar) { |
| 118 | + logger.debug("Password is too similar to the user name {}", username); |
| 119 | + return ErrorType.SIMILAR_PASSWORD; |
| 120 | + } |
| 121 | + return ErrorType.NONE; |
| 122 | + } |
| 123 | + |
| 124 | + public enum ScoreStrength { |
| 125 | + |
| 126 | + // The weak score defines here only for debugging information |
| 127 | + // and doesn't use as a configuration setting value. |
| 128 | + WEAK(0, "too guessable: risky password"), |
| 129 | + FAIR(1, "very guessable: protection from throttled online attacks"), |
| 130 | + GOOD(2, "somewhat guessable: protection from unthrottled online attacks"), |
| 131 | + STRONG(3, "safely unguessable: moderate protection from offline slow-hash scenario"), |
| 132 | + VERY_STRONG(4, "very unguessable: strong protection from offline slow-hash scenario"); |
| 133 | + |
| 134 | + private final int score; |
| 135 | + |
| 136 | + private final String description; |
| 137 | + |
| 138 | + static final List<ScoreStrength> CONFIGURATION_VALUES = ImmutableList.of(FAIR, STRONG, VERY_STRONG); |
| 139 | + |
| 140 | + static final String EXPECTED_CONFIGURATION_VALUES = |
| 141 | + new StringJoiner(",") |
| 142 | + .add(FAIR.name().toLowerCase(Locale.ROOT)) |
| 143 | + .add(STRONG.name().toLowerCase(Locale.ROOT)) |
| 144 | + .add(VERY_STRONG.name().toLowerCase(Locale.ROOT)) |
| 145 | + .toString(); |
| 146 | + |
| 147 | + private ScoreStrength(final int score, final String description) { |
| 148 | + this.score = score; |
| 149 | + this.description = description; |
| 150 | + } |
| 151 | + |
| 152 | + public static ScoreStrength fromScore(final int score) { |
| 153 | + for (final ScoreStrength strength : values()) { |
| 154 | + if (strength.score == score) |
| 155 | + return strength; |
| 156 | + } |
| 157 | + throw new IllegalArgumentException("Unknown score " + score); |
| 158 | + } |
| 159 | + |
| 160 | + public static ScoreStrength fromConfiguration(final String value) { |
| 161 | + for (final ScoreStrength strength : CONFIGURATION_VALUES) { |
| 162 | + if (strength.name().equalsIgnoreCase(value)) |
| 163 | + return strength; |
| 164 | + } |
| 165 | + throw new IllegalArgumentException( |
| 166 | + String.format( |
| 167 | + "Setting [%s] cannot be used with the configured: %s. Expected one of [%s]", |
| 168 | + SECURITY_RESTAPI_PASSWORD_SCORE_BASED_VALIDATION_STRENGTH, |
| 169 | + value, |
| 170 | + EXPECTED_CONFIGURATION_VALUES |
| 171 | + ) |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public String toString() { |
| 177 | + return String.format("Password strength score %s. %s", score, description); |
| 178 | + } |
| 179 | + |
| 180 | + public int score() { |
| 181 | + return this.score; |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | +} |
0 commit comments