@@ -319,6 +319,8 @@ CommandResult authenticate(Mongo mongo, final MongoCredential credentials) {
319319 authenticator = new GSSAPIAuthenticator (mongo , credentials );
320320 } else if (credentials .getMechanism ().equals (MongoCredential .PLAIN_MECHANISM )) {
321321 authenticator = new PlainAuthenticator (mongo , credentials );
322+ } else if (credentials .getMechanism ().equals (MongoCredential .MONGODB_X509_MECHANISM )) {
323+ authenticator = new X509Authenticator (mongo , credentials );
322324 } else {
323325 throw new IllegalArgumentException ("Unsupported authentication protocol: " + credentials .getMechanism ());
324326 }
@@ -519,6 +521,30 @@ private CommandResult sendSaslContinue(final int conversationId, final byte[] ou
519521 public abstract String getMechanismName ();
520522 }
521523
524+ class X509Authenticator extends Authenticator {
525+ X509Authenticator (final Mongo mongo , final MongoCredential credential ) {
526+ super (mongo , credential );
527+ }
528+
529+ @ Override
530+ CommandResult authenticate () {
531+ try {
532+ DB db = mongo .getDB (credential .getSource ());
533+ CommandResult res = runCommand (db , getAuthCommand ());
534+ res .throwOnError ();
535+ return res ;
536+ } catch (IOException e ) {
537+ throw new MongoException .Network ("IOException authenticating the connection" , e );
538+ }
539+ }
540+
541+ private DBObject getAuthCommand () {
542+ return new BasicDBObject ("authenticate" , 1 )
543+ .append ("user" , credential .getUserName ())
544+ .append ("mechanism" , MongoCredential .MONGODB_X509_MECHANISM );
545+ }
546+ }
547+
522548 class NativeAuthenticator extends Authenticator {
523549 NativeAuthenticator (Mongo mongo , MongoCredential credentials ) {
524550 super (mongo , credentials );
0 commit comments