session.addPublicKeyIdentity(pair); session.auth().verify(10000L);
@Override public void operationComplete(final AuthFuture future) { if (future.isSuccess()) { handleSshAuthenticated(session, ctx); } else { // Exception does not have to be set in the future, add simple exception in such case final Throwable exception = future.getException() == null ? new IllegalStateException("Authentication failed") : future.getException(); handleSshSetupFailure(ctx, exception); } } });
protected void signalAuthFailure(AuthFuture future, Throwable t) { boolean signalled = false; synchronized (lock) { if ((future != null) && (!future.isDone())) { future.setException(t); signalled = true; } } if (log.isDebugEnabled()) { log.debug("signalAuthFailure({}) type={}, signalled={}, message=\"{}\"", this, t.getClass().getSimpleName(), signalled, t.getMessage()); } }
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()); }
private boolean readyForAuth(UserAuth userAuth) { while (!this.authFuture.isDone()) { log.debug("waiting to send authentication"); try { this.authFuture.await(); // TODO use verify + configurable timeout } catch (IOException e) { log.debug("Unexpected exception", e); if (this.authFuture.isSuccess()) { log.debug("already authenticated"); throw new IllegalStateException("Already authenticated"); Throwable err = this.authFuture.getException(); if (err != null) { log.debug("probably closed", err); return false; if (!this.authFuture.isFailure()) { log.debug("unexpected state"); throw new IllegalStateException("Unexpected authentication state");
assertTrue("Failed to complete auth in allocated time", future.await(11L, TimeUnit.SECONDS)); assertFalse("Unexpected authentication success", future.isSuccess()); Throwable actual = future.getException(); if (actual instanceof IOException) { actual = actual.getCause();
AuthFuture authFuture = authFutureHolder.get(); boolean debugEnabled = log.isDebugEnabled(); if ((authFuture != null) && authFuture.isSuccess()) { log.error("process({}) unexpected authenticated client command: {}", session, SshConstants.getCommandMessageName(cmd)); throw new IllegalStateException("UserAuth message delivered to authenticated client"); } else if ((authFuture != null) && authFuture.isDone()) {
authFuture.addListener(new SshFutureListener<AuthFuture>() System.out.println("Authentication completed with " + ( arg0.isSuccess() ? "success" : "failure"));
@Override public void operationComplete(AuthFuture authFuture) { log.info("Authentication completed with " + (authFuture.isSuccess() ? "success" : "failure") + " for " + user + "@" + ip + ":" + port); } });
@Override public void start() { synchronized (lock) { log.debug("accepted"); // kick start the authentication process by failing the pending auth. this.authFuture.setAuthed(false); } }
@Test public void testPasswordTimeout() throws Exception { SSHClient client = spy(this.client); SshClient ssh = spy(SshClient.setUpDefaultClient()); ConnectFuture future = mock(ConnectFuture.class); ClientSession session = mock(ClientSession.class); doReturn(ssh).when(client).createSshClient(); doReturn(future).when(ssh).connect(any(), any(), anyInt()); when(future.await(anyLong())).thenReturn(true); when(future.getSession()).thenReturn(session); AuthFuture authFuture = mock(AuthFuture.class); when(authFuture.await(anyLong())).thenReturn(false); when(session.auth()).thenReturn(authFuture); assertThrows(TimeLimitExceededException.class, () -> { client.connect(); client.authenticate(); }); } }
cond.add(ClientSessionEvent.AUTHED); if (KexState.DONE.equals(kexState.get()) && authFuture.isFailure()) { cond.add(ClientSessionEvent.WAIT_AUTH);
private synchronized void handleSshSessionCreated(final ConnectFuture future, final ChannelHandlerContext ctx) { try { LOG.trace("SSH session created on channel: {}", ctx.channel()); session = future.getSession(); final AuthFuture authenticateFuture = authenticationHandler.authenticate(session); authenticateFuture.addListener(new SshFutureListener<AuthFuture>() { @Override public void operationComplete(final AuthFuture future) { if (future.isSuccess()) { handleSshAuthenticated(session, ctx); } else { // Exception does not have to be set in the future, add simple exception in such case final Throwable exception = future.getException() == null ? new IllegalStateException("Authentication failed") : future.getException(); handleSshSetupFailure(ctx, exception); } } }); } catch (final IOException e) { handleSshSetupFailure(ctx, e); } }
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()); }
private boolean readyForAuth(UserAuth userAuth) { while (!this.authFuture.isDone()) { log.debug("waiting to send authentication"); try { this.authFuture.await(); // TODO use verify + configurable timeout } catch (IOException e) { log.debug("Unexpected exception", e); if (this.authFuture.isSuccess()) { log.debug("already authenticated"); throw new IllegalStateException("Already authenticated"); Throwable err = this.authFuture.getException(); if (err != null) { log.debug("probably closed", err); return false; if (!this.authFuture.isFailure()) { log.debug("unexpected state"); throw new IllegalStateException("Unexpected authentication state");
assertTrue("Failed to complete auth in allocated time", future.await(11L, TimeUnit.SECONDS)); assertFalse("Unexpected authentication success", future.isSuccess()); Throwable actual = future.getException(); if (actual instanceof IOException) { actual = actual.getCause();
AuthFuture authFuture = authFutureHolder.get(); boolean debugEnabled = log.isDebugEnabled(); if ((authFuture != null) && authFuture.isSuccess()) { log.error("process({}) unexpected authenticated client command: {}", session, SshConstants.getCommandMessageName(cmd)); throw new IllegalStateException("UserAuth message delivered to authenticated client"); } else if ((authFuture != null) && authFuture.isDone()) {
@Override public void start() { synchronized (lock) { log.debug("accepted"); // kick start the authentication process by failing the pending auth. this.authFuture.setAuthed(false); } }
cond.add(ClientSessionEvent.AUTHED); if (KexState.DONE.equals(kexState.get()) && authFuture.isFailure()) { cond.add(ClientSessionEvent.WAIT_AUTH);