forked from Cookies_Github_mirror/AquaDX
[F] Fix response syntax limitation
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
package icu.samnyan.aqua.net
|
||||
|
||||
import ext.*
|
||||
import icu.samnyan.aqua.net.components.GeoIP
|
||||
import icu.samnyan.aqua.net.components.JWT
|
||||
import icu.samnyan.aqua.net.components.TurnstileService
|
||||
import icu.samnyan.aqua.net.db.AquaNetUser
|
||||
import icu.samnyan.aqua.net.db.AquaNetUserRepo
|
||||
import icu.samnyan.aqua.net.components.GeoIP
|
||||
import icu.samnyan.aqua.net.components.TurnstileService
|
||||
import jakarta.servlet.http.HttpServletRequest
|
||||
import org.springframework.security.crypto.password.PasswordEncoder
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
@@ -17,53 +18,57 @@ class UserRegistrar(
|
||||
val userRepo: AquaNetUserRepo,
|
||||
val hasher: PasswordEncoder,
|
||||
val turnstileService: TurnstileService,
|
||||
val geoIP: GeoIP
|
||||
val geoIP: GeoIP,
|
||||
val jwt: JWT
|
||||
) {
|
||||
/**
|
||||
* Register a new user
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
suspend fun register(@RP username: Str, @RP email: Str, @RP password: Str,
|
||||
@RP turnstile: Str, request: HttpServletRequest) {
|
||||
suspend fun register(
|
||||
@RP username: Str, @RP email: Str, @RP password: Str,
|
||||
@RP turnstile: Str, request: HttpServletRequest
|
||||
) {
|
||||
val ip = geoIP.getIP(request)
|
||||
|
||||
// Check captcha
|
||||
if (!turnstileService.validate(turnstile, ip)) 400 > "Invalid captcha"
|
||||
if (!turnstileService.validate(turnstile, ip)) 400 - "Invalid captcha"
|
||||
|
||||
// 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
|
||||
if (async { userRepo.findByEmailIgnoreCase(email) != null })
|
||||
400 > "User with email `$email` already exists"
|
||||
400 - "User with email `$email` already exists"
|
||||
|
||||
// Check if username is valid
|
||||
if (username.length < 2) 400 > "Username must be at least 2 letters"
|
||||
if (username.length > 32) 400 > "Username too long (max 32 letters)"
|
||||
if (username.contains(" ")) 400 > "Username cannot contain spaces"
|
||||
if (username.length < 2) 400 - "Username must be at least 2 letters"
|
||||
if (username.length > 32) 400 - "Username too long (max 32 letters)"
|
||||
if (username.contains(" ")) 400 - "Username cannot contain spaces"
|
||||
|
||||
// Check if username is within A-Za-z0-9_-~.
|
||||
username.find { !it.isLetterOrDigit() && it != '_' && it != '-' && it != '~' && it != '.' }?.let {
|
||||
400 > "Username cannot contain `$it`. Please only use letters (A-Z), numbers (0-9), and `_-~.` characters. " +
|
||||
"You can set a display name later."
|
||||
400 - "Username cannot contain `$it`. Please only use letters (A-Z), numbers (0-9), and `_-~.` characters. You can set a display name later."
|
||||
}
|
||||
|
||||
// Check if user with the same username exists
|
||||
if (async { userRepo.findByUsernameIgnoreCase(username) != null })
|
||||
400 > "User with username `$username` already exists"
|
||||
400 - "User with username `$username` already exists"
|
||||
|
||||
// Validate password
|
||||
if (password.length < 8) 400 > "Password must be at least 8 characters"
|
||||
if (password.length < 8) 400 - "Password must be at least 8 characters"
|
||||
|
||||
// GeoIP check to infer country
|
||||
val country = geoIP.getCountry(ip)
|
||||
|
||||
val u = AquaNetUser(username = username, email = email, pwHash = hasher.encode(password),
|
||||
regTime = millis(), lastLogin = millis(), country = country)
|
||||
val u = AquaNetUser(
|
||||
username = username, email = email, pwHash = hasher.encode(password),
|
||||
regTime = millis(), lastLogin = millis(), country = country
|
||||
)
|
||||
async { userRepo.save(u) }
|
||||
|
||||
// TODO: Send confirmation email
|
||||
|
||||
200 > "User created"
|
||||
200 - "User created"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user