org.apache.sshd.server.auth.password.PasswordChangeRequiredException Java Examples

The following examples show how to use org.apache.sshd.server.auth.password.PasswordChangeRequiredException. 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 File: AsyncAuthTestBase.java    From termd with Apache License 2.0 7 votes vote down vote up
public void startServer(Integer timeout) throws Exception {
  if (server != null) {
    throw failure("Server already started");
  }
  server = SshServer.setUpDefaultServer();
  if (timeout != null) {
    server.getProperties().put(FactoryManager.AUTH_TIMEOUT, timeout.toString());
  }
  server.setPort(5000);
  server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath()));
  server.setPasswordAuthenticator(new PasswordAuthenticator() {
    @Override
    public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
      return authenticator.authenticate(username, password, session);
    }
  });
  server.setShellFactory(new EchoShellFactory());
  server.setServiceFactories(Arrays.asList(ServerConnectionServiceFactory.INSTANCE, AsyncUserAuthServiceFactory.INSTANCE));
  server.start();
}
 
Example #2
Source File: SshTtyTestBase.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
protected void server(final Consumer<TtyConnection> onConnect) {
  if (sshd != null) {
    throw failure("Already a server");
  }
  try {
    sshd = createServer();
    sshd.setPort(5000);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath()));
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
      @Override
      public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
        return true;
      }
    });
    sshd.setShellFactory(new Factory<Command>() {
      @Override
      public Command create() {
        return createConnection(onConnect);
      }
    });
    sshd.start();
  } catch (Exception e) {
    throw failure(e);
  }
}
 
Example #3
Source File: AsyncAuthTestBase.java    From termd with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsyncAuthFailed() throws Exception {
  startServer();
  authenticator = new PasswordAuthenticator() {
    @Override
    public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
      final AsyncAuth auth = new AsyncAuth();
      new Thread() {
        @Override
        public void run() {
          try {
            Thread.sleep(200);
          } catch (InterruptedException ignore) {
          } finally {
            auth.setAuthed(false);
          }
        }
      }.start();
      throw auth;
    }
  };
  assertFalse(authenticate());
}
 
Example #4
Source File: AsyncAuthTestBase.java    From termd with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsyncAuthSucceeded() throws Exception {
  startServer();
  authenticator = new PasswordAuthenticator() {
    @Override
    public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
      final AsyncAuth auth = new AsyncAuth();
      new Thread() {
        @Override
        public void run() {
          try {
            Thread.sleep(200);
          } catch (InterruptedException ignore) {
          } finally {
            auth.setAuthed(true);
          }
        }
      }.start();
      throw auth;
    }
  };
  assertTrue(authenticate());
}
 
Example #5
Source File: SshShellSecurityAuthenticationProvider.java    From ssh-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
@Override
public boolean authenticate(String username, String pass,
                            ServerSession serverSession) throws PasswordChangeRequiredException {
    try {
        Authentication auth = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(username, pass));
        LOGGER.debug("User {} authenticated with authorities: {}", username, auth.getAuthorities());
        List<String> authorities =
                auth.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
        serverSession.getIoSession().setAttribute(AUTHENTICATION_ATTRIBUTE, new SshAuthentication(username,
                auth.getPrincipal(), auth.getDetails(), auth.getCredentials(), authorities));
        return auth.isAuthenticated();
    } catch (AuthenticationException e) {
        LOGGER.error("Unable to authenticate user [{}] : {}", username, e.getMessage());
        LOGGER.debug("Unable to authenticate user [{}]", username, e);
        return false;
    }
}
 
Example #6
Source File: NettySshTtyBootstrap.java    From termd with Apache License 2.0 5 votes vote down vote up
public NettySshTtyBootstrap() {
  this.host = "localhost";
  this.port = 5000;
  this.charset = UTF_8;
  this.parentGroup = new NioEventLoopGroup(1);
  this.childGroup = new NioEventLoopGroup();
  this.keyPairProvider = new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath());
  this.passwordAuthenticator = new PasswordAuthenticator() {
    @Override
    public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
      return true;
    }
  };
}
 
