Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public class DelegatingPasswordEncoder

  1. Object
  2. DelegatingPasswordEncoder

ImplementsPasswordEncoder

Reads which scheme a stored password was made with from the {id} in front of it, so one user store can hold passwords of several ages:

{pbkdf2-sha256}pbkdf2$210000$3q2-7w$kZ...
{bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG
{noop}password

New passwords are encoded with the scheme named at construction. A stored password of any other scheme reports upgradeEncoding, which is what makes a sign-in re-encode it.

Constructors

public DelegatingPasswordEncoder(String idForEncode, Map<String, PasswordEncoder> idToPasswordEncoder)

Methods

public synchronized void setDefaultPasswordEncoderForMatches(PasswordEncoder encoder)The encoder a stored password with no {id}, or one this encoder does not know, is checked with.
public String encode(CharSequence rawPassword)The password as it should be stored: salted and hashed, so encoding one password twice gives two different results.
public boolean matches(CharSequence rawPassword, String prefixEncodedPassword)Whether rawPassword is the password encodedPassword was made from.
public boolean isSupported(String prefixEncodedPassword)Whether prefixEncodedPassword names an id this encoder has a delegate for.
public boolean upgradeEncoding(String prefixEncodedPassword)Whether encodedPassword should be encoded again for better protection: it was made by an older scheme, or with fewer rounds than this encoder uses now.

Inherited methods

Constructor details

DelegatingPasswordEncoder

public DelegatingPasswordEncoder(String idForEncode, Map<String, PasswordEncoder> idToPasswordEncoder)

Parameters

idForEncode String
the scheme new passwords are encoded with
idToPasswordEncoder Map<String, PasswordEncoder>
every scheme a stored password may name

Method details

setDefaultPasswordEncoderForMatches

public synchronized void setDefaultPasswordEncoderForMatches(PasswordEncoder encoder)
The encoder a stored password with no {id}, or one this encoder does not know, is checked with. Unless set, such a password is refused with an exception saying so: set this to the scheme of a store that predates the prefixes.

encode

public String encode(CharSequence rawPassword)
The password as it should be stored: salted and hashed, so encoding one password twice gives two different results.

matches

public boolean matches(CharSequence rawPassword, String prefixEncodedPassword)
Whether rawPassword is the password encodedPassword was made from.

isSupported

public boolean isSupported(String prefixEncodedPassword)
Whether prefixEncodedPassword names an id this encoder has a delegate for. A value it does not is refused by matches(CharSequence, String) through the default for matches, which by default throws; a migration asks this first, so it can report the row instead of importing a password nobody can sign in with.

upgradeEncoding

public boolean upgradeEncoding(String prefixEncodedPassword)
Whether encodedPassword should be encoded again for better protection: it was made by an older scheme, or with fewer rounds than this encoder uses now.