|
21 | 21 | import org.jnosql.diana.api.JNoSQLException; |
22 | 22 | import org.jnosql.diana.api.Settings; |
23 | 23 |
|
24 | | -import java.util.ArrayList; |
25 | 24 | import java.util.Arrays; |
26 | | -import java.util.List; |
27 | | -import java.util.stream.Collectors; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.function.Supplier; |
28 | 27 |
|
29 | | -import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_SOURCE; |
30 | 28 | import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_MECHANISM; |
| 29 | +import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_SOURCE; |
31 | 30 | import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.PASSWORD; |
32 | 31 | import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.USER; |
33 | 32 |
|
34 | 33 | final class MongoAuthentication { |
35 | 34 |
|
36 | | - private final String user; |
37 | 35 |
|
38 | | - private final String database; |
| 36 | + static Optional<MongoCredential> of(Settings settings) { |
39 | 37 |
|
40 | | - private final char[] password; |
41 | 38 |
|
42 | | - private final AuthenticationMechanism mechanism; |
| 39 | + Optional<String> user = settings.get(Arrays.asList(USER.get(), |
| 40 | + Configurations.USER.get())) |
| 41 | + .map(Object::toString); |
43 | 42 |
|
44 | | - private MongoAuthentication(String user, String database, char[] password, |
45 | | - AuthenticationMechanism mechanism) { |
46 | | - this.user = user; |
47 | | - this.database = database; |
48 | | - this.password = password; |
49 | | - this.mechanism = mechanism; |
50 | | - } |
| 43 | + Optional<char[]> password = settings.get(Arrays.asList(PASSWORD.get(), |
| 44 | + Configurations.PASSWORD.get())) |
| 45 | + .map(Object::toString).map(String::toCharArray); |
| 46 | + |
| 47 | + Optional<String> source = settings.get(AUTHENTICATION_SOURCE.get()) |
| 48 | + .map(Object::toString); |
| 49 | + |
| 50 | + AuthenticationMechanism mechanism = settings.get(AUTHENTICATION_MECHANISM.get()) |
| 51 | + .map(Object::toString) |
| 52 | + .map(AuthenticationMechanism::fromMechanismName) |
| 53 | + .orElse(AuthenticationMechanism.PLAIN); |
| 54 | + |
| 55 | + if (!user.isPresent()) { |
| 56 | + return Optional.empty(); |
| 57 | + } |
51 | 58 |
|
52 | | - MongoCredential toCredential() { |
53 | 59 | switch (mechanism) { |
54 | 60 | case PLAIN: |
55 | | - return MongoCredential.createPlainCredential(user, database, password); |
| 61 | + return Optional.of(MongoCredential.createPlainCredential(user.orElseThrow(missingExceptionUser()), |
| 62 | + source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword()))); |
56 | 63 | case GSSAPI: |
57 | | - return MongoCredential.createGSSAPICredential(user); |
| 64 | + return Optional.of(MongoCredential.createGSSAPICredential(user.orElseThrow(missingExceptionUser()))); |
58 | 65 | case SCRAM_SHA_1: |
59 | | - return MongoCredential.createScramSha1Credential(user, database, password); |
| 66 | + return Optional.of(MongoCredential.createScramSha1Credential(user.orElseThrow(missingExceptionUser()), |
| 67 | + source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword()))); |
60 | 68 | case MONGODB_X509: |
61 | | - return MongoCredential.createMongoX509Credential(user); |
| 69 | + return Optional.of(MongoCredential.createMongoX509Credential(user.orElseThrow(missingExceptionUser()))); |
62 | 70 | case SCRAM_SHA_256: |
63 | | - return MongoCredential.createScramSha256Credential(user, database, password); |
| 71 | + return Optional.of(MongoCredential.createScramSha256Credential(user.orElseThrow(missingExceptionUser()), |
| 72 | + source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword()))); |
64 | 73 | default: |
65 | 74 | throw new JNoSQLException("There is not support to the type: " + mechanism); |
66 | | - |
67 | 75 | } |
68 | | - } |
69 | | - |
70 | 76 |
|
71 | | - static List<MongoCredential> of(Settings settings) { |
72 | | - |
73 | | - List<MongoCredential> credentials = new ArrayList<>(); |
| 77 | + } |
74 | 78 |
|
75 | | - List<String> users = settings.prefix(Arrays.asList(USER.get(), |
76 | | - Configurations.USER.get())).stream() |
77 | | - .map(Object::toString).collect(Collectors.toList()); |
78 | 79 |
|
79 | | - List<String> passwords = settings.prefix(Arrays.asList(PASSWORD.get(), |
80 | | - Configurations.PASSWORD.get())).stream() |
81 | | - .map(Object::toString).collect(Collectors.toList()); |
| 80 | + private static Supplier<JNoSQLException> missingExceptionUser() { |
| 81 | + return missingException("user"); |
| 82 | + } |
82 | 83 |
|
83 | | - List<String> sources = settings.prefix(AUTHENTICATION_SOURCE.get()).stream() |
84 | | - .map(Object::toString).collect(Collectors.toList()); |
| 84 | + private static Supplier<JNoSQLException> missingExceptionPassword() { |
| 85 | + return missingException("password"); |
| 86 | + } |
85 | 87 |
|
86 | | - AuthenticationMechanism mechanism = settings.get(AUTHENTICATION_MECHANISM.get()) |
87 | | - .map(Object::toString) |
88 | | - .map(AuthenticationMechanism::fromMechanismName) |
89 | | - .orElse(AuthenticationMechanism.PLAIN); |
| 88 | + private static Supplier<JNoSQLException> missingExceptionSource() { |
| 89 | + return missingException("source"); |
| 90 | + } |
90 | 91 |
|
91 | | - if (users.size() != passwords.size() || users.size() != sources.size()) { |
92 | | - throw new JNoSQLException("There is an inconsistent number of authentication parameter"); |
93 | | - } |
94 | 92 |
|
95 | | - for (int index = 0; index < sources.size(); index++) { |
96 | | - String user = users.get(index); |
97 | | - String password = passwords.get(index); |
98 | | - String source = sources.get(index); |
99 | | - MongoAuthentication credential = new MongoAuthentication(user, |
100 | | - source, password.toCharArray(), mechanism); |
101 | | - credentials.add(credential.toCredential()); |
102 | | - } |
| 93 | + private static Supplier<JNoSQLException> missingException(String parameter) { |
| 94 | + return () -> new JNoSQLException("There is a missing parameter in mongoDb authentication: " + parameter); |
| 95 | + } |
103 | 96 |
|
104 | | - return credentials; |
105 | 97 |
|
106 | | - } |
107 | 98 | } |
0 commit comments