A simple question that I can’t figure out (nowhere in Google or documentation – perhaps its obvious, but not to me). I am using slick with Play! Framework 2.1 and I want to save some sensitive user information in database. I want to encrypt the data everytime it’s about to be saved (and in memory, when constructed in a case class) and decrypt the data everytime it’s needed. The data is an OAuth access_token.
Here’s my (simplified) code:
case class User(id: Option[Int] = None,
name: String,
email: String,
oauthToken: Option[String] = None)
The object:
object Users extends Table[User]("User") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def email = column[String]("email")
def oauthToken = column[String]("oauthToken", O.Nullable)
def * = id.? ~
name ~
email ~
oauthToken.? <> (User, User.unapply _)
}
What I want to do is to encrypt the oauthToken everytime the User case class is constructed. and decrypt it everytime user.oauthToken is called. For now my best guess is to change the getter and setter of oauthToken, but as far as I know, we can’t really do that in Scala (not to mention the effect on Slick).
What should I do to get encryption/decryption on field?
Thanks before.
Something like this should do the trick