public
Last active

A basic auth strategy for scalatra's scentry

  • Download Gist
AuthenticationSupport.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package org.scalatra.auth
 
import OurImplicits._
import com.mongodb.casbah.Imports._
import org.scalatra.auth.{ScentryConfig, ScentrySupport}
import org.scalatra.ScalatraKernel
 
trait AuthenticationSupport extends ScentrySupport[DBObject] with BasicAuthSupport { self: ScalatraKernel =>
 
val realm = Config.serviceName
protected def contextPath = request.getContextPath
 
protected def fromSession = { case id: String => User.findById(id) getOrElse null }
protected def toSession = { case usr: DBObject => usr.id.toString }
 
protected val scentryConfig = (new ScentryConfig {}).asInstanceOf[ScentryConfiguration]
 
override protected def registerAuthStrategies = {
 
scentry.registerStrategy('Basic, app => new OurBasicAuthStrategy(app, realm))
}
 
}
BasicAuthStrategy.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
package org.scalatra
package auth
 
import OurImplicits._
import com.mongodb.casbah.Imports._
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
import net.iharder.Base64
import org.scalatra.{ScalatraKernel}
import com.mojolly.backchat.model.User
import akka.util.Logging
import org.scalatra.auth.{ScentrySupport, ScalatraKernelProxy, ScentryStrategy}
 
 
class OurBasicAuthStrategy[DBObject](protected val app: ScalatraKernelProxy, realm: String)
extends BasicAuthStrategy(app, realm) {
 
protected def validate(userName: String, password: String): Option[UserType] = {
User.login(userName, password)
}
 
protected def getUserId(user: DBObject) = user.id.toString
}
MyApiApp.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import org.scalatra.ScalatraServlet
import org.scalatra.auth.AuthenticationSupport
 
 
class MyApiApp extends ScalatraServlet with AuthenticationSupport {
 
get("/?") {
basicAuth
<html>
<body>
<h1>Hello from Scalatra</h1>
<p><a href="/myapi/linked" >click</a></p>
</body>
</html>
}
 
get("/linked") {
basicAuth
<html>
<body>
<h1>Hello again from Scalatra</h1>
<p><a href="/" >back</a></p>
</body>
</html>
}
}
web.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?xml version="1.0"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="Backchat Streams"
version="2.5">
<!--<listener>-->
<!--<listener-class>akka.servlet.Initializer</listener-class>-->
<!--</listener>-->
 
 
<servlet>
<servlet-name>MyApiApp</servlet-name>
<servlet-class>org.scalatra.auth.example.HttpStreamApp</servlet-class>
</servlet>
 
<servlet-mapping>
<servlet-name>MyApiApp</servlet-name>
<url-pattern>/stream/*</url-pattern>
</servlet-mapping>
 
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/images/*</url-pattern>
<url-pattern>/css/*</url-pattern>
<url-pattern>/js/*</url-pattern>
</servlet-mapping>
 
</web-app>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.