Created
December 7, 2010 20:26
-
-
Save casualjim/732347 to your computer and use it in GitHub Desktop.
A basic auth strategy for scalatra's scentry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment