Skip to content

Commit

Permalink
Added configuring credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
jroper committed Mar 21, 2012
1 parent 29e1896 commit 58a042c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/scala/play/modules/mongodb/jackson/MongoDB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ class MongoDBPlugin(val app: Application) extends Plugin {
val dbName = app.configuration.getString("mongodb.database").getOrElse("play")
val db = mongo.getDB(dbName)

// Authenticate if necessary
val credentials = app.configuration.getString("mongodb.credentials")
if (credentials.isDefined) {
credentials.get.split(":", 2) match {
case Array(username: String, password: String) => {
if (!db.authenticate(username, password.toCharArray)) {
throw new IllegalArgumentException("MongoDB authentication failed for user: " + username + " on database: "
+ dbName);
}
}
case _ => throw new IllegalArgumentException("mongodb.credentials must be a username and password separated by a colon")
}
}

// Configure the object mapper
var mapper = new ObjectMapper;
mapper = mapper.withModule(MongoJacksonMapperModule.INSTANCE)
Expand Down
11 changes: 11 additions & 0 deletions src/test/scala/play/modules/mongodb/jackson/MongoDBSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ class MongoDBSpec extends Specification {
}
}

/*
// This is commented out because it's not usual for me to have a MongoDB database requiring authentication.
"be configurable with authentication" in new Setup {
implicit val app = fakeApp(Map("mongodb.credentials" -> "user:password"))
running(app) {
val coll = MongoDB.collection(collName, classOf[MockObject], classOf[String])
coll.count mustEqual 0
}
}
*/

"be configurable as a replica set" in new Setup {
implicit val app = fakeApp(Map("mongodb.servers" -> "localhost:27017,localhost:27017"))
running(app) {
Expand Down

0 comments on commit 58a042c

Please sign in to comment.