Example #7
Source File: AsyncAuthTestBase.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testSyncAuthFailed() throws Exception {
  startServer();
  authenticator = new PasswordAuthenticator() {
    @Override
    public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
      return false;
    }
  };
  assertFalse(authenticate());
}
 
Example #8
Source File: AsyncAuthTestBase.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testSyncAuthSucceeded() throws Exception {
  startServer();
  authenticator = new PasswordAuthenticator() {
    @Override
    public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
      return true;
    }
  };
  assertTrue(authenticate());
}
 
Example #9
Source File: AsyncAuthTestBase.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncAuthTimeout() throws Exception {
  startServer(500);
  authenticator = new PasswordAuthenticator() {
    @Override
    public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
      throw new AsyncAuth();
    }
  };
  try {
    authenticate();
  } catch (JSchException e) {
    assertTrue("Unexpected failure " + e.getMessage(), e.getMessage().startsWith("SSH_MSG_DISCONNECT"));
  }
}
 
Example #10
Source File: AsyncAuthTestBase.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncAuthSucceededAfterTimeout() throws Exception {
  startServer(500);
  authenticator = new PasswordAuthenticator() {
    @Override
    public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
      final AsyncAuth auth = new AsyncAuth();
      new Thread() {
        @Override
        public void run() {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException ignore) {
          } finally {
            auth.setAuthed(true);
          }
        }
      }.start();
      throw auth;
    }
  };
  try {
    authenticate();
  } catch (JSchException e) {
    assertTrue("Unexpected failure " + e.getMessage(), e.getMessage().startsWith("SSH_MSG_DISCONNECT"));
  }
}
 
Example #11
Source File: SshShellPasswordAuthenticationProvider.java    From ssh-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public boolean authenticate(String username, String pass,
                            ServerSession serverSession) throws PasswordChangeRequiredException {

    serverSession.getIoSession().setAttribute(AUTHENTICATION_ATTRIBUTE, new SshAuthentication(username, username));

    return username.equals(this.user) && pass.equals(this.password);
}
 
Example #12
Source File: PasswordAuthenticatorImpl.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException, AsyncAuthException {
    LOG.info("authenticate: {}", username);
    String passwd = credentials.get(username);
    if (passwd != null && passwd.equals(passwd)) {
        return true;
    } else {
        return false;
    }
}
 
Example #13
Source File: AuthProviderSshdPasswordAuthenticator.java    From sshd-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public boolean authenticate(String username, String password, ServerSession session) throws
        PasswordChangeRequiredException {
    try {
        Authentication auth = authProvider.authenticate(
                new UsernamePasswordAuthenticationToken(username, password));
        session.getIoSession().setAttribute(Constants.USER, username);
        session.getIoSession().setAttribute(Constants.USER_ROLES, auth.getAuthorities().stream()
                .map(ga -> ga.getAuthority()).collect(Collectors.toSet()));
        return true;
    } catch (AuthenticationException ex) {
        log.warn(ex.getMessage());
        return false;
    }
}
 
Example #14
Source File: SimpleSshdPasswordAuthenticator.java    From sshd-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public boolean authenticate(String username, String password, ServerSession session) throws
        PasswordChangeRequiredException {
    if (username.equals(props.getUsername()) && password.equals(props.getPassword())) {
        session.getIoSession().setAttribute(Constants.USER_ROLES, systemCommandRoles);
        session.getIoSession().setAttribute(Constants.USER, username);
        return true;
    }
    return false;
}
 
Example #15
Source File: SSHDUnitTest.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
public boolean authenticate(String username, String password, ServerSession session)
    throws PasswordChangeRequiredException {
  return username.equals(TESTUSER) && password.equals(TESTPASS);
}