MongoAuthentication { static Optional<MongoCredential> of(Settings settings) { Optional<String> user = settings.get(Arrays.asList(USER.get(), Configurations.USER.get())) .map(Object::toString); Optional<char[]> password = settings.get(Arrays.asList(PASSWORD.get(), Configurations.PASSWORD.get())) .map(Object::toString).map(String::toCharArray); Optional<String> source = settings.get(AUTHENTICATION_SOURCE.get()) .map(Object::toString); Optional<AuthenticationMechanism> mechanism = settings.get(AUTHENTICATION_MECHANISM.get()) .map(Object::toString) .map(AuthenticationMechanism::fromMechanismName); if (!user.isPresent()) { return Optional.empty(); } if (!mechanism.isPresent()) { return Optional.of(MongoCredential.createCredential(user.orElseThrow(missingExceptionUser()), source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword()))); } switch (mechanism.get()) { case PLAIN: return Optional.of(MongoCredential.createPlainCredential(user.orElseThrow(missingExceptionUser()), source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword()))); case GSSAPI: return Optional.of(MongoCredential.createGSSAPICredential(user.orElseThrow(missingExceptionUser()))); case SCRAM_SHA_1: return Optional.of(MongoCredential.createScramSha1Credential(user.orElseThrow(missingExceptionUser()), source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword()))); case MONGODB_X509: return Optional.of(MongoCredential.createMongoX509Credential(user.orElseThrow(missingExceptionUser()))); case SCRAM_SHA_256: return Optional.of(MongoCredential.createScramSha256Credential(user.orElseThrow(missingExceptionUser()), source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword()))); default: throw new CommunicationException("There is not support to the type: " + mechanism); } } private  MongoAuthentication();   }
@Test public void shouldReturnErrorWhenTheNumberParameterIsInvalid() { Settings settings = Settings.builder().put(USER.get(), "value") .build(); assertThrows(CommunicationException.class, () -> MongoAuthentication.of(settings)); }
@Test public void shouldReturnOneAuthentication() { Settings settings = Settings.builder() .put(AUTHENTICATION_SOURCE.get(), "database") .put(PASSWORD.get(), "password") .put(USER.get(), "user") .build(); MongoCredential credential = MongoAuthentication.of(settings).get(); assertEquals("database", credential.getSource()); assertTrue(Arrays.equals("password".toCharArray(), credential.getPassword())); assertEquals("user", credential.getUserName()); assertNull(credential.getMechanism()); }
@Test public void shouldReturnOneAuthenticationWithGSSAPI() { Settings settings = Settings.builder() .put(AUTHENTICATION_SOURCE.get(), "database") .put(PASSWORD.get(), "password") .put(USER.get(), "user") .put(AUTHENTICATION_MECHANISM.get(), "GSSAPI") .build(); MongoCredential credential = MongoAuthentication.of(settings).get(); assertEquals("$external", credential.getSource()); assertEquals("user", credential.getUserName()); assertEquals(GSSAPI.getMechanismName(), credential.getMechanism()); }
@Test public void shouldReturnOneAuthenticationWithMongoX509() { Settings settings = Settings.builder() .put(AUTHENTICATION_SOURCE.get(), "database") .put(PASSWORD.get(), "password") .put(USER.get(), "user") .put(AUTHENTICATION_MECHANISM.get(), "MONGODB-X509") .build(); MongoCredential credential = MongoAuthentication.of(settings).get(); assertEquals("$external", credential.getSource()); assertEquals("user", credential.getUserName()); assertEquals(AuthenticationMechanism.MONGODB_X509.getMechanismName(), credential.getMechanism()); }
@Test public void shouldReturnOneAuthenticationWithSCRAMSHA1() { Settings settings = Settings.builder() .put(AUTHENTICATION_SOURCE.get(), "database") .put(PASSWORD.get(), "password") .put(USER.get(), "user") .put(AUTHENTICATION_MECHANISM.get(), "SCRAM-SHA-1") .build(); MongoCredential credential = MongoAuthentication.of(settings).get(); assertEquals("database", credential.getSource()); assertTrue(Arrays.equals("password".toCharArray(), credential.getPassword())); assertEquals("user", credential.getUserName()); assertEquals(SCRAM_SHA_1.getMechanismName(), credential.getMechanism()); }
@Test public void shouldReturnOneAuthenticationWithSCRAMSHA256() { Settings settings = Settings.builder() .put(AUTHENTICATION_SOURCE.get(), "database") .put(PASSWORD.get(), "password") .put(USER.get(), "user") .put(AUTHENTICATION_MECHANISM.get(), "SCRAM-SHA-256") .build(); MongoCredential credential = MongoAuthentication.of(settings).get(); assertEquals("database", credential.getSource()); assertTrue(Arrays.equals("password".toCharArray(), credential.getPassword())); assertEquals("user", credential.getUserName()); assertEquals(SCRAM_SHA_256.getMechanismName(), credential.getMechanism()); }