<commit_before>package com.identityblitz.login

import com.identityblitz.login.service.ServiceProvider
import com.identityblitz.login.LoggingUtils._
import com.identityblitz.login.authn.method.AuthnMethod
import com.identityblitz.login.authn.provider.{WithAttributes, WithBind, Provider}
import com.identityblitz.login.util.Reflection
import scala.language.implicitConversions

/**
 */
object Conf {
  import ServiceProvider.confService

  val providers = confService.getPropsDeepGrouped("providers").map{
    case (name, options) =>
      val meta = new ProviderMeta(name, options)
      name -> (meta.newInstance -> meta)
  }

  def resolveProvider(name: String) = providers.get(name).getOrElse({
    val err = s"Provider with name $name is not found"
    logger.error(err)
    throw new IllegalArgumentException(err)
  })._1

  val methods = confService.getPropsDeepGrouped("authnMethods")
    .map({case (name, options) => AuthnMethodMeta(name, options, resolveProvider)})
    .foldLeft[collection.mutable.Map[String, (AuthnMethod, AuthnMethodMeta)]](collection.mutable.Map())(
      (acm, meta) => {
        val value = meta.newInstance -> meta
        if (meta.isDefault) {
          acm += ("default" -> value)
        }
        acm += (meta.name -> value)
      }
    ).toMap

  val loginFlow = confService.getOptString("loginFlow").fold[LoginFlow]({
    logger.debug("Will use the built-in login flow: {}", BuiltInLoginFlow.getClass.getSimpleName)
    BuiltInLoginFlow
  })(className => {
    logger.debug("Find in the configuration a custom login flow [class = {}]", className)
    Reflection.getConstructor(className).apply().asInstanceOf[LoginFlow]
  })

  def resolveMethod(name: String) = methods.get(name).getOrElse({
    val err = s"Authentication method with name $name is not found"
    logger.error(err)
    throw new IllegalArgumentException(err)
  })._1
}

case class ProviderMeta(name: String, options: Map[String, String]) extends Instantiable[Provider] {

  private object Options extends Enumeration {
    import scala.language.implicitConversions

    type Options = Value
    val `class` = Value

    implicit def valueToString(v: Value): String = v.toString

    val builtInOptions = values.map(_.toString)
  }

  import Options._

  override val initArgs = Array[Any](name, options.filter(entry => !builtInOptions.contains(entry._1)))

  override def toString: String = {
    val sb =new StringBuilder("BindProviderMeta(")
    sb.append("name -> ").append(name).append(",")
      .append("options -> ").append(options)
      .append(")").toString()
  }
}

case class AuthnMethodMeta(name: String, options: Map[String, String],
                           private val resolveProvider: (String) => Provider) extends Instantiable[AuthnMethod] {

  private object Options extends Enumeration {
    import scala.language.implicitConversions

    type Options = Value
    val `class`, default, `bind-providers`, `attributes-providers` = Value

    implicit def valueToString(v: Value): String = v.toString

    val builtInOptions = values.map(_.toString)
  }

  import Options._
  
  val isDefault = options.get(default).fold(false)(_.toBoolean)
  val bindProviders = options.get(`bind-providers`).map(_.split(",")).getOrElse[Array[String]]({<commit_msg>move to command pattern (partial commit)<commit_after>package com.identityblitz.login

import com.identityblitz.login.service.ServiceProvider
import com.identityblitz.login.LoggingUtils._
import com.identityblitz.login.authn.method.AuthnMethod
import com.identityblitz.login.authn.provider.{WithAttributes, WithBind, Provider}
import com.identityblitz.login.util.Reflection
import scala.language.implicitConversions

/**
 */
object Conf {
  import ServiceProvider.confService

  val providers = confService.getPropsDeepGrouped("providers").map{
    case (name, options) =>
      val meta = new ProviderMeta(name, options)
      name -> (meta.newInstance -> meta)
  }

  def resolveProvider(name: String) = providers.get(name).getOrElse({
    val err = s"Provider with name $name is not found"
    logger.error(err)
    throw new IllegalArgumentException(err)
  })._1

  val methods = confService.getPropsDeepGrouped("authnMethods")
    .map({case (name, options) => AuthnMethodMeta(name, options, resolveProvider)})
    .foldLeft[collection.mutable.Map[String, (AuthnMethod, AuthnMethodMeta)]](collection.mutable.Map())(
      (acm, meta) => {
        val value = meta.newInstance -> meta
        if (meta.isDefault) {
          acm += ("default" -> value)
        }
        acm += (meta.name -> value)
      }
    ).toMap

  val loginFlow = confService.getOptString("loginFlow").fold[LoginFlow]({
    logger.debug("Will use the built-in login flow: {}", BuiltInLoginFlow.getClass.getSimpleName)
    BuiltInLoginFlow
  })(className => {
    logger.debug("Find in the configuration a custom login flow [class = {}]", className)
    Reflection.getConstructor(className).apply().asInstanceOf[LoginFlow]
  })

  def resolveMethod(name: String) = methods.get(name).getOrElse({
    val err = s"Authentication method with name $name is not found"
    logger.error(err)
    throw new IllegalArgumentException(err)
  })._1
}

case class ProviderMeta(name: String, options: Map[String, String]) extends Instantiable[Provider] {

  private object Options extends Enumeration {
    import scala.language.implicitConversions

    type Options = Value
    val `class` = Value

    implicit def valueToString(v: Value): String = v.toString

    val builtInOptions = values.map(_.toString)
  }

  import Options._

  override val initArgs = Array[Any](name, options.filter(entry => !builtInOptions.contains(entry._1)))

  override def toString: String = {
    val sb =new StringBuilder("BindProviderMeta(")
    sb.append("name -> ").append(name).append(",")
      .append("options -> ").append(options)
      .append(")").toString()
  }
}

case class AuthnMethodMeta(name: String, options: Map[String, String],
                           private val resolveProvider: (String) => Provider) extends Instantiable[AuthnMethod] {

  private object Options extends Enumeration {
    import scala.language.implicitConversions

    type Options = Value
    val `class`, default, `bind-providers`, `attributes-providers` = Value

    implicit def valueToString(v: Value): String = v.toString

    val builtInOptions = values.map(_.toString)
  }

  import Options._
  
  val isDefault = options.get(default).fold(false)(_.toBoolean)
  /*todo: restore*/
  val bindProviders = options.get("bind-providers").map(_.split(",")).getOrElse[Array[String]]({
{'fraction_non_alphanumeric': 0.12350356114562813, 'fraction_numerical': 0.0009092286710107592, 'mean_word_length': 3.984894259818731, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '4434739', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1883, 'n_words': 446}