1515
1616package org .jnosql .diana .mongodb .document ;
1717
18+ import com .mongodb .AuthenticationMechanism ;
1819import com .mongodb .MongoCredential ;
20+ import org .jnosql .diana .api .Configurations ;
21+ import org .jnosql .diana .api .JNoSQLException ;
22+ import org .jnosql .diana .api .Settings ;
23+
24+ import java .util .ArrayList ;
25+ import java .util .Arrays ;
26+ import java .util .List ;
27+ import java .util .stream .Collectors ;
28+
29+ import static org .jnosql .diana .mongodb .document .MongoDBDocumentConfigurations .AUTHENTICATION_DATABASE ;
30+ import static org .jnosql .diana .mongodb .document .MongoDBDocumentConfigurations .AUTHENTICATION_MECHANISM ;
31+ import static org .jnosql .diana .mongodb .document .MongoDBDocumentConfigurations .PASSWORD ;
32+ import static org .jnosql .diana .mongodb .document .MongoDBDocumentConfigurations .USER ;
1933
2034final class MongoAuthentication {
2135
@@ -25,15 +39,69 @@ final class MongoAuthentication {
2539
2640 private final char [] password ;
2741
28- MongoAuthentication (String user , String database , char [] password ) {
42+ private final AuthenticationMechanism mechanism ;
43+
44+ private MongoAuthentication (String user , String database , char [] password ,
45+ AuthenticationMechanism mechanism ) {
2946 this .user = user ;
3047 this .database = database ;
3148 this .password = password ;
49+ this .mechanism = mechanism ;
3250 }
3351
3452 MongoCredential toCredential () {
35- return MongoCredential .createCredential (user , database , password );
53+ switch (mechanism ) {
54+ case PLAIN :
55+ return MongoCredential .createPlainCredential (user , database , password );
56+ case GSSAPI :
57+ return MongoCredential .createGSSAPICredential (user );
58+ case SCRAM_SHA_1 :
59+ return MongoCredential .createScramSha1Credential (user , database , password );
60+ case MONGODB_X509 :
61+ return MongoCredential .createMongoX509Credential (user );
62+ case SCRAM_SHA_256 :
63+ return MongoCredential .createScramSha256Credential (user , database , password );
64+ default :
65+ throw new JNoSQLException ("There is not support to the type: " + mechanism );
66+
67+ }
3668 }
3769
3870
71+ static List <MongoAuthentication > of (Settings settings ) {
72+
73+ List <MongoAuthentication > authentications = new ArrayList <>();
74+
75+ List <String > users = settings .prefix (Arrays .asList (USER .get (),
76+ Configurations .USER .get ())).stream ()
77+ .map (Object ::toString ).collect (Collectors .toList ());
78+
79+ List <String > passwords = settings .prefix (Arrays .asList (PASSWORD .get (),
80+ Configurations .PASSWORD .get ())).stream ()
81+ .map (Object ::toString ).collect (Collectors .toList ());
82+
83+ List <String > databases = settings .prefix (AUTHENTICATION_DATABASE .get ()).stream ()
84+ .map (Object ::toString ).collect (Collectors .toList ());
85+
86+ AuthenticationMechanism mechanism = settings .get (AUTHENTICATION_MECHANISM .get ())
87+ .map (Object ::toString )
88+ .map (AuthenticationMechanism ::fromMechanismName )
89+ .orElse (AuthenticationMechanism .PLAIN );
90+
91+ if (users .size () != passwords .size () && users .size () != databases .size ()) {
92+ throw new JNoSQLException ("There is an inconsistent number of authentication parameter" );
93+ }
94+
95+ for (int index = 0 ; index < databases .size (); index ++) {
96+ String user = users .get (index );
97+ String password = passwords .get (index );
98+ String database = databases .get (index );
99+ MongoAuthentication authentication = new MongoAuthentication (user ,
100+ database , password .toCharArray (), mechanism );
101+ authentications .add (authentication );
102+ }
103+
104+ return authentications ;
105+
106+ }
39107}
0 commit comments