diff --git a/src/main/scala/lc/core/captchaProviders.scala b/src/main/scala/lc/core/captchaProviders.scala index f8588f0..5af97a0 100644 --- a/src/main/scala/lc/core/captchaProviders.scala +++ b/src/main/scala/lc/core/captchaProviders.scala @@ -24,7 +24,7 @@ object CaptchaProviders { private val seed = Config.seed private val random = new scala.util.Random(seed) - private val config = Config.captchaConfigMap + private val config = Config.captchaConfig private def getNextRandomInt(max: Int): Int = random.synchronized { @@ -35,21 +35,31 @@ object CaptchaProviders { return providers(id) } - private def filterProviderByParam(param: Parameters): Iterable[(List[String], List[String])] = { - for { - configValue <- config.values - if configValue("supportedLevels").contains(param.level) - if configValue("supportedMedia").contains(param.media) - if configValue("supportedinputType").contains(param.input_type) - } yield (configValue("name"), configValue("config")) + private def filterProviderByParam(param: Parameters): Iterable[(String, String)] = { + val configFilter = for { + configValue <- config + if configValue.allowedLevels.contains(param.level) + if configValue.allowedMedia.contains(param.media) + if configValue.allowedInputType.contains(param.input_type) + } yield (configValue.name, configValue.config) + + val providerFilter = for { + providerValue <- configFilter + providerConfigMap = providers(providerValue._1).supportedParameters() + if providerConfigMap.get(ParametersEnum.SUPPORTEDLEVEL.toString).contains(param.level) + if providerConfigMap.get(ParametersEnum.SUPPORTEDMEDIA.toString).contains(param.media) + if providerConfigMap.get(ParametersEnum.SUPPORTEDINPUTTYPE.toString).contains(param.input_type) + } yield (providerValue._1, providerValue._2) + + providerFilter } def getProvider(param: Parameters): ChallengeProvider = { val providerConfig = filterProviderByParam(param).toList val randomIndex = getNextRandomInt(providerConfig.length) val providerIndex = providerConfig(randomIndex)._1 - val selectedProvider = providers(providerIndex(0)) - selectedProvider.configure(providerConfig(randomIndex)._2(0)) + val selectedProvider = providers(providerIndex) + selectedProvider.configure(providerConfig(randomIndex)._2) selectedProvider } }