[+] Validate captcha

pull/14/head
Azalea 2024-02-17 00:31:40 -05:00
parent 6200c56144
commit e0dc3bd1f4
2 changed files with 15 additions and 12 deletions

View File

@ -1,14 +1,16 @@
package ext package ext
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestHeader import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.server.ResponseStatusException import org.springframework.web.server.ResponseStatusException
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
typealias RP = RequestParam typealias RP = RequestParam
typealias RB = RequestBody typealias RB = RequestBody
@ -32,3 +34,5 @@ val HTTP = HttpClient(CIO) {
json() json()
} }
} }
suspend fun <T> async(block: suspend kotlinx.coroutines.CoroutineScope.() -> T): T = withContext(Dispatchers.IO) { block() }

View File

@ -4,8 +4,9 @@ import ext.*
import icu.samnyan.aqua.net.db.AquaNetUser import icu.samnyan.aqua.net.db.AquaNetUser
import icu.samnyan.aqua.net.db.AquaNetUserRepo import icu.samnyan.aqua.net.db.AquaNetUserRepo
import icu.samnyan.aqua.net.utils.TurnstileService import icu.samnyan.aqua.net.utils.TurnstileService
import io.ktor.client.request.*
import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.springframework.security.crypto.password.PasswordEncoder import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
@ -22,23 +23,21 @@ class UserRegistrar(
* Register a new user * Register a new user
*/ */
@PostMapping("/register") @PostMapping("/register")
fun register(@RP email: Str, @RP pass: Str, @RP captcha: Str?, request: HttpServletRequest) { suspend fun register(@RP email: Str, @RP pass: Str, @RP captcha: Str?, request: HttpServletRequest) {
request.ip
// Check captcha // Check captcha
if (!turnstileService.validate(captcha, if (!turnstileService.validate(captcha, request)) 400 > "Invalid captcha"
// Check if email is valid // Check if email is valid
if (!email.isValidEmail()) 400 > "Invalid email" if (!email.isValidEmail()) 400 > "Invalid email"
// Check if user with the same email exists // Check if user with the same email exists
if (userRepo.existsByEmail(email)) 400 > "User already exists" if (async { userRepo.existsByEmail(email) }) 400 > "User already exists"
// Validate password // Validate password
if (pass.length < 8) 400 > "Password too short" if (pass.length < 8) 400 > "Password too short"
val u = AquaNetUser(email = email, pwHash = hasher.encode(pass), regTime = millis(), lastLogin = millis()) val u = AquaNetUser(email = email, pwHash = hasher.encode(pass), regTime = millis(), lastLogin = millis())
userRepo.save(u) async { userRepo.save(u) }
200 > "User created" 200 > "User created"
} }