| Method from com.sshtools.j2ssh.authentication.PublicKeyAuthenticationClient Detail: |
public boolean acceptsKey(AuthenticationProtocolClient authentication,
String username,
String serviceToStart,
SshPublicKey key) throws IOException {
authentication.registerMessage(SshMsgUserAuthPKOK.class,
SshMsgUserAuthPKOK.SSH_MSG_USERAUTH_PK_OK);
log.info(
"Determining if server can accept public key for authentication");
ByteArrayWriter baw = new ByteArrayWriter();
// Now prepare and send the message
baw.write(0);
baw.writeString(key.getAlgorithmName());
baw.writeBinaryString(key.getEncoded());
SshMessage msg = new SshMsgUserAuthRequest(username, serviceToStart,
getMethodName(), baw.toByteArray());
authentication.sendMessage(msg);
try {
msg = authentication.readMessage(SshMsgUserAuthPKOK.SSH_MSG_USERAUTH_PK_OK);
if (msg instanceof SshMsgUserAuthPKOK) {
return true;
} else {
throw new IOException(
"Unexpected message returned from readMessage");
}
} catch (TerminatedStateException ex) {
return false;
}
}
|
public void authenticate(AuthenticationProtocolClient authentication,
String serviceToStart) throws TerminatedStateException, IOException {
if ((getUsername() == null) || (key == null)) {
throw new AuthenticationProtocolException(
"You must supply a username and a key");
}
ByteArrayWriter baw = new ByteArrayWriter();
log.info("Generating data to sign");
SshPublicKey pub = key.getPublicKey();
log.info("Preparing public key authentication request");
// Now prepare and send the message
baw.write(1);
baw.writeString(pub.getAlgorithmName());
baw.writeBinaryString(pub.getEncoded());
// Create the signature data
ByteArrayWriter data = new ByteArrayWriter();
data.writeBinaryString(authentication.getSessionIdentifier());
data.write(SshMsgUserAuthRequest.SSH_MSG_USERAUTH_REQUEST);
data.writeString(getUsername());
data.writeString(serviceToStart);
data.writeString(getMethodName());
data.write(1);
data.writeString(pub.getAlgorithmName());
data.writeBinaryString(pub.getEncoded());
// Generate the signature
baw.writeBinaryString(key.generateSignature(data.toByteArray()));
SshMsgUserAuthRequest msg = new SshMsgUserAuthRequest(getUsername(),
serviceToStart, getMethodName(), baw.toByteArray());
authentication.sendMessage(msg);
}
|
public boolean canAuthenticate() {
return ((getUsername() != null) && (key != null));
}
|
public String getKeyfile() {
return privateKeyFile;
}
|
public String getMethodName() {
return "publickey";
}
|
public Properties getPersistableProperties() {
Properties properties = new Properties();
if (getUsername() != null) {
properties.setProperty("Username", getUsername());
}
if (privateKeyFile != null) {
properties.setProperty("PrivateKey", privateKeyFile);
}
return properties;
}
|
public void reset() {
privateKeyFile = null;
passphrase = null;
}
|
public void setKey(SshPrivateKey key) {
this.key = key;
}
|
public void setKeyfile(String privateKeyFile) {
this.privateKeyFile = privateKeyFile;
}
|
public void setPersistableProperties(Properties properties) {
setUsername(properties.getProperty("Username"));
if (properties.getProperty("PrivateKey") != null) {
privateKeyFile = properties.getProperty("PrivateKey");
}
if (properties.getProperty("Passphrase") != null) {
passphrase = properties.getProperty("Passphrase");
}
}
|