Package org.passay.rule
Class HaveIBeenPwnedRule
java.lang.Object
org.passay.rule.HaveIBeenPwnedRule
- All Implemented Interfaces:
Rule
Validates the password against the online database of
haveibeenpwned.com
optionally allowing the usage of found passwords, but returns the number of found matches in
the metadata.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final booleanShould password be allowed if it is found in the API results.private final booleanShould password be allowed if API calls throw exceptions.private final URLURL of the API.private final StringName of the app.private DurationMaximum waiting time for established connection.private static final DurationDefault time to wait for HTTP connect.private static final DurationDefault time to wait for HTTP response.private static final StringURL for pwned passwords.static final StringError code for exposed passwords.static final StringError code for API IO errors.private static final intnumber of chars to use from the SHA1 digest for the api call.private DurationMaximum waiting time for reading all data. -
Constructor Summary
ConstructorsConstructorDescriptionHaveIBeenPwnedRule(String appName) Create the rule, appName is required by the API.HaveIBeenPwnedRule(String appName, String address) Create the rule, appName is required by the API.HaveIBeenPwnedRule(String appName, String address, boolean allowExposed, boolean allowOnException) Create the rule, appName is required by the API. -
Method Summary
Modifier and TypeMethodDescriptionprivate static StringgetHexDigest(PasswordData passwordData) Returns an uppercase, hex encoded, SHA1 hash of the password.private LineNumberReaderopenApiConnectionForRange(String range) Opens a connection to the API and returns a reader for the input stream.private RuleResultsearchResponse(String hexDigest, LineNumberReader reader) Reads the supplied reader line by line until a match is found against the supplied hex digest.voidsetConnectTimeout(Duration timeout) maximum Duration for connecting to the API.voidsetReadTimeout(Duration timeout) maximum Duration for reading from the API.toString()validate(PasswordData passwordData) Validates the supplied password data per the requirements of this rule.
-
Field Details
-
ERROR_CODE
Error code for exposed passwords.- See Also:
-
IO_ERROR_CODE
Error code for API IO errors.- See Also:
-
DEFAULT_URL
URL for pwned passwords.- See Also:
-
PREFIX_LENGTH
private static final int PREFIX_LENGTHnumber of chars to use from the SHA1 digest for the api call.- See Also:
-
DEFAULT_CONNECT_TIMEOUT
Default time to wait for HTTP connect. -
DEFAULT_READ_TIMEOUT
Default time to wait for HTTP response. -
applicationName
Name of the app. -
apiUrl
URL of the API. -
allowExposed
private final boolean allowExposedShould password be allowed if it is found in the API results. -
allowOnException
private final boolean allowOnExceptionShould password be allowed if API calls throw exceptions. -
connectTimeout
Maximum waiting time for established connection. Default is 5 seconds. -
readTimeout
Maximum waiting time for reading all data. Default is 30 seconds.
-
-
Constructor Details
-
HaveIBeenPwnedRule
Create the rule, appName is required by the API.- Parameters:
appName- must not be null
-
HaveIBeenPwnedRule
Create the rule, appName is required by the API.- Parameters:
appName- must not be nulladdress- the URL must end with a/.
-
HaveIBeenPwnedRule
public HaveIBeenPwnedRule(String appName, String address, boolean allowExposed, boolean allowOnException) Create the rule, appName is required by the API.- Parameters:
appName- must not be nulladdress- the URL must end with a/.allowExposed- false: the rule does not allow previously pwned passwords, true: pwned passwords are allowed, but the number of matches is returned in the result.allowOnException- true: if the API is not accessible, any password is accepted. false: Default, API must answer in time to allow the password.
-
-
Method Details
-
setConnectTimeout
maximum Duration for connecting to the API.- Parameters:
timeout- for connecting.
-
setReadTimeout
maximum Duration for reading from the API.- Parameters:
timeout- for reading.
-
validate
Description copied from interface:RuleValidates the supplied password data per the requirements of this rule. -
toString
-
searchResponse
Reads the supplied reader line by line until a match is found against the supplied hex digest.- Parameters:
hexDigest- to matchreader- to read- Returns:
- rule result whose validity is determined by whether a match was found
- Throws:
IOException- if an error occurs reading from the reader
-
getHexDigest
Returns an uppercase, hex encoded, SHA1 hash of the password.- Parameters:
passwordData- to hash- Returns:
- hex encoded hash
-
openApiConnectionForRange
Opens a connection to the API and returns a reader for the input stream.- Parameters:
range- to request from the API- Returns:
- reader for the connection
- Throws:
IOException- if a connection cannot be opened to the API
-