Skip to content

Instantly share code, notes, and snippets.

@troystribling
Last active January 1, 2017 08:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troystribling/4678051 to your computer and use it in GitHub Desktop.
Save troystribling/4678051 to your computer and use it in GitHub Desktop.
Scalatra 2.2 Sentry implemented for username/password and cookie authentication.
package lib
import org.scalatra._
import org.scalatra.util.RicherString._
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
import org.scalatra.auth.{ScentrySupport, ScentryStrategy}
import net.iharder.Base64
import java.util.Locale
import io.Codec
abstract class RememberMeAuthStrategy[UserType <: AnyRef](protected val app:ScalatraBase with CookieSupport)
extends ScentryStrategy[UserType] {
val cookieKey = "RememberMeAuth"
override val name = "RememberMeAuth"
protected def setAuthToken(user:UserType) : Unit
protected def authenticate(token:String) : Option[UserType]
override def isValid = {
app.cookies.get(cookieKey) match {
case None => false
case _ => true
}
}
override def afterAuthenticate(winningStrategy:String, user:UserType) {
if (winningStrategy != name) {
setAuthToken(user)
}
}
override def beforeLogout(user:UserType) {
app.cookies.delete(cookieKey)
}
def authenticate() = {
authenticate(app.cookies(cookieKey))
}
}
package app
import org.scalatra._
import org.scalatra.auth.{ScentrySupport, ScentryConfig}
import models._
import lib._
class UserAuthUserPasswordAuthStrategy(protected override val app: ScalatraBase) extends UserPasswordAuthStrategy[User](app) {
override val name = "UserAuthUserPasswordAuth"
protected def authenticate(username: String, password: String) = {
SystemAdministratorsCollection.findFirstByUserId(username) match {
case Some(user) => user.authenticate(password)
case None => None
}
}
}
class UserAuthRemeberMeAuthStrategy(protected override val app: ScalatraBase with CookieSupport) extends RememberMeAuthStrategy[User](app) {
override val name = "UserAuthRemeberMeAuth"
override val cookieKey = "com.company.user"
protected def setAuthToken(user:SystemAdministrator) {
app.cookies.set(cookieKey, user.authToken)(CookieOptions(path="/", maxAge=30*24*3600))
}
protected def authenticate(token:String) = {
SystemAdministratorsCollection.findFirstByAuthToken(token)
}
}
trait UserAuthSupport extends ScentrySupport[User] with CookieSupport {
self: (ScalatraBase with ScentrySupport[User]) =>
protected def authenticateUserWithPassword() = {
scentry.authenticate("UserAuthUserPasswordAuth")
}
protected def authenticateUserWithCookie() = {
scentry.authenticate("UserAuthRemeberMeAuth")
}
protected def fromSession = {
case id:String => {
SystemAdministratorsCollection.findFirstByUserId(id) match {
case Some(user) => user
case None => null.asInstanceOf[SystemAdministrator]
}
}
}
protected def toSession = {
case usr:SystemAdministrator => usr.userId
}
protected val scentryConfig = (new ScentryConfig {}).asInstanceOf[ScentryConfiguration]
override protected def registerAuthStrategies = {
scentry.register("UserAuthUserPasswordAuth", app => new SystemAdministratorUserPasswordAuthStrategy(app))
scentry.register("UserAuthRemeberMeAuth", app => new SystemAdministratorRemeberMeAuthStrategy(app.asInstanceOf[ScalatraBase with CookieSupport]))
}
}
package lib
import org.scalatra._
import org.scalatra.ScalatraKernel
import org.scalatra.util.RicherString._
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
import org.scalatra.auth.{ScentrySupport, ScentryStrategy}
import java.util.Locale
import io.Codec
abstract class UserPasswordAuthStrategy[UserType <: AnyRef](protected val app:ScalatraBase)
extends ScentryStrategy[UserType] {
private def username = app.params.get("username")
private def password = app.params.get("password")
protected def authenticate(username: String, password: String): Option[UserType]
override val name = "UserPasswordAuth"
override def isValid = {
username.isDefined && password.isDefined
}
def authenticate() = {
authenticate(username.get, password.get)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment