diff --git a/app/controllers/Account.scala b/app/controllers/Account.scala index dfd3328c3b..d53c89f92d 100644 --- a/app/controllers/Account.scala +++ b/app/controllers/Account.scala @@ -199,7 +199,7 @@ object Account extends LilaController { } { password => Env.user.authenticator.authenticateById( me.id, - PasswordAndToken(ClearPassword(password), me.totpSecret.map(_.totpDefault)) + PasswordAndToken(ClearPassword(password), me.totpSecret.map(_.currentTotp)) ).map(_.isDefined) flatMap { case false => BadRequest(html.account.close(me, Env.security.forms.closeAccount)).fuccess case true => Env.current.closeAccount(me.id, self = true) inject { diff --git a/modules/user/src/main/TotpSecret.scala b/modules/user/src/main/TotpSecret.scala index 028eceb119..f287656159 100644 --- a/modules/user/src/main/TotpSecret.scala +++ b/modules/user/src/main/TotpSecret.scala @@ -14,9 +14,9 @@ case class TotpSecret(secret: Array[Byte]) extends AnyVal { def base32: String = new Base32().encodeAsString(secret) - def totpDefault = totp(System.currentTimeMillis / 30000) + def currentTotp = totp(System.currentTimeMillis / 30000) - def totp(period: Long): TotpToken = TotpToken { + private def totp(period: Long): TotpToken = TotpToken { val msg = BigInt(period).toByteArray.reverse.padTo(8, 0.toByte).reverse val hmac = Mac.getInstance("HMACSHA1") diff --git a/modules/user/src/test/TotpTest.scala b/modules/user/src/test/TotpTest.scala index f984c900f4..820280c454 100644 --- a/modules/user/src/test/TotpTest.scala +++ b/modules/user/src/test/TotpTest.scala @@ -1,6 +1,7 @@ package lila.user import org.specs2.mutable.Specification +import User.TotpToken class TotpTest extends Specification { @@ -12,15 +13,15 @@ class TotpTest extends Specification { "authenticate" in { val secret = TotpSecret.random - val token = secret.totp(System.currentTimeMillis / 30000) + val token = secret.currentTotp secret.verify(token) must beTrue } "not authenticate" in { val secret = TotpSecret("1234567890123456") - secret.verify("") must beFalse - secret.verify("000000") must beFalse - secret.verify("123456") must beFalse + secret.verify(TotpToken("")) must beFalse + secret.verify(TotpToken("000000")) must beFalse + secret.verify(TotpToken("123456")) must beFalse } } }