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]
}
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)

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