public class PublicKeyAuthentication extends Object implements SshAuthentication
Public key based authentication implementation. Public-key authentication uses public-key cryptography to verify the client's identity. To access an account on an SSH server machine, the client proves that it possesses a secret key. A key is authorized if its public component is contained in the accounts authorization file (typically ~/.ssh/authorized_keys).
This class implements a basic publickey SshAuthentication that can be passed into the SshClient to authenticate. As a username is required to establish a connection it is not required that it be set on the authentication object, however if you wish to change the username you can do so (this may not be allowed by some server implementations).
First you need to load a public/private key pair; a set of utility classes based on the SSHTools public key formats is available in the com.sshtools.publickey package. These currently allow SSHTools, OpenSSH and SSH1 private keys to be read.
FileInputStream in = new FileInputStream("someprivatekey"); ByteArrayOutputStream out = new ByteArrayOutputStream(); int read; while((read = in.read()) > -1) out.write(read); in.close(); SshPrivateKeyFile pkf = SshPrivateKeyFileFactory.parse(out.toByteArray()); SshKeyPair pair = pkf.toKeyPair("mypassphrase"); PublicKeyAuthentication pk = new PublicKeyAuthentication(); pk.setPrivateKey(pair.getPrivateKey()); pk.setPublicKey(pair.getPublicKey()); if(ssh.authenticate(pk)==SshAuthentication.COMPLETE) System.out.println("Authentication completed");
Modifier and Type | Field and Description |
---|---|
protected boolean |
authenticating |
protected SshPrivateKey |
privatekey |
protected SshPublicKey |
publickey |
protected String |
username |
CANCELLED, COMPLETE, FAILED, FURTHER_AUTHENTICATION_REQUIRED, PUBLIC_KEY_ACCEPTABLE
Constructor and Description |
---|
PublicKeyAuthentication() |
PublicKeyAuthentication(File identityFile,
String passphrase) |
PublicKeyAuthentication(InputStream identityFile,
String passphrase) |
PublicKeyAuthentication(SshKeyPair pair) |
PublicKeyAuthentication(String identityFile,
String passphrase) |
Modifier and Type | Method and Description |
---|---|
String |
getMethod()
The SSH authentication method name
|
SshPrivateKey |
getPrivateKey()
Get the private key for this authentication.
|
SshPublicKey |
getPublicKey()
Get the public key for this authentication.
|
String |
getUsername()
Get the username for this authentication attempt.
|
boolean |
isAuthenticating()
Is the authentication attempt actually going to perform an authentication or
are we simply just checking the suitability of a public key.
|
void |
setAuthenticating(boolean authenticating)
If true the authentication will proceed as normal and the result will either
be a success or failure.
|
void |
setPrivateKey(SshPrivateKey privatekey)
Set the private key for this authentication.
|
void |
setPublicKey(SshPublicKey publickey)
Set the public key for this authentication.
|
void |
setUsername(String username)
Set the username for this authentication attempt.
|
protected String username
protected SshPrivateKey privatekey
protected SshPublicKey publickey
protected boolean authenticating
public PublicKeyAuthentication()
public PublicKeyAuthentication(SshKeyPair pair)
public PublicKeyAuthentication(File identityFile, String passphrase) throws IOException, InvalidPassphraseException
public PublicKeyAuthentication(InputStream identityFile, String passphrase) throws IOException, InvalidPassphraseException
public PublicKeyAuthentication(String identityFile, String passphrase) throws IOException, InvalidPassphraseException
public void setUsername(String username)
SshAuthentication
setUsername
in interface SshAuthentication
public String getUsername()
SshAuthentication
getUsername
in interface SshAuthentication
public void setPrivateKey(SshPrivateKey privatekey)
privatekey
- public String getMethod()
SshAuthentication
getMethod
in interface SshAuthentication
public SshPrivateKey getPrivateKey()
public void setPublicKey(SshPublicKey publickey)
publickey
- public SshPublicKey getPublicKey()
public void setAuthenticating(boolean authenticating)
authenticating
- public boolean isAuthenticating()
Copyright © 2024. All rights reserved.