public

  • Download Gist
AuthenticationSupportMixin.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
trait AuthenticationSupport
        extends ScentrySupport[ApiUser]
                with FlashMapSupport
                with CookieSupport
                with Logging { self: ScalatraKernel =>
 
  before { scentry.authenticate('RememberMe) }
 
  override protected def registerAuthStrategies = {
    scentry.registerStrategy('UserPassword, app => new UserPasswordStrategy(app))
    scentry.registerStrategy('RememberMe, app => new RememberMeStrategy(app))
  }
 
  protected def fromSession = {
    case id: String => { /* Serialize user from session */ }
  }
  protected def toSession = {
    case usr: ApiUser => usr.id
  }
}
RememberMeStrategy.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 27 28 29 30 31 32 33 34 35 36
object RememberMeStrategy {
  class RememberMeStrategy(protected val app: ScalatraKernelProxy)
          extends ScentryStrategy[ApiUser] with Logging {
    val COOKIE_KEY = "scentry.auth.remember"
 
    private val oneWeek = 7 * 24 * 3600
 
    override def valid_? = {
      app.cookies(COOKIE_KEY).isDefined
    }
 
    override def afterAuthenticate(winningStrategy: Symbol, user: ApiUser) = {
      log debug "Executing after authenticate in remember me strategy with winning strategy [%s] and user [%s]".format(winningStrategy, user)
      if (winningStrategy == 'RememberMe ||
              (winningStrategy == 'UserPassword && checkbox2boolean(app.params.get("remember").getOrElse("")))) {
        log debug "Remembering user [%s]".format(user.email)
        // val token = fetchTokenFromBackend
        // app.cookies.update(COOKIE_KEY, token, CookieOptions( path = "/", maxAge = oneWeek ))
      }
    }
 
    def authenticate_! = {
      log debug "Authenticating in Remember me strategy"
      app.cookies(COOKIE_KEY) flatMap { token =>
        log debug "Authenticating with token [%s]".format(token)
        // Validate remember me token here
      }
    }
 
    override def beforeLogout(user: ApiUser) = {
      // Perform backend cleanup on logout
      app.cookies(COOKIE_KEY) foreach { _ => app.cookies.update(COOKIE_KEY, null) }
      log info "Removed cookie for user [%s]".format(user)
    }
  }
}
UserPasswordStrategy.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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
object UserPasswordStrategy {
  class UserPasswordStrategy(protected val app: ScalatraKernelProxy)
          extends ScentryStrategy[ApiUser]
          with Logging
  {
 
    private def email = app.params.get("email")
    private def password = app.params.get("password")
 
    private def remoteAddress ={
      val proxied = app.request.getHeader("X-FORWARDED-FOR")
      val res = if (proxied.nonBlank_?) proxied else app.request.getRemoteAddr
      log debug "The remote address is: %s".format(res)
      res
    }
 
    override def valid_? = {
      email.isDefined && password.isDefined
    }
 
    override def authenticate_! = {
      log debug "Authenticating in UserPasswordStrategy with: %s, %s, %s".format(email, password, remoteAddress)
      // perform authentication against database here
    }
 
 
  }
 
  trait UserPasswordMixin { self: ScalatraServlet =>
 
    before {
      log info "Executing before filter in user password filter"
    }
 
    get("/signup") {
      redirectIfAuthenticated_!
      // Show signup view
    }
 
    post( "/signup") {
      // Perform signup and redirect to login
    }
 
    get("/confirm/:token?") {
      redirectIfAuthenticated_!
      val token = params("token")
      if (token.blank_?) {
        flash.update("error", "The token is missing from the url.")
        redirect("/")
      } else {
        // perform confirmation logic here
        redirect("/")
      }
    }
 
    get("/login") {
      redirectIfAuthenticated_!
      // Show login view here
    }
 
    post("/login") {
      authenticate
      redirectIfAuthenticated_!
      flash.update("error", "There was a problem with your username and/or password.")
      redirect("/login")
    }
 
    get("/logout") {
      // logout logic here
      flash.update("success", "You have been logged out")
      redirectIfAuthenticated_!
      redirect("/")
    }
 
    get("/forgot") {
      redirectIfAuthenticated_!
      // show forgot password form
    }
 
    post("/forgot") {
      redirectIfAuthenticated_!
      // Create email and send
    }
 
    get("/reset/:token?") {
      def missingToken = {
        flash.update("error", "The token is missing from the url.")
        redirect("/")
      }
      redirectIfAuthenticated_!
      params.get("token") match {
        case None => missingToken
        case Some(token) => if (token.blank_?) {
          missingToken
        } else {
          // Verify the token
        }
      }
    }
 
    post("/reset") {
      redirectIfAuthenticated_!
      // Perform logic here to reset the password
    }
  }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.