casualjim
12/7/2010 - 8:26 PM

A basic auth strategy for scalatra's scentry

A basic auth strategy for scalatra's scentry

<?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>
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>
  }
}
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
}
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))
  }

}