Search code examples
scalaplayframeworksqueryl

squeryl ORM in Play with SHA1 password encryption


I am trying to insert username and encrypted password pairs into database table using squeryl ORM framework. My code basically looks like the following:

      class SHA1(e: StringExpression[String], m:OutMapper[String]) extends FunctionNode[String]("sha1", Some(m), Seq(e)) with StringExpression[String]

      def sha1(e:StringExpression[String])(implicit m:OutMapper[String]) = new SHA1(e,m)
      transaction{
        val foo = TestUser.userTable insert User("test@domain.com", sha1("password"))
      }

But this does not work. I got an error saying:

type mismatch; found : controllers.SHA1 required: String Error occurred in an application involving default arguments. 

Solution

  • There is a limit to the magic that Squeryl can perform here. Your model class takes a String value for password, and you are passing it a value of type SHA1 (the value that is returned from the sha1 function). The scala compiler isn't going to allow that. There are a couple of options here:

    1. Hash the password before sending it to the db
    2. Insert your User with a null value for the password, then update it in the same transaction with the password hashed via your function
    3. If you are using Squeryl 0.9.6-SNAPSHOT, you might create a SHAString custom type that handles hashing the string when it is sent to the DB

    To hash the password before you insert it, look at java.security.MessageDigest and see this answer.