<commit_before>package lila.security

import play.api.data._
import play.api.data.Forms._
import play.api.data.validation.Constraints

import lila.common.{ LameName, EmailAddress }
import lila.user.{ User, TotpSecret, UserRepo }
import User.{ ClearPassword, TotpToken }

final class DataForm(
    val captcher: akka.actor.ActorSelection,
    authenticator: lila.user.Authenticator,
    emailValidator: EmailAddressValidator
) extends lila.hub.CaptchedForm {

  import DataForm._

  case class Empty(gameId: String, move: String)

  val empty = Form(mapping(
    "gameId" -> text,
    "move" -> text
  )(Empty.apply)(_ => None)
    .verifying(captchaFailMessage, validateCaptcha _))

  def emptyWithCaptcha = withCaptcha(empty)

  private val anyEmail = trimField(text).verifying(Constraints.emailAddress)
  private val acceptableEmail = anyEmail.verifying(emailValidator.acceptableConstraint)
  private def acceptableUniqueEmail(forUser: Option[User]) =
    acceptableEmail.verifying(emailValidator uniqueConstraint forUser)

  private def withAcceptableDns(m: Mapping[String]) = m verifying emailValidator.withAcceptableDns

  private def trimField(m: Mapping[String]) = m.transform[String](_.trim, identity)

  private val preloadEmailDnsForm = Form(single("email" -> acceptableEmail))

  def preloadEmailDns(implicit req: play.api.mvc.Request[_]): Funit =
    preloadEmailDnsForm.bindFromRequest.fold(
      _ => funit,
      email => emailValidator.preloadDns(EmailAddress(email))
    )

  object signup {

    private val username = trimField(nonEmptyText).verifying(
      Constraints minLength 2,
      Constraints maxLength 20,
      Constraints.pattern(
        regex = User.newUsernamePrefix,
        error = "usernamePrefixInvalid"
      ),
      Constraints.pattern(
        regex = User.newUsernameSuffix,
        error = "usernameSuffixInvalid"
      ),
      Constraints.pattern(
        regex = User.newUsernameChars,
        error = "usernameCharsInvalid"
      )
    ).verifying("usernameUnacceptable", u => !LameName.username(u))
      .verifying("usernameAlreadyUsed", u => !UserRepo.nameExists(u).awaitSeconds(4))

    val website = Form(mapping(
      "username" -> username,
      "password" -> text(minLength = 4),
      "email" -> withAcceptableDns(acceptableUniqueEmail(none)),
      "fp" -> optional(nonEmptyText),
      "g-recaptcha-response" -> optional(nonEmptyText)
    )(SignupData.apply)(_ => None))

    val mobile = Form(mapping(
      "username" -> username,
      "password" -> text(minLength = 4),<commit_msg>trim auto forms username fields<commit_after>package lila.security

import play.api.data._
import play.api.data.Forms._
import play.api.data.validation.Constraints

import lila.common.{ LameName, EmailAddress }
import lila.user.{ User, TotpSecret, UserRepo }
import User.{ ClearPassword, TotpToken }

final class DataForm(
    val captcher: akka.actor.ActorSelection,
    authenticator: lila.user.Authenticator,
    emailValidator: EmailAddressValidator
) extends lila.hub.CaptchedForm {

  import DataForm._

  case class Empty(gameId: String, move: String)

  val empty = Form(mapping(
    "gameId" -> text,
    "move" -> text
  )(Empty.apply)(_ => None)
    .verifying(captchaFailMessage, validateCaptcha _))

  def emptyWithCaptcha = withCaptcha(empty)

  private val anyEmail = trimField(text).verifying(Constraints.emailAddress)
  private val acceptableEmail = anyEmail.verifying(emailValidator.acceptableConstraint)
  private def acceptableUniqueEmail(forUser: Option[User]) =
    acceptableEmail.verifying(emailValidator uniqueConstraint forUser)

  private def withAcceptableDns(m: Mapping[String]) = m verifying emailValidator.withAcceptableDns

  private def trimField(m: Mapping[String]) = m.transform[String](_.trim, identity)

  private val preloadEmailDnsForm = Form(single("email" -> acceptableEmail))

  def preloadEmailDns(implicit req: play.api.mvc.Request[_]): Funit =
    preloadEmailDnsForm.bindFromRequest.fold(
      _ => funit,
      email => emailValidator.preloadDns(EmailAddress(email))
    )

  object signup {

    private val username = trimField(nonEmptyText).verifying(
      Constraints minLength 2,
      Constraints maxLength 20,
      Constraints.pattern(
        regex = User.newUsernamePrefix,
        error = "usernamePrefixInvalid"
      ),
      Constraints.pattern(
        regex = User.newUsernameSuffix,
        error = "usernameSuffixInvalid"
      ),
      Constraints.pattern(
        regex = User.newUsernameChars,
        error = "usernameCharsInvalid"
      )
    ).verifying("usernameUnacceptable", u => !LameName.username(u))
      .verifying("usernameAlreadyUsed", u => !UserRepo.nameExists(u).awaitSeconds(4))

    val website = Form(mapping(
      "username" -> trimField(username),
      "password" -> text(minLength = 4),
      "email" -> withAcceptableDns(acceptableUniqueEmail(none)),
      "fp" -> optional(nonEmptyText),
      "g-recaptcha-response" -> optional(nonEmptyText)
    )(SignupData.apply)(_ => None))

    val mobile = Form(mapping(
      "username" -> trimField(username),
      "password" -> text(minLength = 4),
{'fraction_non_alphanumeric': 0.11063498246980911, 'fraction_numerical': 0.002337358784573432, 'mean_word_length': 3.9138755980861246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7307378', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1482, 'n_words': 311}