Attic/scala/Application4.scala
author cu
Sat, 30 Sep 2017 05:28:58 +0100
changeset 540 441a1e662f28
parent 198 2ce98ee39990
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
103
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
package controllers
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
import play.api.mvc._
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
import java.security.MessageDigest
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
/*
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
 * Application sets a cookie in plain ASCII on the
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
 * clients browser recording the visits of a page.
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
 *
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
 * The cookie data is hashed and salted with a
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
 * secret key.
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
 */
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
object Application extends Controller {
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  //secret key for salting 
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  val salt = "my secret key"
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  //SHA-1 + salt
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
  def mk_hash(s: String) : String = {
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
    val hash_fun = MessageDigest.getInstance("SHA-1")
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
    hash_fun.digest((s + salt).getBytes).map{ "%02x".format(_) }.mkString
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
  }
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  def gt_cookie(c: Cookie) : Int = c.value.split("/") match {
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
    case Array(s, h) if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt 
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
    case _ => 0
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  }
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  def mk_cookie(i: Int) : Cookie = {
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    val hash = mk_hash(i.toString)
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
    Cookie("visits", s"$i/$hash")
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  }
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
   
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  def index = Action { request =>
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
    val visits_cookie = request.cookies.get("visits")
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
    val visits = visits_cookie.map(gt_cookie).getOrElse(0)
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
    val msg = 
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
      if (visits >= 10)
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
        s"You are a valued customer who has visited this site $visits times."               
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
      else s"You have visited this site $visits times."
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
    
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
    Ok(msg).withCookies(mk_cookie(visits + 1))
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  }     
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
}
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52