Add captcha fields enum

Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
Rahul Rudragoudar 2021-03-11 21:26:58 +05:30
parent 21b299a30f
commit 033db81721
No known key found for this signature in database
GPG Key ID: 0D2CE231A7287EBC
2 changed files with 45 additions and 17 deletions

View File

@ -53,25 +53,25 @@ class Captcha {
token.asInstanceOf[Int] token.asInstanceOf[Int]
} }
val supportedinputType = Config.supportedinputType val allowedInputType = Config.allowedInputType
val supportedLevels = Config.supportedLevels val allowedLevels = Config.allowedLevels
val supportedMedia = Config.supportedMedia val allowedMedia = Config.allowedMedia
private def validateParam(param: Parameters): Boolean = { private def validateParam(param: Parameters): Boolean = {
if ( if (
supportedLevels.contains(param.level) && allowedLevels.contains(param.level) &&
supportedMedia.contains(param.media) && allowedMedia.contains(param.media) &&
supportedinputType.contains(param.input_type) allowedInputType.contains(param.input_type)
) )
return true return true
else else
return false return false
} }
def getChallenge(param: Parameters): Id = { def getChallenge(param: Parameters): ChallengeResult = {
try { try {
val validParam = validateParam(param) val validParam = validateParam(param)
val result = if (validParam) { if (validParam) {
val tokenPstmt = Statements.tlStmts.get.tokenPstmt val tokenPstmt = Statements.tlStmts.get.tokenPstmt
tokenPstmt.setString(1, param.level) tokenPstmt.setString(1, param.level)
tokenPstmt.setString(2, param.media) tokenPstmt.setString(2, param.media)
@ -84,23 +84,22 @@ class Captcha {
} }
val updateAttemptedPstmt = Statements.tlStmts.get.updateAttemptedPstmt val updateAttemptedPstmt = Statements.tlStmts.get.updateAttemptedPstmt
val token = tokenOpt.getOrElse(generateChallenge(param)) val token = tokenOpt.getOrElse(generateChallenge(param))
val uuidResult = if (token != -1) { val result = if (token != -1) {
val uuid = getUUID(token) val uuid = getUUID(token)
updateAttemptedPstmt.setString(1, uuid) updateAttemptedPstmt.setString(1, uuid)
updateAttemptedPstmt.executeUpdate() updateAttemptedPstmt.executeUpdate()
uuid Id(uuid)
} else { } else {
"No Captcha for the provided parameters" Error(ErrorMessageEnum.NO_CAPTCHA.toString)
} }
uuidResult result
} else { } else {
"Invalid Parameters" Error(ErrorMessageEnum.INVALID_PARAM.toString)
} }
Id(result)
} catch { } catch {
case e: Exception => case e: Exception =>
println(e) println(e)
Id("Something went wrong") Error(ErrorMessageEnum.SMW.toString)
} }
} }
@ -122,10 +121,10 @@ class Captcha {
val secret = rs.getString("secret") val secret = rs.getString("secret")
val provider = rs.getString("provider") val provider = rs.getString("provider")
val check = CaptchaProviders.getProviderById(provider).checkAnswer(secret, answer.answer) 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 result
} else { } else {
"EXPIRED" ResultEnum.EXPIRED.toString
} }
val deleteAnswerPstmt = Statements.tlStmts.get.deleteAnswerPstmt val deleteAnswerPstmt = Statements.tlStmts.get.deleteAnswerPstmt
deleteAnswerPstmt.setString(1, answer.id) deleteAnswerPstmt.setString(1, answer.id)

View File

@ -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")
}