-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from eclipse/mongo_authenticate
Mongo authenticate
- Loading branch information
Showing
4 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
mongodb-driver/src/main/java/org/jnosql/diana/mongodb/document/MongoAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) 2019 Otávio Santana and others | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
* Contributors: | ||
* | ||
* Otavio Santana | ||
*/ | ||
|
||
package org.jnosql.diana.mongodb.document; | ||
|
||
import com.mongodb.AuthenticationMechanism; | ||
import com.mongodb.MongoCredential; | ||
import org.jnosql.diana.api.Configurations; | ||
import org.jnosql.diana.api.JNoSQLException; | ||
import org.jnosql.diana.api.Settings; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_MECHANISM; | ||
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_SOURCE; | ||
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.PASSWORD; | ||
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.USER; | ||
|
||
final class MongoAuthentication { | ||
|
||
private 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); | ||
|
||
AuthenticationMechanism mechanism = settings.get(AUTHENTICATION_MECHANISM.get()) | ||
.map(Object::toString) | ||
.map(AuthenticationMechanism::fromMechanismName) | ||
.orElse(AuthenticationMechanism.PLAIN); | ||
|
||
if (!user.isPresent()) { | ||
return Optional.empty(); | ||
} | ||
|
||
switch (mechanism) { | ||
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 JNoSQLException("There is not support to the type: " + mechanism); | ||
} | ||
|
||
} | ||
|
||
|
||
private static Supplier<JNoSQLException> missingExceptionUser() { | ||
return missingException("user"); | ||
} | ||
|
||
private static Supplier<JNoSQLException> missingExceptionPassword() { | ||
return missingException("password"); | ||
} | ||
|
||
private static Supplier<JNoSQLException> missingExceptionSource() { | ||
return missingException("source"); | ||
} | ||
|
||
|
||
private static Supplier<JNoSQLException> missingException(String parameter) { | ||
return () -> new JNoSQLException("There is a missing parameter in mongoDb authentication: " + parameter); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
mongodb-driver/src/test/java/org/jnosql/diana/mongodb/document/MongoAuthenticationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright (c) 2019 Otávio Santana and others | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
* Contributors: | ||
* | ||
* Otavio Santana | ||
*/ | ||
package org.jnosql.diana.mongodb.document; | ||
|
||
import com.mongodb.AuthenticationMechanism; | ||
import com.mongodb.MongoCredential; | ||
import org.jnosql.diana.api.JNoSQLException; | ||
import org.jnosql.diana.api.Settings; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static com.mongodb.AuthenticationMechanism.GSSAPI; | ||
import static com.mongodb.AuthenticationMechanism.PLAIN; | ||
import static com.mongodb.AuthenticationMechanism.SCRAM_SHA_1; | ||
import static com.mongodb.AuthenticationMechanism.SCRAM_SHA_256; | ||
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_MECHANISM; | ||
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_SOURCE; | ||
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.PASSWORD; | ||
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.USER; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class MongoAuthenticationTest { | ||
|
||
@Test | ||
public void shouldReturnErrorWhenTheNumberParameterIsInvalid() { | ||
Settings settings = Settings.builder().put(USER.get(), "value") | ||
.build(); | ||
|
||
assertThrows(JNoSQLException.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()); | ||
assertEquals(PLAIN.getMechanismName(), 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()); | ||
} | ||
|
||
} |