public int authenticate(AuthenticationProtocolServer authentication,
SshMsgUserAuthRequest msg) throws IOException {
//, Map nativeSettings)
NativeAuthenticationProvider authImpl = NativeAuthenticationProvider.getInstance();
if (authImpl == null) {
log.error(
"Cannot perfrom authentication witout native authentication provider");
return AuthenticationProtocolState.FAILED;
}
authentication.registerMessage(SshMsgUserAuthInfoResponse.SSH_MSG_USERAUTH_INFO_RESPONSE,
SshMsgUserAuthInfoResponse.class);
SshMsgUserAuthInfoRequest info = new SshMsgUserAuthInfoRequest("Password authentication",
"", "");
info.addPrompt(msg.getUsername() + "'s password", false);
authentication.sendMessage(info);
SshMessage response = authentication.readMessage();
if (response instanceof SshMsgUserAuthInfoResponse) {
String[] responses = ((SshMsgUserAuthInfoResponse) response).getResponses();
if (responses.length == 1) {
String password = responses[0];
try {
if (authImpl.logonUser(msg.getUsername(), password)) { //, nativeSettings)) {
log.info(msg.getUsername() +
" has passed password authentication");
return AuthenticationProtocolState.COMPLETE;
} else {
log.info(msg.getUsername() +
" has failed password authentication");
return AuthenticationProtocolState.FAILED;
}
} catch (PasswordChangeException ex) {
info = new SshMsgUserAuthInfoRequest("Password change required",
"", "");
info.addPrompt("New password", false);
info.addPrompt("Confirm password", false);
authentication.sendMessage(info);
response = authentication.readMessage();
if (response instanceof SshMsgUserAuthInfoResponse) {
responses = ((SshMsgUserAuthInfoResponse) response).getResponses();
if (responses.length == 2) {
if (responses[0].equals(responses[1])) {
if (authImpl.changePassword(msg.getUsername(),
password, responses[0])) {
return AuthenticationProtocolState.COMPLETE;
} else {
return AuthenticationProtocolState.FAILED;
}
} else {
return AuthenticationProtocolState.FAILED;
}
} else {
log.error("Client replied with an invalid message " +
response.getMessageName());
return AuthenticationProtocolState.FAILED;
}
} else {
log.error("Client replied with an invalid message " +
response.getMessageName());
return AuthenticationProtocolState.FAILED;
}
}
} else {
log.error("Client responded with too many values!");
return AuthenticationProtocolState.FAILED;
}
} else {
log.error("Client replied with an invalid message " +
response.getMessageName());
return AuthenticationProtocolState.FAILED;
}
}
|