Skip to content

Commit 56e7a91

Browse files
committed
[JAVA-247]: additional authenticate method
1 parent 97973aa commit 56e7a91

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

src/main/com/mongodb/DB.java

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -424,19 +424,48 @@ public boolean isAuthenticated() {
424424
public boolean authenticate(String username, char[] passwd )
425425
throws MongoException {
426426

427-
if ( username == null )
427+
if ( username == null || passwd == null )
428428
throw new NullPointerException( "username can't be null" );
429429

430430
if ( _username != null )
431431
throw new IllegalStateException( "can't call authenticate twice on the same DBObject" );
432432

433433
String hash = _hash( username , passwd );
434-
if ( ! _doauth( username , hash.getBytes() ) )
434+
CommandResult res = _doauth( username , hash.getBytes() );
435+
if ( !res.ok())
435436
return false;
436437
_username = username;
437438
_authhash = hash.getBytes();
438439
return true;
439440
}
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+
440469
/*
441470
boolean reauth(){
442471
if ( _username == null || _authhash == null )
@@ -465,27 +494,23 @@ static DBObject _authCommand( String nonce , String username , byte[] hash ){
465494
return cmd;
466495
}
467496

468-
private boolean _doauth( String username , byte[] hash ){
497+
private CommandResult _doauth( String username , byte[] hash ){
469498
CommandResult res = command(new BasicDBObject("getnonce", 1));
470-
471499
if ( ! res.ok() ){
472-
throw new MongoException("Error - unable to get nonce value for authentication.");
500+
throw new MongoException(res);
473501
}
474502

475503
DBObject cmd = _authCommand( res.getString( "nonce" ) , username , hash );
476-
477-
res = command(cmd);
478-
479-
return res.ok();
504+
return command(cmd);
480505
}
481506

482507
/**
483508
* Adds a new user for this db
484509
* @param username
485510
* @param passwd
486511
*/
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);
489514
}
490515

491516
/**
@@ -494,23 +519,23 @@ public void addUser( String username , char[] passwd ){
494519
* @param passwd
495520
* @param readOnly if true, user will only be able to read
496521
*/
497-
public void addUser( String username , char[] passwd, boolean readOnly ){
522+
public WriteResult addUser( String username , char[] passwd, boolean readOnly ){
498523
DBCollection c = getCollection( "system.users" );
499524
DBObject o = c.findOne( new BasicDBObject( "user" , username ) );
500525
if ( o == null )
501526
o = new BasicDBObject( "user" , username );
502527
o.put( "pwd" , _hash( username , passwd ) );
503528
o.put( "readOnly" , readOnly );
504-
c.save( o );
529+
return c.save( o );
505530
}
506531

507532
/**
508533
* Removes a user for this db
509534
* @param username
510535
*/
511-
public void removeUser( String username ){
536+
public WriteResult removeUser( String username ){
512537
DBCollection c = getCollection( "system.users" );
513-
c.remove(new BasicDBObject( "user" , username ));
538+
return c.remove(new BasicDBObject( "user" , username ));
514539
}
515540

516541
String _hash( String username , char[] passwd ){

src/main/com/mongodb/MapReduceCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public void setOutputDB(String outputDB) {
224224
}
225225

226226

227-
DBObject toDBObject() {
227+
public DBObject toDBObject() {
228228
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
229229

230230
builder.add("mapreduce", _input)

0 commit comments

Comments
 (0)