com.maverick.ssh
Class PublicKeyAuthentication

java.lang.Object
  extended bycom.maverick.ssh.PublicKeyAuthentication
All Implemented Interfaces:
SshAuthentication
Direct Known Subclasses:
Ssh2PublicKeyAuthentication

public class PublicKeyAuthentication
extends java.lang.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 provide for the reading of SSHTools, OpenSSH and SSH1 private keys.

 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");
 

Author:
Lee David Painter

Field Summary
 
Fields inherited from interface com.maverick.ssh.SshAuthentication
CANCELLED, COMPLETE, FAILED, FURTHER_AUTHENTICATION_REQUIRED, PUBLIC_KEY_ACCEPTABLE
 
Constructor Summary
PublicKeyAuthentication()
           
 
Method Summary
 java.lang.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.
 java.lang.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(java.lang.String username)
          Set the username for this authentication attempt.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PublicKeyAuthentication

public PublicKeyAuthentication()
Method Detail

setUsername

public void setUsername(java.lang.String username)
Description copied from interface: SshAuthentication
Set the username for this authentication attempt.

Specified by:
setUsername in interface SshAuthentication
Parameters:
username -

getUsername

public java.lang.String getUsername()
Description copied from interface: SshAuthentication
Get the username for this authentication attempt.

Specified by:
getUsername in interface SshAuthentication
Returns:
the username used.

setPrivateKey

public void setPrivateKey(SshPrivateKey privatekey)
Set the private key for this authentication.

Parameters:
privatekey -

getMethod

public java.lang.String getMethod()
Description copied from interface: SshAuthentication
The SSH authentication method name

Specified by:
getMethod in interface SshAuthentication
Returns:
String

getPrivateKey

public SshPrivateKey getPrivateKey()
Get the private key for this authentication.

Returns:

setPublicKey

public void setPublicKey(SshPublicKey publickey)
Set the public key for this authentication.

Parameters:
publickey -

getPublicKey

public SshPublicKey getPublicKey()
Get the public key for this authentication.

Returns:

setAuthenticating

public void setAuthenticating(boolean authenticating)
If true the authentication will proceed as normal and the result will either be a success or failure. If false the authentication result will be either PUBLIC_KEY_ACCEPTABLE or a failure. If the result returned is PUBLIC_KEY_ACCEPTABLE the authentication can be completed by setting this flag to true and re-authenticating with the SshClient.

Parameters:
authenticating -

isAuthenticating

public boolean isAuthenticating()
Is the authentication attempt actually going to perform an authentication or are we simply just checking the suitability of a public key.

Returns:


Copyright © 2003 3SP LTD. All Rights Reserved.