private <T> boolean genericAuthenticate(Class<T> type, String username, T credential) { for (AuthenticationPlugin authenticationPlugin : authenticationPlugins) { if (authenticationPlugin.getCredentialType().equals(type)) { try { log.log(Level.FINE, "Using authentication plugin " + authenticationPlugin + " to authenticate user " + username); @SuppressWarnings("unchecked") AuthenticationPlugin<T> authPlugin = (AuthenticationPlugin<T>) authenticationPlugin; if (authPlugin.authenticate(username, credential)) { return true; } } catch (Exception e) { log.log(Level.SEVERE, "Exception authenticating user " + username + " in authentication plugin: " + authenticationPlugin, e); } } } return false; } }
@Override public void init() { PluginContext context = getContext(); this.username = context.getProperty(SIMPLE_USERNAME); this.password = context.getProperty(SIMPLE_PASSWORD); }
@Override public void init() { String authorizedKeyPath = getContext().getProperty(AUTHORIZED_KEY_PATH); if (authorizedKeyPath != null) { File f = new File(authorizedKeyPath); if (f.exists() && f.isFile()) { log.log(Level.FINE, "Found authorized key path " + authorizedKeyPath); Set<PublicKey> keys; keys = new LinkedHashSet<PublicKey>(); KeyPairProvider provider = new FilePublicKeyProvider(new String[]{authorizedKeyPath}); for (String type : TYPES) { KeyPair pair = provider.loadKey(type); if (pair != null) { PublicKey key = pair.getPublic(); if (key != null) { keys.add(key); } } } authorizedKeys = keys; } else { log.log(Level.FINE, "Ignoring invalid authorized key path " + authorizedKeyPath); } } }
@Test public void test() { String pubKeyFile = Thread.currentThread().getContextClassLoader().getResource("test_authorized_key.pem").getFile(); assertTrue(new File(pubKeyFile).exists()); FilePublicKeyProvider SUT = new FilePublicKeyProvider(new String[]{pubKeyFile}); assertTrue(SUT.loadKeys().iterator().hasNext()); }
public boolean authenticate(String username, PublicKey key, ServerSession session) { try { log.log(Level.FINE, "Using authentication plugin " + authentication + " to authenticate user " + username); return keyAuthentication.authenticate(username, key); } catch (Exception e) { log.log(Level.SEVERE, "Exception authenticating user " + username + " in authentication plugin: " + authentication, e); return false; } } });
public boolean authenticate(final String username, final String password) throws Exception { String domain = getContext().getProperty(JAAS_DOMAIN); if (domain != null) { log.log(Level.FINE, "Will use the JAAS domain '" + domain + "' for authenticating user " + username);
if (server.getPasswordAuthenticator() == null && authenticationPlugin.getCredentialType().equals(String.class)) { server.setPasswordAuthenticator(new PasswordAuthenticator() { public boolean authenticate(String _username, String _password, ServerSession session) { if (server.getPublickeyAuthenticator() == null && authenticationPlugin.getCredentialType().equals(PublicKey.class)) { server.setPublickeyAuthenticator(new PublickeyAuthenticator() { public boolean authenticate(String username, PublicKey key, ServerSession session) {
if (authentication != null) { for (AuthenticationPlugin authenticationPlugin : getContext().getPlugins(AuthenticationPlugin.class)) { if (authentication.contains(authenticationPlugin.getName())) { authPlugins.add(authenticationPlugin);
public Iterable<KeyPair> loadKeys() { if (!SecurityUtils.isBouncyCastleRegistered()) { throw new IllegalStateException("BouncyCastle must be registered as a JCE provider"); } List<KeyPair> keys = new ArrayList<KeyPair>(); for (String file : files) { try { Object o = KeyPairUtils.readKey(new InputStreamReader(new FileInputStream(file))); if (o instanceof KeyPair) { keys.add(new KeyPair(((KeyPair)o).getPublic(), null)); } else if (o instanceof PublicKey) { keys.add(new KeyPair((PublicKey)o, null)); } else if (o instanceof PEMKeyPair) { PEMKeyPair keyPair = (PEMKeyPair)o; keys.add(convertPemKeyPair(keyPair)); } else if (o instanceof SubjectPublicKeyInfo) { PEMKeyPair keyPair = new PEMKeyPair((SubjectPublicKeyInfo) o, null); keys.add(convertPemKeyPair(keyPair)); } else { throw new UnsupportedOperationException(String.format("Key type %s not supported.", o.getClass().getName())); } } catch (Exception e) { LOG.info("Unable to read key {}: {}", file, e); } } return keys; }
@Before public void setUp() throws Exception { // int port = PORTS.getAndIncrement(); // // IOHandler handler = new IOHandler(); SimpleAuthenticationPlugin auth = new SimpleAuthenticationPlugin(); // Foo foo = new Foo(); TestPluginLifeCycle lifeCycle = new TestPluginLifeCycle(new SSHPlugin(), foo, auth); lifeCycle.setProperty(SSHPlugin.SSH_PORT, port); lifeCycle.setProperty(SSHPlugin.SSH_SERVER_IDLE_TIMEOUT, 10 * 60 * 1000); lifeCycle.setProperty(SSHPlugin.SSH_SERVER_AUTH_TIMEOUT, 10 * 60 * 1000); lifeCycle.setProperty(SSHPlugin.SSH_ENCODING, Utils.UTF_8); lifeCycle.setProperty(AuthenticationPlugin.AUTH, Arrays.asList(auth.getName())); lifeCycle.setProperty(SimpleAuthenticationPlugin.SIMPLE_USERNAME, "root"); lifeCycle.setProperty(SimpleAuthenticationPlugin.SIMPLE_PASSWORD, ""); lifeCycle.start(); SSHClient client = new SSHClient(port).connect(); // // this.handler = handler; this.client = client; this.lifeCycle = lifeCycle; this.foo = foo; }
public boolean authenticate(String _username, String _password, ServerSession session) throws PasswordChangeRequiredException { AuthInfo authInfo = genericAuthenticate(String.class, _username, _password); if (authInfo.isSuccessful()) { // We store username and password in session for later reuse session.setAttribute(USERNAME, _username); session.setAttribute(PASSWORD, _password); session.setAttribute(AUTH_INFO, authInfo); return true; } else { return false; } } });
@Override public void init() { String authorizedKeyPath = getContext().getProperty(AUTHORIZED_KEY_PATH); if (authorizedKeyPath != null) { File f = new File(authorizedKeyPath); if (f.exists() && f.isFile()) { log.log(Level.FINE, "Found authorized key path " + authorizedKeyPath); Set<PublicKey> keys; keys = new LinkedHashSet<PublicKey>(); KeyPairProvider provider = new FilePublicKeyProvider(new String[]{authorizedKeyPath}); for (String type : TYPES) { KeyPair pair = provider.loadKey(type); if (pair != null) { PublicKey key = pair.getPublic(); if (key != null) { keys.add(key); } } } authorizedKeys = keys; } else { log.log(Level.FINE, "Ignoring invalid authorized key path " + authorizedKeyPath); } } }
private <T> AuthInfo genericAuthenticate(Class<T> type, String username, T credential) { for (AuthenticationPlugin authenticationPlugin : authenticationPlugins) { if (authenticationPlugin.getCredentialType().equals(type)) { try { log.log(Level.FINE, "Using authentication plugin " + authenticationPlugin + " to authenticate user " + username); @SuppressWarnings("unchecked") AuthenticationPlugin<T> authPlugin = (AuthenticationPlugin<T>) authenticationPlugin; return authPlugin.authenticate(username, credential); } catch (Exception e) { log.log(Level.SEVERE, "Exception authenticating user " + username + " in authentication plugin: " + authenticationPlugin, e); } } } return AuthInfo.UNSUCCESSFUL; } }
@Override public void init() { PluginContext context = getContext(); this.username = context.getProperty(SIMPLE_USERNAME); this.password = context.getProperty(SIMPLE_PASSWORD); }
public boolean authenticate(String _username, String _password, ServerSession session) { boolean auth; try { log.log(Level.FINE, "Using authentication plugin " + authentication + " to authenticate user " + _username); auth = passwordAuthentication.authenticate(_username, _password); } catch (Exception e) { log.log(Level.SEVERE, "Exception authenticating user " + _username + " in authentication plugin: " + authentication, e); return false; } // We store username and password in session for later reuse session.setAttribute(USERNAME, _username); session.setAttribute(PASSWORD, _password); // return auth; } });
@Override public void init() { String authorizedKeyPath = getContext().getProperty(AUTHORIZED_KEY_PATH); if (authorizedKeyPath != null) { File f = new File(authorizedKeyPath); if (f.exists() && f.isFile()) { log.log(Level.FINE, "Found authorized key path " + authorizedKeyPath); Set<PublicKey> keys; keys = new LinkedHashSet<PublicKey>(); KeyPairProvider provider = new FilePublicKeyProvider(new String[]{authorizedKeyPath}); for (String type : TYPES) { KeyPair pair = provider.loadKey(type); if (pair != null) { PublicKey key = pair.getPublic(); if (key != null) { keys.add(key); } } } authorizedKeys = keys; } else { log.log(Level.FINE, "Ignoring invalid authorized key path " + authorizedKeyPath); } } }
private <T> boolean genericAuthenticate(Class<T> type, String username, T credential) { for (AuthenticationPlugin authenticationPlugin : authenticationPlugins) { if (authenticationPlugin.getCredentialType().equals(type)) { try { log.log(Level.FINE, "Using authentication plugin " + authenticationPlugin + " to authenticate user " + username); @SuppressWarnings("unchecked") AuthenticationPlugin<T> authPlugin = (AuthenticationPlugin<T>) authenticationPlugin; if (authPlugin.authenticate(username, credential)) { return true; } } catch (Exception e) { log.log(Level.SEVERE, "Exception authenticating user " + username + " in authentication plugin: " + authenticationPlugin, e); } } } return false; } }
@Override public void init() { PluginContext context = getContext(); this.username = context.getProperty(SIMPLE_USERNAME); this.password = context.getProperty(SIMPLE_PASSWORD); }
@Override public void init() { String authorizedKeyPath = getContext().getProperty(AUTHORIZED_KEY_PATH); if (authorizedKeyPath != null) { File f = new File(authorizedKeyPath); if (f.exists() && f.isFile()) { log.log(Level.FINE, "Found authorized key path " + authorizedKeyPath); Set<PublicKey> keys; keys = new LinkedHashSet<PublicKey>(); KeyPairProvider provider = new FilePublicKeyProvider(new String[]{authorizedKeyPath}); for (String type : TYPES) { KeyPair pair = provider.loadKey(type); if (pair != null) { PublicKey key = pair.getPublic(); if (key != null) { keys.add(key); } } } authorizedKeys = keys; } else { log.log(Level.FINE, "Ignoring invalid authorized key path " + authorizedKeyPath); } } }
@Override public void init() { PluginContext context = getContext(); this.username = context.getProperty(SIMPLE_USERNAME); this.password = context.getProperty(SIMPLE_PASSWORD); }