Commit 9c460a4a authored by Mathieu leclaire's avatar Mathieu leclaire

Add basic DBStrategy

parent 28693bcb
......@@ -49,6 +49,7 @@ lazy val server = project.in(file("server")) settings(
"com.lihaoyi" %% "upickle" % upickleVersion,
"com.lihaoyi" %% "scalatags" % scalatagsVersion,
"org.scalatra" %% "scalatra" % scalatraVersion,
"org.scalatra" %% "scalatra-auth" % scalatraVersion,
"ch.qos.logback" % "logback-classic" % "1.1.3" % "runtime",
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided",
"org.eclipse.jetty" % "jetty-webapp" % jettyVersion,
......
package server
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import org.openmole.gate.server.Data.User
import org.scalatra.auth.strategy.{BasicAuthStrategy}
import org.scalatra.ScalatraBase
class DBStrategy(protected override val app: ScalatraBase, realm: String) extends BasicAuthStrategy[User](app, realm) {
override protected def getUserId(user: User)(implicit request: HttpServletRequest, response: HttpServletResponse): String = user.login
override protected def validate(userName: String, password: String)(implicit request: HttpServletRequest, response: HttpServletResponse): Option[User] = {
//Check whether user is in DB
//stub
if (userName == "scalatra" && password == "scalatra") Some(User(userName))
else None
}
}
\ No newline at end of file
package org.openmole.gate.server
import org.scalatra._
import org.scalatra.auth.{ScentryConfig, ScentrySupport}
import org.scalatra.auth.strategy.BasicAuthSupport
import server.DBStrategy
import scala.concurrent.duration._
import scala.concurrent.Await
import scalatags.Text.all._
......@@ -13,13 +17,12 @@ object AutowireServer extends autowire.Server[String, upickle.default.Reader, up
def write[Result: upickle.default.Writer](r: Result) = upickle.default.write(r)
}
class Servlet extends ScalatraServlet {
class Servlet extends ScalatraServlet with AuthenticationSupport {
val basePath = "shared"
get("/") {
contentType = "text/html"
tags.html(
tags.head(
tags.meta(tags.httpEquiv := "Content-Type", tags.content := "text/html; charset=UTF-8"),
......@@ -27,6 +30,7 @@ class Servlet extends ScalatraServlet {
tags.script(tags.`type` := "text/javascript", tags.src := "js/gate.js")
),
tags.body(
div("authenticated: " + basicAuth),
script("client.Gate.run();")
)
)
......@@ -40,3 +44,37 @@ class Servlet extends ScalatraServlet {
}
}
object Data {
type Login = String
type Password = String
case class User(login: Login)
}
trait AuthenticationSupport extends ScentrySupport[Data.User] with BasicAuthSupport[Data.User] {
this: Servlet
val realm = "Gate authentication"
protected def fromSession = {
case id: String Data.User(id)
}
protected def toSession = {
case usr: Data.User usr.login
}
protected val scentryConfig = (new ScentryConfig {}).asInstanceOf[ScentryConfiguration]
override protected def configureScentry = {
scentry.unauthenticated {
scentry.strategies("DB").unauthenticated()
}
}
override protected def registerAuthStrategies = {
scentry.register("DB", app new DBStrategy(app, realm))
}
}
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