[F] Fix response syntax limitation

pull/14/head
Azalea 2024-02-19 03:16:35 -05:00
parent e962baaf48
commit 77b2f90259
3 changed files with 26 additions and 23 deletions

View File

@ -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

View File

@ -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<String, Any> {
// 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 }

View File

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