Java Code Examples for org.apache.sshd.client.future.AuthFuture

The following examples show how to use org.apache.sshd.client.future.AuthFuture. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: termd   Source File: ClientUserAuthServiceOld.java    License: Apache License 2.0 5 votes vote down vote up
public AuthFuture auth(UserAuth userAuth) throws IOException {
    log.debug("Trying authentication with {}", userAuth);
    synchronized (lock) {
        if (readyForAuth(userAuth)) {
            processUserAuth(null);
        }
        return authFuture;
    }
}
 
Example 2
Source Project: termd   Source File: ServerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFailAuthenticationWithFuture() throws Exception {
    final int maxAllowedAuths = 10;
    PropertyResolverUtils.updateProperty(sshd, ServerAuthenticationManager.MAX_AUTH_REQUESTS, maxAllowedAuths);

    sshd.start();

    client.setServiceFactories(Arrays.asList(
            new ClientUserAuthServiceOld.Factory(),
            ClientConnectionServiceFactory.INSTANCE
    ));
    client.start();
    try (ClientSession s = client.connect(getCurrentTestName(), TEST_LOCALHOST, sshd.getPort()).verify(7L, TimeUnit.SECONDS).getSession()) {
        int nbTrials = 0;
        AuthFuture authFuture;
        do {
            nbTrials++;
            assertTrue("Number of trials below max.", nbTrials < 100);
            authFuture = s.getService(ClientUserAuthServiceOld.class)
                    .auth(new org.apache.sshd.deprecated.UserAuthPassword(s, "ssh-connection", "buggy"));
            assertTrue("Authentication wait failed", authFuture.await(5L, TimeUnit.SECONDS));
            assertTrue("Authentication not done", authFuture.isDone());
            assertFalse("Authentication unexpectedly successful", authFuture.isSuccess());
        } while (authFuture.getException() == null);

        Throwable t = authFuture.getException();
        assertNotNull("Missing auth future exception", t);
        assertTrue("Number trials (" + nbTrials + ") below min.=" + maxAllowedAuths, nbTrials > maxAllowedAuths);
    } finally {
        client.stop();
    }
}
 
Example 3
Source Project: termd   Source File: ClientUserAuthServiceOld.java    License: Apache License 2.0 5 votes vote down vote up
public AuthFuture auth(UserAuth userAuth) throws IOException {
    log.debug("Trying authentication with {}", userAuth);
    synchronized (lock) {
        if (readyForAuth(userAuth)) {
            processUserAuth(null);
        }
        return authFuture;
    }
}
 
Example 4
Source Project: termd   Source File: ServerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFailAuthenticationWithFuture() throws Exception {
    final int maxAllowedAuths = 10;
    PropertyResolverUtils.updateProperty(sshd, ServerAuthenticationManager.MAX_AUTH_REQUESTS, maxAllowedAuths);

    sshd.start();

    client.setServiceFactories(Arrays.asList(
            new ClientUserAuthServiceOld.Factory(),
            ClientConnectionServiceFactory.INSTANCE
    ));
    client.start();
    try (ClientSession s = client.connect(getCurrentTestName(), TEST_LOCALHOST, sshd.getPort()).verify(7L, TimeUnit.SECONDS).getSession()) {
        int nbTrials = 0;
        AuthFuture authFuture;
        do {
            nbTrials++;
            assertTrue("Number of trials below max.", nbTrials < 100);
            authFuture = s.getService(ClientUserAuthServiceOld.class)
                    .auth(new org.apache.sshd.deprecated.UserAuthPassword(s, "ssh-connection", "buggy"));
            assertTrue("Authentication wait failed", authFuture.await(5L, TimeUnit.SECONDS));
            assertTrue("Authentication not done", authFuture.isDone());
            assertFalse("Authentication unexpectedly successful", authFuture.isSuccess());
        } while (authFuture.getException() == null);

        Throwable t = authFuture.getException();
        assertNotNull("Missing auth future exception", t);
        assertTrue("Number trials (" + nbTrials + ") below min.=" + maxAllowedAuths, nbTrials > maxAllowedAuths);
    } finally {
        client.stop();
    }
}
 
Example 5
Source Project: termd   Source File: AuthenticationTest.java    License: Apache License 2.0 4 votes vote down vote up
private static void assertAuthenticationResult(String message, AuthFuture future, boolean expected) throws IOException {
    assertTrue(message + ": failed to get result on time", future.await(5L, TimeUnit.SECONDS));
    assertEquals(message + ": mismatched authentication result", expected, future.isSuccess());
}
 
Example 6
Source Project: termd   Source File: AuthenticationTest.java    License: Apache License 2.0 4 votes vote down vote up
private static AuthFuture authPassword(ClientSession s, String user, String pswd) throws IOException {
    s.setUsername(user);
    s.addPasswordIdentity(pswd);
    return s.auth();
}
 
