From 77b2f90259fd02270a8a1c266f1326d9e9dff6ff Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Mon, 19 Feb 2024 03:16:35 -0500 Subject: [PATCH] [F] Fix response syntax limitation --- src/main/java/ext/Ext.kt | 2 +- .../sega/game/maimai2/Maimai2New.kt | 6 +-- .../icu/samnyan/aqua/net/UserRegistrar.kt | 41 +++++++++++-------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/main/java/ext/Ext.kt b/src/main/java/ext/Ext.kt index b33ae136..b64440e8 100644 --- a/src/main/java/ext/Ext.kt +++ b/src/main/java/ext/Ext.kt @@ -21,7 +21,7 @@ typealias Bool = Boolean // Make it easier to throw a ResponseStatusException operator fun HttpStatus.invoke(message: String? = null): Nothing = throw ApiException(value(), message ?: this.reasonPhrase) -operator fun Int.compareTo(message: String): Int = throw ApiException(this, message) +operator fun Int.minus(message: String): Nothing = throw ApiException(this, message) // Email validation // https://www.baeldung.com/java-email-validation-regex diff --git a/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt b/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt index a3ff91db..1a3eb2df 100644 --- a/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt +++ b/src/main/java/icu/samnyan/aqua/api/controller/sega/game/maimai2/Maimai2New.kt @@ -1,14 +1,12 @@ package icu.samnyan.aqua.api.controller.sega.game.maimai2 import ext.RP -import ext.invoke +import ext.minus import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserDataRepository import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserGeneralDataRepository import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserPlaylogRepository -import org.springframework.http.HttpStatus.NOT_FOUND import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController import kotlin.jvm.optionals.getOrNull @@ -52,7 +50,7 @@ class Maimai2New( fun userSummary(@RP userId: Long): Map { // Summary values: total plays, player rating, server-wide ranking // number of each rank, max combo, number of full combo, number of all perfect - val user = userDataRepository.findByCard_ExtId(userId).getOrNull() ?: NOT_FOUND() + val user = userDataRepository.findByCard_ExtId(userId).getOrNull() ?: (404 - "User not found") val plays = userPlaylogRepository.findByUser_Card_ExtId(userId) val extra = userGeneralDataRepository.findByUser_Card_ExtId(userId) .associate { it.propertyKey to it.propertyValue } diff --git a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt index e9b4fcf6..c281303f 100644 --- a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt +++ b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt @@ -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" } } \ No newline at end of file