Attic/programs/Application4.scala
author cu
Fri, 27 Oct 2017 23:57:21 +0100
changeset 558 86334134abe5
parent 198 2ce98ee39990
permissions -rw-r--r--
updated cw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
package controllers
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import play.api._
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import play.api.mvc._
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import play.api.data._
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
import play.api.data.Forms._
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
import java.security.MessageDigest
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
/*
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
 * Application sets a cookie in plain ASCII on the
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
 * clients browser recording the visits of a page.
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
 *
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
 * The cookie data is hashed and salted with a
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
 * secret key.
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
 */
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
object Application extends Controller {
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  //secret key for salting - this key should not be
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  //sent to the client; the key should normally be
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  //a unguessable random number generated once
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  val salt = "my secret key"
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  //SHA-1 + salt
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  def mk_hash(s: String) : String = {
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
    val hash_fun = MessageDigest.getInstance("SHA-1")
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
    hash_fun.digest((s + salt).getBytes).map{ "%02x".format(_) }.mkString
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  }
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  def gt_cookie(c: Option[Cookie]) : Int = 
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
    c.map(_.value.split("/")) match {
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
      case Some(Array(s, h)) 
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
        if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt 
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
      case _ => 0
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    }
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  def mk_cookie(i: Int) : Cookie = {
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
    val s = i.toString
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
    Cookie("visits", s + "/" + mk_hash(s))
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  }
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
   
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  def index = Action { request =>
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
    val visits_cookie = request.cookies.get("visits")
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
    val visits = gt_cookie(visits_cookie)
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
    val msg1 = "You are a valued customer who has visited this site %d times."
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
    val msg2 = "You have visited this site %d times."
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
    val msg = 
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
      if (visits >= 10) msg1.format(visits) else msg2.format(visits)
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
    
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
    Ok(msg).as(HTML).withCookies(mk_cookie(visits + 1))
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  }     
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
}
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
2bd3530c30af added programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59