Modifying data before serializing

28 views
Skip to first unread message

Nolan Darilek

unread,
Jul 16, 2012, 11:43:54 AM7/16/12
to scala...@googlegroups.com
I'm trying to port an authentication system to MongoDB+Salat and am new
to Salat. My system uses a variety of authentication tokens, so I have:

@Salat
sealed trait Token {
val credentials:String
}

I next want to create a PasswordToken that accepts the password in its
constructor, but hashes it as credentials.

case class PasswordToken(
@transient val password:String = ""
) extends Token {
override val credentials = BCrypt.hashpw(password, BCrypt.gensalt())
}

This doesn't work, though, I get an object with my password as
"password" in plaintext and no credentials.

Is there any way to achieve this in Salat? Ideally I'd like for all of
my tokens to store their value in "credentials", then have various
getters on the case class instances that pull the data out if necessary.
But at the very least I'd like to have my case class hash the password
itself rather than doing it beforehand.

Thanks.

rose katherine toomey

unread,
Jul 16, 2012, 4:00:57 PM7/16/12
to scala...@googlegroups.com
Hi Nolan,

Well, this is more OO-calisthenics that anything to do with Salat.
Normally this could be done with an apply on the companion object, but
both the input and the output are typed to String, you would get an
error about defining "apply" twice. So try a factory method of some
type for creating the object:

scala> :paste
// Entering paste mode (ctrl-D to finish)

sealed trait Token {
def credentials: String
}

object PasswordToken {

def hash(s: String) = s.reverse

def create(password: String): PasswordToken =
PasswordToken(credentials = hash(password))
}

case class PasswordToken(credentials: String) extends Token


// Exiting paste mode, now interpreting.

defined trait Token
defined module PasswordToken
defined class PasswordToken

scala> PasswordToken("secure") // doesn't hash password
res0: PasswordToken = PasswordToken(secure)

scala> PasswordToken.create("secure") // does hash password
res1: PasswordToken = PasswordToken(eruces)


Best,
Rose
Reply all
Reply to author
Forward
0 new messages