Change Password
This workflow can take place when the end user wants/needs to change their password as part of the standard application usage.
Password change can be mandated at regular intervals if the password policy specifies a maxAge value for instance.
Perform the usual steps to get the Container instance.
- Create an instance of the Device (DeviceFactory.getDevice).
- Get the instance of the Container (Device.findContainers).
- At this point, depending on the server configuration, either:
- Get the container policy (Container.getProtectionPolicy).
- Find the key whose password needs to be changed (Container.findKeys) and its protection policy (Key.getProtectionPolicy).
- Prompt the end user for the old and new passwords, and change it (PasswordPolicy.changePassword).
- If the current password is correct and the new password matches the Protection Policy, then the operation is successful and the password is changed. Otherwise, an error is returned/thrown.
Or
// You can check the policy protecting the container, or alternatively a policy protecting a key.
// Unless a specific configuration is used, they will be the same.
var containerPolicy: ProtectionPolicy? = null
try {
containerPolicy = currentContainer.protectionPolicy
} catch (e: UnsupportedDeviceException) {
e.printStackTrace()
} catch (e: InternalException) {
e.printStackTrace()
} catch (e: LostCredentialsException) {
e.printStackTrace()
}
// ChangePassword operation only applies to PASSWORD or BIOPASSWORD
if (containerPolicy!!.type == ProtectionPolicy.PolicyType.BIOPASSWORD.toString() ||
containerPolicy!!.type == ProtectionPolicy.PolicyType.PASSWORD.toString() ) {
val PasswordPolicy = containerPolicy as PasswordPolicy?
try {
PasswordPolicy?.changePassword(oldPassword, newPassword)
} catch (ex: Exception) {
when(ex) {
is AuthenticationException -> { // Old Password is incorrect
ex.printStackTrace()
}
is InvalidPasswordException -> { // New Password doesn't meet policy requirements.
ex.printStackTrace()
}
is LostCredentialsException, is InternalException, is FingerprintAuthenticationRequiredException, is UnsupportedDeviceException, is FingerprintNotEnrolledException, is PasswordRequiredException, is InvalidParameterException , is PasswordNotYetUpdatableException -> {
ex.printStackTrace()
}
else -> throw ex
}
}
}
ProtectionPolicy policy = null;
boolean result = false;
// You can check the policy protecting the container, or alternatively a policy protecting a key.
// Unless a specific configuration is used, they will be the same.
try {
policy = currentContainer.getProtectionPolicy();
} catch (UnsupportedDeviceException e) {
e.printStackTrace();
} catch (InternalException e) {
e.printStackTrace();
} catch (LostCredentialsException e) {
e.printStackTrace();
}
// ChangePassword operation only applies to PASSWORD or BIOPASSWORD
if (policy.getType() == ProtectionPolicy.PolicyType.BIOPASSWORD.toString() ||
policy.getType() == ProtectionPolicy.PolicyType.PASSWORD.toString()) {
try {
result = ((PasswordPolicy) policy).changePassword(oldPassword.toCharArray(), newPassword.toCharArray());
Log.d(LOG_TAG,"Password changed successfully");
} catch (AuthenticationException e) { // Old Password is incorrect
e.printStackTrace();
} catch (InvalidPasswordException e) { // New Password doesn't meet policy requirements.
e.printStackTrace();
} catch (LostCredentialsException | InternalException | FingerprintAuthenticationRequiredException | UnsupportedDeviceException | FingerprintNotEnrolledException | PasswordRequiredException | InvalidParameterException | PasswordNotYetUpdatableException e) {
e.printStackTrace();
}
}