mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 15:20:54 +03:00
keybased auth cleanups
This commit is contained in:
@@ -20,7 +20,6 @@ import net.schmizz.sshj.common.KeyType;
|
||||
import net.schmizz.sshj.userauth.password.PasswordFinder;
|
||||
import net.schmizz.sshj.userauth.password.PasswordUtils;
|
||||
import net.schmizz.sshj.userauth.password.PrivateKeyFileResource;
|
||||
import net.schmizz.sshj.userauth.password.Resource;
|
||||
import org.bouncycastle.openssl.EncryptionException;
|
||||
import org.bouncycastle.openssl.PEMReader;
|
||||
import org.slf4j.Logger;
|
||||
@@ -53,8 +52,7 @@ public class PKCS8KeyFile
|
||||
|
||||
protected final Logger log = LoggerFactory.getLogger(getClass());
|
||||
protected PasswordFinder pwdf;
|
||||
protected File location;
|
||||
protected Resource resource;
|
||||
protected PrivateKeyFileResource resource;
|
||||
protected KeyPair kp;
|
||||
|
||||
protected KeyType type;
|
||||
@@ -82,8 +80,7 @@ public class PKCS8KeyFile
|
||||
@Override
|
||||
public void init(File location) {
|
||||
assert location != null;
|
||||
this.location = location;
|
||||
resource = new PrivateKeyFileResource(location.getAbsolutePath());
|
||||
resource = new PrivateKeyFileResource(location.getAbsoluteFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -114,7 +111,7 @@ public class PKCS8KeyFile
|
||||
for (; ;) {
|
||||
// while the PasswordFinder tells us we should retry
|
||||
try {
|
||||
r = new PEMReader(new InputStreamReader(new FileInputStream(location)), pFinder);
|
||||
r = new PEMReader(new InputStreamReader(new FileInputStream(resource.getDetail())), pFinder);
|
||||
o = r.readObject();
|
||||
} catch (EncryptionException e) {
|
||||
if (pwdf.shouldRetry(resource))
|
||||
@@ -131,7 +128,7 @@ public class PKCS8KeyFile
|
||||
}
|
||||
|
||||
if (o == null)
|
||||
throw new IOException("Could not read key pair from: " + location);
|
||||
throw new IOException("Could not read key pair from: " + resource);
|
||||
if (o instanceof KeyPair)
|
||||
kp = (KeyPair) o;
|
||||
else
|
||||
@@ -139,4 +136,8 @@ public class PKCS8KeyFile
|
||||
return kp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PKCS8KeyFile{resource=" + resource + "}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,6 @@ public abstract class AbstractAuthMethod
|
||||
.putString(params.getUsername()) // username goes first
|
||||
.putString(params.getNextServiceName()) // the service that we'd like on success
|
||||
.putString(name); // name of auth method
|
||||
|
||||
}
|
||||
|
||||
protected AccountResource makeAccountResource() {
|
||||
|
||||
@@ -21,8 +21,6 @@ import net.schmizz.sshj.transport.TransportException;
|
||||
import net.schmizz.sshj.userauth.UserAuthException;
|
||||
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Implements the {@code "publickey"} SSH authentication method.
|
||||
* <p/>
|
||||
@@ -60,11 +58,7 @@ public class AuthPublickey
|
||||
*/
|
||||
private SSHPacket buildReq(boolean signed)
|
||||
throws UserAuthException {
|
||||
try {
|
||||
kProv.getPublic();
|
||||
} catch (IOException ioe) {
|
||||
throw new UserAuthException("Problem getting public key", ioe);
|
||||
}
|
||||
log.debug("Attempting authentication using {}", kProv);
|
||||
return putPubKey(super.buildReq().putBoolean(signed));
|
||||
}
|
||||
|
||||
@@ -76,7 +70,7 @@ public class AuthPublickey
|
||||
*/
|
||||
private void sendSignedReq()
|
||||
throws UserAuthException, TransportException {
|
||||
log.debug("Sending signed request");
|
||||
log.debug("Key acceptable, sending signed request");
|
||||
params.getTransport().write(putSig(buildReq(true)));
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ public abstract class KeyedAuthMethod
|
||||
try {
|
||||
key = kProv.getPublic();
|
||||
} catch (IOException ioe) {
|
||||
throw new UserAuthException("Problem getting public key", ioe);
|
||||
throw new UserAuthException("Problem getting public key from " + kProv, ioe);
|
||||
}
|
||||
|
||||
// public key as 2 strings: [ key type | key blob ]
|
||||
@@ -59,7 +59,7 @@ public abstract class KeyedAuthMethod
|
||||
try {
|
||||
key = kProv.getPrivate();
|
||||
} catch (IOException ioe) {
|
||||
throw new UserAuthException("Problem getting private key", ioe);
|
||||
throw new UserAuthException("Problem getting private key from " + kProv, ioe);
|
||||
}
|
||||
|
||||
final String kt = KeyType.fromKey(key).toString();
|
||||
|
||||
@@ -15,11 +15,13 @@
|
||||
*/
|
||||
package net.schmizz.sshj.userauth.password;
|
||||
|
||||
public class PrivateKeyFileResource
|
||||
extends Resource<String> {
|
||||
import java.io.File;
|
||||
|
||||
public PrivateKeyFileResource(String path) {
|
||||
super(path);
|
||||
public class PrivateKeyFileResource
|
||||
extends Resource<File> {
|
||||
|
||||
public PrivateKeyFileResource(File privateKeyFile) {
|
||||
super(privateKeyFile);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user