<commit_before>package controllers

import models.join.{Activity, User}
import models.db
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits._
import org.json4s._
import org.json4s.native.{ JsonMethods => J }
import org.json4s.native.Serialization.write
import scala.concurrent.Future
import scalikejdbc._
import com.ponkotuy.data.{MyFleetAuth, Auth}
import tool.Authentication

/**
 *
 * @author ponkotuy
 * Date: 14/02/25
 */
object Common extends Controller {
  type Req = Map[String, Seq[String]]
  implicit val formats = DefaultFormats

  def authAndParse[T](f: (db.Admiral, T) => Result)(implicit mf: Manifest[T]): Action[Req] = {
    Action.async(parse.urlFormEncoded) { request =>
      authentication(request.body) { auth =>
        withData[T](request.body) { data =>
          f(auth, data)
        }
      }
    }
  }

  /** 実際はCheckしてないです */
  def checkPonkotuAndParse[T](f: (T) => Result)(implicit mf: Manifest[T]): Action[Req] = {
    Action.async(parse.urlFormEncoded(1024*1024*2)) { request =>
      checkPonkotu(request.body) {
        withData[T](request.body) { data =>
          f(data)
        }
      }
    }
  }

  /**
   * 1. 旧ログイン系は必ず必要（さもないとデータが不足する）
   * 2. 新ログイン系は任意だが、一度でも認証させたら通さないと駄目
   */
  def authentication(request: Req)(f: (db.Admiral) => Result): Future[Result] = {
    Future {
      reqHeadParse[Auth](request)("auth") match {
        case Some(oldAuth) =>
          Authentication.oldAuth(oldAuth) match {
            case Some(ad) =>
              reqHeadParse[MyFleetAuth](request)("auth2") match {
                case Some(auth) =>
                  if(auth.id == oldAuth.memberId && Authentication.myfleetAuthOrCreate(auth)) f(ad)
                  else Unauthorized("Failed Pasword Authentication")
                case None =>
                  if(db.MyFleetAuth.find(oldAuth.memberId).isEmpty) f(ad)
                  else Unauthorized("Require Password")
              }
            case None => Unauthorized("Failed Old Authentication")
          }
        case None => Unauthorized("Require Auth Data")
      }
    }
  }

  /** Checkしなくなりました */
  def checkPonkotu(request: Req)(f: => Result): Future[Result] = {
    Future {
      f
      Ok("Success")
    }
  }<commit_msg>Fix authentication error messages<commit_after>package controllers

import models.join.{Activity, User}
import models.db
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits._
import org.json4s._
import org.json4s.native.{ JsonMethods => J }
import org.json4s.native.Serialization.write
import scala.concurrent.Future
import scalikejdbc._
import com.ponkotuy.data.{MyFleetAuth, Auth}
import tool.Authentication

/**
 *
 * @author ponkotuy
 * Date: 14/02/25
 */
object Common extends Controller {
  type Req = Map[String, Seq[String]]
  implicit val formats = DefaultFormats

  def authAndParse[T](f: (db.Admiral, T) => Result)(implicit mf: Manifest[T]): Action[Req] = {
    Action.async(parse.urlFormEncoded) { request =>
      authentication(request.body) { auth =>
        withData[T](request.body) { data =>
          f(auth, data)
        }
      }
    }
  }

  /** 実際はCheckしてないです */
  def checkPonkotuAndParse[T](f: (T) => Result)(implicit mf: Manifest[T]): Action[Req] = {
    Action.async(parse.urlFormEncoded(1024*1024*2)) { request =>
      checkPonkotu(request.body) {
        withData[T](request.body) { data =>
          f(data)
        }
      }
    }
  }

  /**
   * 1. 旧ログイン系は必ず必要（さもないとデータが不足する）
   * 2. 新ログイン系は任意だが、一度でも認証させたら通さないと駄目
   */
  def authentication(request: Req)(f: (db.Admiral) => Result): Future[Result] = {
    Future {
      reqHeadParse[Auth](request)("auth") match {
        case Some(oldAuth) =>
          Authentication.oldAuth(oldAuth) match {
            case Some(ad) =>
              reqHeadParse[MyFleetAuth](request)("auth2") match {
                case Some(auth) =>
                  if(auth.id == oldAuth.memberId && Authentication.myfleetAuthOrCreate(auth)) f(ad)
                  else Unauthorized("Password Authentication Failed")
                case None =>
                  if(db.MyFleetAuth.find(oldAuth.memberId).isEmpty) f(ad)
                  else Unauthorized("Password Required")
              }
            case None => Unauthorized("Old Protocol Authentication Failed")
          }
        case None => Unauthorized("Authentication Data Required")
      }
    }
  }

  /** Checkしなくなりました */
  def checkPonkotu(request: Req)(f: => Result): Future[Result] = {
    Future {
      f
      Ok("Success")
    }
  }
{'fraction_non_alphanumeric': 0.13265755839576907, 'fraction_numerical': 0.009255178492728075, 'mean_word_length': 2.610978520286396, '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': '1669438', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1510, 'n_words': 266}