Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when implementing my own hasher. #176

Closed
jakob85 opened this issue Mar 4, 2013 · 3 comments
Closed

Error when implementing my own hasher. #176

jakob85 opened this issue Mar 4, 2013 · 3 comments

Comments

@jakob85
Copy link

jakob85 commented Mar 4, 2013

Hello!
First of all thank you for a great plugin!
I'm trying to implement my own hasher but I'm getting a pretty strange error.

My Hasher:

class MyPasswordHasher(app : Application) extends PasswordHasher {

  def hash(plainPassword: String): PasswordInfo = {
    PasswordInfo("my_hasher", MD5(plainPassword + "tut"), Some("tut"))
  }

  def matches(passwordInfo: PasswordInfo, suppliedPassword: String): Boolean = {
    val hp = MD5(suppliedPassword + passwordInfo.salt.get)
    passwordInfo.password == hp
  }

  def id: String = "my_hasher"

  def MD5(s : String) = {
    val bytes = MessageDigest.getInstance("MD5").digest(s.getBytes());
    bytes.map{ b => String.format("%02X", new java.lang.Integer(b & 0xff)) }.mkString
  }
}

Check the log that the hasher is loaded:

[info] application - [securesocial] loaded user service: class services.MyDbUserService
[info] application - [securesocial] loaded password hasher my_hasher

Then I do the signup flow and I get:

[debug] application - [securesocial] trying sign up with token 3551e792-711e-427a-b819-b649d92646d9
[error] application - [securesocial] can't find password hasher for id bcrypt
[error] play - Cannot invoke the action, eventually got an error: java.util.NoSuchElementException: None.get
[error] application - 

! @6dg5l2col - Internal server error, for (POST) [/signup/3551e792-711e-427a-b819-b649d92646d9] ->

play.api.Application$$anon$1: Execution exception[[NoSuchElementException: None.get]]
    at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.0]
    at play.api.DefaultApplication.handleError(Application.scala:383) [play_2.10.jar:2.1.0]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$handleAction$1$4$$anonfun$apply$28.apply(PlayDefaultUpstreamHandler.scala:391) [play_2.10.jar:2.1.0]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$handleAction$1$4$$anonfun$apply$28.apply(PlayDefaultUpstreamHandler.scala:391) [play_2.10.jar:2.1.0]
    at scala.Option.map(Option.scala:145) [scala-library.jar:na]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$handleAction$1$4.apply(PlayDefaultUpstreamHandler.scala:391) [play_2.10.jar:2.1.0]
java.util.NoSuchElementException: None.get
    at scala.None$.get(Option.scala:313) ~[scala-library.jar:na]
    at scala.None$.get(Option.scala:311) ~[scala-library.jar:na]
    at securesocial.core.Registry$$anon$1.currentHasher$lzycompute(Registry.scala:60) ~[securesocial_2.10-master-SNAPSHOT.jar:master-SNAPSHOT]
    at securesocial.core.Registry$$anon$1.currentHasher(Registry.scala:60) ~[securesocial_2.10-master-SNAPSHOT.jar:master-SNAPSHOT]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_37]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_37]

Hmm why do I get [error] application - [securesocial] can't find password hasher for id bcrypt
Ok let's try to change the name from my_hasher to bcrypt

[info] application - [securesocial] loaded user service: class services.MyDbUserService
[info] application - [securesocial] loaded password hasher bcrypt

[debug] application - [securesocial] trying sign up with token 8c3852fd-fc54-46fb-82bd-efeb1d83a40d
[info] application - Some(PasswordInfo(bcrypt,10B88F75651BD2973904799EB7DCA5A4,Some(tut)))

And it works... The salt, md5 and db save
What am I missing here? Is there something that I have forgotten to implement?

@jaliss
Copy link
Owner

jaliss commented Mar 4, 2013

Did you set the securesocial.userpass.hasher property to "my_hasher"? That property specifies the hasher that will be used in new registrations or password changes. You can add several hashers in the play.plugins file, but the one specified with that property is the default. This allows for easy migration if you need to change the algorithm.

@jakob85
Copy link
Author

jakob85 commented Mar 4, 2013

Aha! Perfect! Thank you!

@jaliss
Copy link
Owner

jaliss commented Mar 4, 2013

You're welcome :)

@jaliss jaliss closed this as completed Mar 4, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants