SSHKeyAuthenticatorImpl implements SSHKeyAuthenticator { @Override public User authenticate(final String userName, final PublicKey key) { final Optional<User> userOptional = Optional.ofNullable(userManager.getUser(userName)); if (userOptional.isPresent()) { List<SSHPublicKey> keys = new ArrayList<>(keyStoreService.keyStore().getUserKeys(userName)); PublicKey resultKey = KeyUtils.findMatchingKey(key, keys.stream().map(SSHPublicKey::getKey).collect(Collectors.toList())); if (resultKey != null) { keys.stream() .filter(userKey -> userKey.getKey().equals(resultKey)) .findAny() .ifPresent(userKey -> updateUserKey(userName, userKey)); return userOptional.get(); } } return null; } @Inject  SSHKeyAuthenticatorImpl(final SSHKeyStoreService keyStoreService, final Instance<WorkbenchUserManager> workbenchUserManagerInstance); @Override User authenticate(final String userName, final PublicKey key);  }
@Test public void testAuthenticateUserWithoutKeys() throws Exception { authenticator = new SSHKeyAuthenticatorImpl(keyStoreService, workbenchUserManagerInstance); PublicKey publicKey = PublicKeyConverter.fromString(readSampleSSHKey()); Assertions.assertThat(authenticator.authenticate(ADMIN, publicKey)) .isNull(); }
@Test public void testAuthenticateNonPlatformUser() throws Exception { authenticator = new SSHKeyAuthenticatorImpl(keyStoreService, workbenchUserManagerInstance); PublicKey publicKey = PublicKeyConverter.fromString(readSampleSSHKey()); Assertions.assertThat(authenticator.authenticate("user", publicKey)) .isNull(); }