Class RecoveryCodeService
Recovery codes: what signs a user in when their authenticator app is gone.
generate(String) makes ten, to be shown to the user once and never again -- the
server keeps a salted PBKDF2 password hash of each and cannot show them a second time. Each
works once, in place of a one-time code at sign-in. Generating again
replaces whatever was left.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRecoveryCodeService(RecoveryCodeRepository repository) A service whose codes are hashed bydefaultEncoder().RecoveryCodeService(RecoveryCodeRepository repository, PasswordEncoder encoder) A service whose codes are hashed and verified byencoder, for codes imported from a system that stored them another way. -
Method Summary
Modifier and TypeMethodDescriptionbooleanUses a code up.static PasswordEncoderThe encoder recovery codes use unless another is given: aDelegatingPasswordEncoderthat hashes new codes as{pbkdf2-sha256}, and verifies a stored hash by the{id}in front of it --{pbkdf2-sha256}, or{sha256}for the hexadecimal SHA-256 of the code another system may have stored (Sha256HexPasswordEncoder).MakesCOUNTnew codes forusername, replacing any they had.static booleanisCodeShaped(String code) Whethercodeis written as a recovery code is: ten letters and digits, with or without the dash.intHow many codesusernamehas left.
-
Field Details
-
COUNT
-
-
Constructor Details
-
RecoveryCodeService
A service whose codes are hashed bydefaultEncoder(). -
RecoveryCodeService
A service whose codes are hashed and verified by
encoder, for codes imported from a system that stored them another way.Parameters
repository: where the hashes are keptencoder: hashes new codes and verifies stored ones
-
-
Method Details
-
defaultEncoder
The encoder recovery codes use unless another is given: a
DelegatingPasswordEncoderthat hashes new codes as{pbkdf2-sha256}, and verifies a stored hash by the{id}in front of it --{pbkdf2-sha256}, or{sha256}for the hexadecimal SHA-256 of the code another system may have stored (Sha256HexPasswordEncoder). A hash with no{id}is PBKDF2, as every code this service made before ids were written.A code is compared in the normalized form a user's input is reduced to: lower case, without
-or spaces. Codes imported as{sha256}must be hashed from that form. -
generate
-
consume
Uses a code up. At most four verifications run in this process, and at most one per username (case insensitive). A busy verifier returns false immediately; it does not consume a code or queue password-hashing work.- Returns:
- whether
codewas one ofusername's unused codes
-
isCodeShaped
Whethercodeis written as a recovery code is: ten letters and digits, with or without the dash. It says nothing about whether anybody has that code -- only that it is not something else, such as the digits of a one-time code. -
remaining
How many codesusernamehas left.
-