Example 7
Source Project: termd   Source File: AuthenticationTest.java    License: Apache License 2.0 4 votes vote down vote up
private static AuthFuture authPublicKey(ClientSession s, String user, KeyPair pair) throws IOException {
    s.setUsername(user);
    s.addPublicKeyIdentity(pair);
    return s.auth();
}
 
Example 8
Source Project: termd   Source File: SinglePublicKeyAuthTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testPublicKeyAuthWithoutCache() throws Exception {
    final ConcurrentHashMap<String, AtomicInteger> count = new ConcurrentHashMap<String, AtomicInteger>();
    delegate = new PublickeyAuthenticator() {
        @SuppressWarnings("synthetic-access")
        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            String fp = KeyUtils.getFingerPrint(key);
            count.putIfAbsent(fp, new AtomicInteger());
            count.get(fp).incrementAndGet();
            return key.equals(pairRsa.getPublic());
        }
    };

    try (SshClient client = setupTestClient()) {
        client.start();

        try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {
            session.addPublicKeyIdentity(pairRsaBad);
            session.addPublicKeyIdentity(pairRsa);

            AuthFuture auth = session.auth();
            assertTrue("Failed to authenticate on time", auth.await(5L, TimeUnit.SECONDS));
            assertTrue("Authentication failed", auth.isSuccess());
        } finally {
            client.stop();
        }
    }

    assertEquals("Mismatched attempted keys count", 2, count.size());

    String badFingerPrint = KeyUtils.getFingerPrint(pairRsaBad.getPublic());
    Number badIndex = count.get(badFingerPrint);
    assertNotNull("Missing bad RSA key", badIndex);
    assertEquals("Mismatched attempt index for bad key", 1, badIndex.intValue());

    String goodFingerPrint = KeyUtils.getFingerPrint(pairRsa.getPublic());
    Number goodIndex = count.get(goodFingerPrint);
    assertNotNull("Missing good RSA key", goodIndex);
    assertEquals("Mismatched attempt index for good key", 2, goodIndex.intValue());
}
 
Example 9
Source Project: termd   Source File: AuthenticationTest.java    License: Apache License 2.0 4 votes vote down vote up
private static void assertAuthenticationResult(String message, AuthFuture future, boolean expected) throws IOException {
    assertTrue(message + ": failed to get result on time", future.await(5L, TimeUnit.SECONDS));
    assertEquals(message + ": mismatched authentication result", expected, future.isSuccess());
}
 
Example 10
Source Project: termd   Source File: AuthenticationTest.java    License: Apache License 2.0 4 votes vote down vote up
private static AuthFuture authPassword(ClientSession s, String user, String pswd) throws IOException {
    s.setUsername(user);
    s.addPasswordIdentity(pswd);
    return s.auth();
}
 
Example 11
Source Project: termd   Source File: AuthenticationTest.java    License: Apache License 2.0 4 votes vote down vote up
private static AuthFuture authPublicKey(ClientSession s, String user, KeyPair pair) throws IOException {
    s.setUsername(user);
    s.addPublicKeyIdentity(pair);
    return s.auth();
}
 
Example 12
Source Project: termd   Source File: SinglePublicKeyAuthTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testPublicKeyAuthWithoutCache() throws Exception {
    final ConcurrentHashMap<String, AtomicInteger> count = new ConcurrentHashMap<String, AtomicInteger>();
    delegate = new PublickeyAuthenticator() {
        @SuppressWarnings("synthetic-access")
        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            String fp = KeyUtils.getFingerPrint(key);
            count.putIfAbsent(fp, new AtomicInteger());
            count.get(fp).incrementAndGet();
            return key.equals(pairRsa.getPublic());
        }
    };

    try (SshClient client = setupTestClient()) {
        client.start();

        try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {
            session.addPublicKeyIdentity(pairRsaBad);
            session.addPublicKeyIdentity(pairRsa);

            AuthFuture auth = session.auth();
            assertTrue("Failed to authenticate on time", auth.await(5L, TimeUnit.SECONDS));
            assertTrue("Authentication failed", auth.isSuccess());
        } finally {
            client.stop();
        }
    }

    assertEquals("Mismatched attempted keys count", 2, count.size());

    String badFingerPrint = KeyUtils.getFingerPrint(pairRsaBad.getPublic());
    Number badIndex = count.get(badFingerPrint);
    assertNotNull("Missing bad RSA key", badIndex);
    assertEquals("Mismatched attempt index for bad key", 1, badIndex.intValue());

    String goodFingerPrint = KeyUtils.getFingerPrint(pairRsa.getPublic());
    Number goodIndex = count.get(goodFingerPrint);
    assertNotNull("Missing good RSA key", goodIndex);
    assertEquals("Mismatched attempt index for good key", 2, goodIndex.intValue());
}
 
Example 13
Source Project: xenon   Source File: MockClientSession.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AuthFuture auth() throws IOException {
    throw new RuntimeException("Not implemented");
}