Validate usernames on account creation like PSFPortal

This commit is contained in:
Chord 2020-05-09 20:13:26 +02:00
parent ceb58ed39a
commit 52de77a735

View file

@ -34,6 +34,8 @@ class LoginSessionActor extends Actor with MDCContextAware {
import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.ExecutionContext.Implicits.global
private case class UpdateServerList() private case class UpdateServerList()
val usernameRegex = """[A-Za-z0-9]{3,}""".r
var sessionId : Long = 0 var sessionId : Long = 0
var leftRef : ActorRef = ActorRef.noSender var leftRef : ActorRef = ActorRef.noSender
var rightRef : ActorRef = ActorRef.noSender var rightRef : ActorRef = ActorRef.noSender
@ -212,46 +214,55 @@ class LoginSessionActor extends Actor with MDCContextAware {
def createNewAccount : Receive = { def createNewAccount : Receive = {
case CreateNewAccount(connection, username, password, newToken) => case CreateNewAccount(connection, username, password, newToken) =>
log.info(s"Account $username does not exist, creating new account...") log.info(s"Account $username does not exist, creating new account...")
val bcryptPassword : String = password.bcrypt(numBcryptPasses)
connection.get.inTransaction { username match {
c => c.sendPreparedStatement( case usernameRegex(_*) => ;
"INSERT INTO accounts (username, passhash) VALUES(?,?) RETURNING id", val bcryptPassword : String = password.bcrypt(numBcryptPasses)
Array(username, bcryptPassword)
) connection.get.inTransaction {
}.onComplete { c => c.sendPreparedStatement(
case Success(insertResult) => "INSERT INTO accounts (username, passhash) VALUES(?,?) RETURNING id",
insertResult match { Array(username, bcryptPassword)
case result: QueryResult => )
if (result.rows.nonEmpty) { }.onComplete {
val accountId = result.rows.get.head(0).asInstanceOf[Int] case Success(insertResult) =>
accountIntermediary ! StoreAccountData(newToken, new Account(accountId, username)) insertResult match {
log.info(s"Successfully created new account for $username") case result: QueryResult =>
context.become(logTheLoginOccurrence) if (result.rows.nonEmpty) {
self ! LogTheLoginOccurrence(connection, username, newToken, true, accountId) val accountId = result.rows.get.head(0).asInstanceOf[Int]
} else { accountIntermediary ! StoreAccountData(newToken, new Account(accountId, username))
log.error(s"No result from account create insert for $username") log.info(s"Successfully created new account for $username")
context.become(finishAccountLogin) context.become(logTheLoginOccurrence)
self ! FinishAccountLogin(connection, username, newToken, false) self ! LogTheLoginOccurrence(connection, username, newToken, true, accountId)
} else {
log.error(s"No result from account create insert for $username")
context.become(finishAccountLogin)
self ! FinishAccountLogin(connection, username, newToken, false)
}
case default =>
log.error(s"Error creating new account for $username - $default")
context.become(finishAccountLogin)
self ! FinishAccountLogin(connection, username, newToken, false)
} }
case default => case Failure(e : com.github.mauricio.async.db.postgresql.exceptions.GenericDatabaseException) =>
log.error(s"Error creating new account for $username - $default") log.error(s"Error creating new account - ${e.errorMessage.message}")
context.become(finishAccountLogin) context.become(finishAccountLogin)
self ! FinishAccountLogin(connection, username, newToken, false) self ! FinishAccountLogin(connection, username, newToken, false)
case Failure(e : java.sql.SQLException) =>
log.error(s"Error creating new account - ${e.getMessage}")
context.become(finishAccountLogin)
self ! FinishAccountLogin(connection, username, newToken, false)
case _ =>
failWithError(s"Something to do?")
} }
case Failure(e : com.github.mauricio.async.db.postgresql.exceptions.GenericDatabaseException) =>
log.error(s"Error creating new account - ${e.errorMessage.message}")
context.become(finishAccountLogin)
self ! FinishAccountLogin(connection, username, newToken, false)
case Failure(e : java.sql.SQLException) =>
log.error(s"Error creating new account - ${e.getMessage}")
context.become(finishAccountLogin)
self ! FinishAccountLogin(connection, username, newToken, false)
case _ => case _ =>
failWithError(s"Something to do?") log.warn(s"Account '$username' does not match regex")
context.become(finishAccountLogin)
self ! FinishAccountLogin(connection, username, newToken, false)
} }
case default => case default =>
failWithError(s"Invalid message '$default' received in createNewAccount") failWithError(s"Invalid message '$default' received in createNewAccount")
} }