

package io.restassured.scalatra

import org.scalatra.ScalatraServlet

import scala.collection.mutable
import scala.util.Random

class CustomAuthExample extends ScalatraServlet {

  val authenticatedSessions = new mutable.HashMap[String, Int]()

  before() {
    contentType = "application/json"
  }

  post("/login") {
    val rand = new Random(System.currentTimeMillis())
    val operandA = rand.nextInt(1000)
    val operandB = rand.nextInt(1000)
    val expectedSum = operandA + operandB
    val id = rand.nextLong().toString
    authenticatedSessions += id -> expectedSum
    "{ \"operandA\" : "+operandA + ", \"operandB\" : "+operandB + ", \"id\" : \""+id+"\" }"
  }

  get("/secretMessage") {
    returnIfLoggedIn("I'm secret")
  }

  get("/secretMessage2") {
    returnIfLoggedIn("I'm also secret")
  }

  private def returnIfLoggedIn(message: => String) : String = {
    val actualSum = request.getParameter("sum")
    val id = request.getParameter("id")
    val expectedSum = authenticatedSessions.getOrElse(id, -1)
    if (actualSum == null || id == null || expectedSum == -1 || actualSum.toInt != expectedSum) {
      """{ "message" : "You're not authorized to see the secret message" }"""
    } else {
      "{ \"message\" : \""+message+"\" }"
    }
  }
}