@@ -424,19 +424,48 @@ public boolean isAuthenticated() {
424
424
public boolean authenticate (String username , char [] passwd )
425
425
throws MongoException {
426
426
427
- if ( username == null )
427
+ if ( username == null || passwd == null )
428
428
throw new NullPointerException ( "username can't be null" );
429
429
430
430
if ( _username != null )
431
431
throw new IllegalStateException ( "can't call authenticate twice on the same DBObject" );
432
432
433
433
String hash = _hash ( username , passwd );
434
- if ( ! _doauth ( username , hash .getBytes () ) )
434
+ CommandResult res = _doauth ( username , hash .getBytes () );
435
+ if ( !res .ok ())
435
436
return false ;
436
437
_username = username ;
437
438
_authhash = hash .getBytes ();
438
439
return true ;
439
440
}
441
+
442
+ /**
443
+ * Authenticates to db with the given name and password
444
+ *
445
+ * @param username name of user for this database
446
+ * @param passwd password of user for this database
447
+ * @return the CommandResult from authenticate command
448
+ * @throws MongoException if authentication failed due to invalid user/pass, or other exceptions like I/O
449
+ * @dochub authenticate
450
+ */
451
+ public CommandResult authenticateCommand (String username , char [] passwd )
452
+ throws MongoException {
453
+
454
+ if ( username == null || passwd == null )
455
+ throw new NullPointerException ( "username can't be null" );
456
+
457
+ if ( _username != null )
458
+ throw new IllegalStateException ( "can't call authenticate twice on the same DBObject" );
459
+
460
+ String hash = _hash ( username , passwd );
461
+ CommandResult res = _doauth ( username , hash .getBytes () );
462
+ if ( !res .ok ())
463
+ throw new MongoException (res );
464
+ _username = username ;
465
+ _authhash = hash .getBytes ();
466
+ return res ;
467
+ }
468
+
440
469
/*
441
470
boolean reauth(){
442
471
if ( _username == null || _authhash == null )
@@ -465,27 +494,23 @@ static DBObject _authCommand( String nonce , String username , byte[] hash ){
465
494
return cmd ;
466
495
}
467
496
468
- private boolean _doauth ( String username , byte [] hash ){
497
+ private CommandResult _doauth ( String username , byte [] hash ){
469
498
CommandResult res = command (new BasicDBObject ("getnonce" , 1 ));
470
-
471
499
if ( ! res .ok () ){
472
- throw new MongoException ("Error - unable to get nonce value for authentication." );
500
+ throw new MongoException (res );
473
501
}
474
502
475
503
DBObject cmd = _authCommand ( res .getString ( "nonce" ) , username , hash );
476
-
477
- res = command (cmd );
478
-
479
- return res .ok ();
504
+ return command (cmd );
480
505
}
481
506
482
507
/**
483
508
* Adds a new user for this db
484
509
* @param username
485
510
* @param passwd
486
511
*/
487
- public void addUser ( String username , char [] passwd ){
488
- addUser (username , passwd , false );
512
+ public WriteResult addUser ( String username , char [] passwd ){
513
+ return addUser (username , passwd , false );
489
514
}
490
515
491
516
/**
@@ -494,23 +519,23 @@ public void addUser( String username , char[] passwd ){
494
519
* @param passwd
495
520
* @param readOnly if true, user will only be able to read
496
521
*/
497
- public void addUser ( String username , char [] passwd , boolean readOnly ){
522
+ public WriteResult addUser ( String username , char [] passwd , boolean readOnly ){
498
523
DBCollection c = getCollection ( "system.users" );
499
524
DBObject o = c .findOne ( new BasicDBObject ( "user" , username ) );
500
525
if ( o == null )
501
526
o = new BasicDBObject ( "user" , username );
502
527
o .put ( "pwd" , _hash ( username , passwd ) );
503
528
o .put ( "readOnly" , readOnly );
504
- c .save ( o );
529
+ return c .save ( o );
505
530
}
506
531
507
532
/**
508
533
* Removes a user for this db
509
534
* @param username
510
535
*/
511
- public void removeUser ( String username ){
536
+ public WriteResult removeUser ( String username ){
512
537
DBCollection c = getCollection ( "system.users" );
513
- c .remove (new BasicDBObject ( "user" , username ));
538
+ return c .remove (new BasicDBObject ( "user" , username ));
514
539
}
515
540
516
541
String _hash ( String username , char [] passwd ){
0 commit comments