Skip to content

Instantly share code, notes, and snippets.

@mrfyda
Created November 19, 2013 23:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrfyda/7554222 to your computer and use it in GitHub Desktop.
Save mrfyda/7554222 to your computer and use it in GitHub Desktop.
A SecureString Example
package model
import model.traits.BaseTable
import model.traits.SecureTable
import play.api.libs.Crypto
import scala.slick.driver.PostgresDriver.simple._
case class LoveLetter(id: Long, fromId: Long, toId: Long, content: SecureString)
object LoveLetterFactory {
val apply = {
LoveLetter.apply(-1, _: Long, _: Long, _: SecureString)
}
def unapply(l: LoveLetter) = {
Some((l.fromId, l.toId, l.content))
}
}
object LoveLetterTable extends Table[LoveLetter]("LoveLetter") with BaseTable[LoveLetter] with SecureTable {
def fromId = column[Long]("from", O.NotNull)
def toId = column[Long]("to", O.NotNull)
def content = column[SecureString]("content", O.NotNull)
def * = id ~ fromId ~ toId ~ content <> (LoveLetter, LoveLetter.unapply _)
def auto = fromId ~ toId ~ content <> (LoveLetterFactory.apply, LoveLetterFactory.unapply _)
}
CREATE TABLE "LoveLetter" (
"id" SERIAL PRIMARY KEY,
"from" int REFERENCES "Person",
"to" int REFERENCES "Person",
"content" text NOT NULL
);
package model.traits
import play.api.libs.Crypto
case class SecureString(str: String) {
override def toString: String = str
def cipher: String = Crypto.encryptAES(str)
}
object SecureStringFactory {
def decipher(str: String): SecureString = SecureString(Crypto.decryptAES(str))
}
trait SecureTable {
implicit val secureStringMapper = MappedTypeMapper.base[SecureString, String](
{
secure => secure.cipher
}, {
string => SecureStringFactory.decipher(string)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment