Created
November 19, 2013 23:07
-
-
Save mrfyda/7554222 to your computer and use it in GitHub Desktop.
A SecureString Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 _) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE "LoveLetter" ( | |
"id" SERIAL PRIMARY KEY, | |
"from" int REFERENCES "Person", | |
"to" int REFERENCES "Person", | |
"content" text NOT NULL | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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