Commit 64b96b20 authored by Julien Perret's avatar Julien Perret

handling user credentials i URI

parent 7d1a0298
...@@ -13,24 +13,28 @@ import org.scalatra.servlet.ScalatraListener ...@@ -13,24 +13,28 @@ import org.scalatra.servlet.ScalatraListener
import server.Launcher.MongoDB import server.Launcher.MongoDB
object Launcher extends App { object Launcher extends App {
case class MongoDB(host: String, port: Int, db: String, dataSet: String) case class MongoDB(user: String, password:String, host: String, port: Int, db: String, dataSet: String)
val mongoDBArguments = "mongoDBArguments" val mongoDBArguments = "mongoDBArguments"
val option = args.headOption val option = args.headOption
val mongoURI = new URI(option.getOrElse("mongodb://localhost:27017/openmole?login")) val mongoURI = new URI(option.getOrElse("mongodb://openmole:rocks@localhost:27017/openmole?login"))
println(mongoURI) println(mongoURI)
val mongoDB = { val mongoDB = {
println("TTTT userinfo = " + mongoURI.getUserInfo)
val userInfos = mongoURI.getUserInfo.split(':')
val user = userInfos(0)
val password = userInfos(1)
val host = mongoURI.getHost val host = mongoURI.getHost
println(host) println("TTTT host = " + host)
val port = mongoURI.getPort val port = mongoURI.getPort
println(port) println(port)
println("path=" + mongoURI.getPath) println("TTTT path=" + mongoURI.getPath)
val db = mongoURI.getPath.drop(1) val db = mongoURI.getPath.drop(1)
println("query=" + mongoURI.getQuery) println("TTTT query=" + mongoURI.getQuery)
val dataSet = mongoURI.getQuery val dataSet = mongoURI.getQuery
//if (mongoURI.getHost== null || mongoURI.getHost.isEmpty) None //if (mongoURI.getHost== null || mongoURI.getHost.isEmpty) None
//else Some(MongoDB(host, port, db, dataSet)) //else Some(MongoDB(host, port, db, dataSet))
MongoDB(host, port, db, dataSet) MongoDB(user, password, host, port, db, dataSet)
} }
val server = new Server(8080) val server = new Server(8080)
val context = new WebAppContext() val context = new WebAppContext()
......
...@@ -35,11 +35,9 @@ class Servlet(mongoDB: MongoDB) extends ScalatraServlet with AuthenticationSuppo ...@@ -35,11 +35,9 @@ class Servlet(mongoDB: MongoDB) extends ScalatraServlet with AuthenticationSuppo
case class MongoData(collection: MongoCollection, userSession: DBObject) case class MongoData(collection: MongoCollection, userSession: DBObject)
def getMongoData(user:User) = { def getMongoData(user:User) = {
val userSession = MongoDBObject(user.login -> session.getId) val userSession = MongoDBObject(user.login -> session.getId)
println(mongoDB.host + " => " + mongoDB.port)
val server = new ServerAddress(mongoDB.host, mongoDB.port) val server = new ServerAddress(mongoDB.host, mongoDB.port)
val credentials = MongoCredential.createCredential("accountUser", "products", "password".toCharArray) val credentials = MongoCredential.createCredential(mongoDB.user, "admin", mongoDB.password.toCharArray)
val mongoClient = MongoClient(server, List(credentials)) val mongoClient = MongoClient(server, List(credentials))
println(mongoDB.db + " ? " + mongoDB.dataSet)
MongoData(mongoClient(mongoDB.db)(mongoDB.dataSet), userSession) MongoData(mongoClient(mongoDB.db)(mongoDB.dataSet), userSession)
} }
get("/") { get("/") {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment