From 033db81721f13cbe5dfbf3cb7d331ec1f25a4d44 Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Thu, 11 Mar 2021 21:26:58 +0530 Subject: [PATCH] Add captcha fields enum Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/core/captcha.scala | 33 +++++++++++----------- src/main/scala/lc/core/captchaFields.scala | 29 +++++++++++++++++++ 2 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 src/main/scala/lc/core/captchaFields.scala diff --git a/src/main/scala/lc/core/captcha.scala b/src/main/scala/lc/core/captcha.scala index d2fbe29..c8c2edc 100644 --- a/src/main/scala/lc/core/captcha.scala +++ b/src/main/scala/lc/core/captcha.scala @@ -53,25 +53,25 @@ class Captcha { token.asInstanceOf[Int] } - val supportedinputType = Config.supportedinputType - val supportedLevels = Config.supportedLevels - val supportedMedia = Config.supportedMedia + val allowedInputType = Config.allowedInputType + val allowedLevels = Config.allowedLevels + val allowedMedia = Config.allowedMedia private def validateParam(param: Parameters): Boolean = { if ( - supportedLevels.contains(param.level) && - supportedMedia.contains(param.media) && - supportedinputType.contains(param.input_type) + allowedLevels.contains(param.level) && + allowedMedia.contains(param.media) && + allowedInputType.contains(param.input_type) ) return true else return false } - def getChallenge(param: Parameters): Id = { + def getChallenge(param: Parameters): ChallengeResult = { try { val validParam = validateParam(param) - val result = if (validParam) { + if (validParam) { val tokenPstmt = Statements.tlStmts.get.tokenPstmt tokenPstmt.setString(1, param.level) tokenPstmt.setString(2, param.media) @@ -84,23 +84,22 @@ class Captcha { } val updateAttemptedPstmt = Statements.tlStmts.get.updateAttemptedPstmt val token = tokenOpt.getOrElse(generateChallenge(param)) - val uuidResult = if (token != -1) { + val result = if (token != -1) { val uuid = getUUID(token) updateAttemptedPstmt.setString(1, uuid) updateAttemptedPstmt.executeUpdate() - uuid + Id(uuid) } else { - "No Captcha for the provided parameters" + Error(ErrorMessageEnum.NO_CAPTCHA.toString) } - uuidResult + result } else { - "Invalid Parameters" + Error(ErrorMessageEnum.INVALID_PARAM.toString) } - Id(result) } catch { case e: Exception => println(e) - Id("Something went wrong") + Error(ErrorMessageEnum.SMW.toString) } } @@ -122,10 +121,10 @@ class Captcha { val secret = rs.getString("secret") val provider = rs.getString("provider") val check = CaptchaProviders.getProviderById(provider).checkAnswer(secret, answer.answer) - val result = if (check) "TRUE" else "FALSE" + val result = if (check) ResultEnum.TRUE.toString else ResultEnum.FALSE.toString result } else { - "EXPIRED" + ResultEnum.EXPIRED.toString } val deleteAnswerPstmt = Statements.tlStmts.get.deleteAnswerPstmt deleteAnswerPstmt.setString(1, answer.id) diff --git a/src/main/scala/lc/core/captchaFields.scala b/src/main/scala/lc/core/captchaFields.scala new file mode 100644 index 0000000..5418ee9 --- /dev/null +++ b/src/main/scala/lc/core/captchaFields.scala @@ -0,0 +1,29 @@ +package lc.core + +object ParametersEnum extends Enumeration { + type Parameter = Value + + val SUPPORTEDLEVEL: Value = Value("supportedLevels") + val SUPPORTEDMEDIA: Value = Value("supportedMedia") + val SUPPORTEDINPUTTYPE: Value = Value("supportedInputType") + + val ALLOWEDLEVELS: Value = Value("allowedLevels") + val ALLOWEDMEDIA: Value = Value("allowedMedia") + val ALLOWEDINPUTTYPE: Value = Value("allowedInputType") +} + +object ResultEnum extends Enumeration { + type Result = Value + + val TRUE: Value = Value("True") + val FALSE: Value = Value("False") + val EXPIRED: Value = Value("Expired") +} + +object ErrorMessageEnum extends Enumeration { + type ErrorMessage = Value + + val SMW: Value = Value("Oops, something went worng!") + val INVALID_PARAM: Value = Value("Invalid Pramaters") + val NO_CAPTCHA: Value = Value("No captcha for the provided parameters") +}