@Override public String generate(final MongoCredential credential) { char[] password = credential.getPassword(); if (password == null) { throw new IllegalArgumentException("Password must not be null"); } return new String(password); } };
/** * Gets the password * * @return the password */ @Nullable public char[] getPassword() { return credential != null ? credential.getPassword() : null; }
@Override public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (final Callback callback : callbacks) { if (callback instanceof PasswordCallback) { ((PasswordCallback) callback).setPassword(credential.getPassword()); } else if (callback instanceof NameCallback) { ((NameCallback) callback).setName(credential.getUserName()); } } } });
@Override public String generate(final MongoCredential credential) { // Username and password must not be modified going into the hash. String username = credential.getUserName(); char[] password = credential.getPassword(); if (username == null || password == null) { throw new IllegalArgumentException("Username and password must not be null"); } return createAuthenticationHash(username, password); } };
@NonNull private static char[] getPasswordNonNull(final MongoCredential credential) { char[] password = credential.getPassword(); if (password == null) { throw new MongoInternalException("Password can not be null"); } return password; }
@NonNull char[] getPasswordNonNull() { char[] password = credential.getCredential().getPassword(); if (password == null) { throw new MongoInternalException("Password can not be null"); } return password; }
@Override public String generate(final MongoCredential credential) { char[] password = credential.getPassword(); if (password == null) { throw new IllegalArgumentException("Password must not be null"); } return new String(password); } };
@Override public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (final Callback callback : callbacks) { if (callback instanceof PasswordCallback) { ((PasswordCallback) callback).setPassword(credential.getPassword()); } else if (callback instanceof NameCallback) { ((NameCallback) callback).setName(credential.getUserName()); } } } });
/** * Gets the password * * @return the password */ @Nullable public char[] getPassword() { return credential != null ? credential.getPassword() : null; }
@Override public Map<String, ?> properties(RequestContext ctx) throws Exception { Map properties = new HashMap<>(); properties.put(MECHANISM, mongoCredential.getMechanism()); properties.put(USERNAME, mongoCredential.getUserName()); //only the MONGODB CR has a password and database option, the GSS one only accepts a username if (mongoCredential.getMechanism().equals(MongoCredential.MONGODB_CR_MECHANISM)) { properties.put(PASSWORD, new String(mongoCredential.getPassword())); properties.put(DB, mongoCredential.getSource()); } return properties; }
@Override public String generate(final MongoCredential credential) { // Username and password must not be modified going into the hash. String username = credential.getUserName(); char[] password = credential.getPassword(); if (username == null || password == null) { throw new IllegalArgumentException("Username and password must not be null"); } return createAuthenticationHash(username, password); } };
public static String toString(MongoCredential cr) { StringBuilder bld = new StringBuilder(); bld.append("{mechanism:").append(cr.getMechanism()); if (cr.getUserName() != null) { bld.append(" userName:").append(cr.getUserName()); } if (cr.getPassword() != null) { bld.append(" password:").append(cr.getPassword()); } if (cr.getSource() != null) { bld.append(" source:").append(cr.getSource()); } bld.append('}'); return bld.toString(); }
public static String toString(MongoCredential cr) { StringBuilder bld = new StringBuilder(); bld.append("{mechanism:").append(cr.getMechanism()); if (cr.getUserName() != null) { bld.append(" userName:").append(cr.getUserName()); } if (cr.getPassword() != null) { bld.append(" password:").append(cr.getPassword()); } if (cr.getSource() != null) { bld.append(" source:").append(cr.getSource()); } bld.append('}'); return bld.toString(); }
@NonNull private static char[] getPasswordNonNull(final MongoCredential credential) { char[] password = credential.getPassword(); if (password == null) { throw new MongoInternalException("Password can not be null"); } return password; }
@NonNull char[] getPasswordNonNull() { char[] password = credential.getCredential().getPassword(); if (password == null) { throw new MongoInternalException("Password can not be null"); } return password; }
authSource= uri.getCredentials().getSource(); user= uri.getCredentials().getUserName(); if (uri.getCredentials().getPassword() != null) { pwd = new String(uri.getCredentials().getPassword());
@Test public void testCredentials() { Optional<MongoCredential> credentials = SharedMongoResource.createCredentials(TEST_USER, TEST_PASS, TEST_DB); assertTrue(credentials.isPresent()); assertEquals(TEST_USER, credentials.get().getUserName()); assertEquals(TEST_PASS, new String(credentials.get().getPassword())); credentials = SharedMongoResource.createCredentials(null, TEST_PASS, TEST_DB); assertFalse(credentials.isPresent()); credentials = SharedMongoResource.createCredentials(TEST_USER, null, TEST_DB); assertFalse(credentials.isPresent()); credentials = SharedMongoResource.createCredentials(TEST_USER, TEST_PASS, null); assertFalse(credentials.isPresent()); } }
@Test public void cloudMongoCreationWithMultipleHostsByUri() throws Exception { String uri = String.format("%s://%s:%s@%s:%s/%s", MONGODB_SCHEME, TEST_USERNAME, TEST_PASSWORD, StringUtils.arrayToDelimitedString(TEST_HOSTS, ","), TEST_PORT, TEST_DB); MongoServiceInfo serviceInfo = new MongoServiceInfo("id", uri); MongoDbFactory mongoDbFactory = testCreator.create(serviceInfo, null); assertNotNull(mongoDbFactory); MongoClient mongoClient = getMongoClientField(mongoDbFactory); List<ServerAddress> addresses = extractServerAddresses(mongoClient); assertEquals(3, addresses.size()); MongoCredential credentials = mongoClient.getCredentialsList().get(0); assertEquals(TEST_USERNAME, credentials.getUserName()); assertNotNull(credentials.getPassword()); // Don't do connector.getDatabase().getName() as that will try to initiate the connection assertEquals(TEST_DB, ReflectionTestUtils.getField(mongoDbFactory, "databaseName")); ServerAddress address1 = addresses.get(0); assertEquals(TEST_HOST, address1.getHost()); assertEquals(TEST_PORT_DEFAULT, address1.getPort()); ServerAddress address2 = addresses.get(1); assertEquals(TEST_HOST_1, address2.getHost()); assertEquals(TEST_PORT_DEFAULT, address2.getPort()); ServerAddress address3 = addresses.get(2); assertEquals(TEST_HOST_2, address3.getHost()); assertEquals(TEST_PORT, address3.getPort()); }
@Test public void cloudMongoCreationNoConfig() throws Exception { MongoServiceInfo serviceInfo = new MongoServiceInfo("id", TEST_HOST, TEST_PORT, TEST_USERNAME, TEST_PASSWORD, TEST_DB); MongoDbFactory mongoDbFactory = testCreator.create(serviceInfo, null); assertNotNull(mongoDbFactory); MongoClient mongoClient = getMongoClientField(mongoDbFactory); MongoCredential credentials = mongoClient.getCredentialsList().get(0); List<ServerAddress> addresses = extractServerAddresses(mongoClient); assertEquals(1, addresses.size()); ServerAddress address = addresses.get(0); assertEquals(serviceInfo.getHost(), address.getHost()); assertEquals(serviceInfo.getPort(), address.getPort()); assertEquals(serviceInfo.getUserName(), credentials.getUserName()); assertNotNull(credentials.getPassword()); // Don't do connector.getDatabase().getName() as that will try to initiate the connection assertEquals(serviceInfo.getDatabase(), ReflectionTestUtils.getField(mongoDbFactory, "databaseName")); }