mirror of https://github.com/hykilpikonna/AquaDX
[F] Fix response syntax limitation
parent
e962baaf48
commit
77b2f90259
|
@ -21,7 +21,7 @@ typealias Bool = Boolean
|
||||||
|
|
||||||
// Make it easier to throw a ResponseStatusException
|
// Make it easier to throw a ResponseStatusException
|
||||||
operator fun HttpStatus.invoke(message: String? = null): Nothing = throw ApiException(value(), message ?: this.reasonPhrase)
|
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
|
// Email validation
|
||||||
// https://www.baeldung.com/java-email-validation-regex
|
// https://www.baeldung.com/java-email-validation-regex
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
package icu.samnyan.aqua.api.controller.sega.game.maimai2
|
package icu.samnyan.aqua.api.controller.sega.game.maimai2
|
||||||
|
|
||||||
import ext.RP
|
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.UserDataRepository
|
||||||
import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserGeneralDataRepository
|
import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserGeneralDataRepository
|
||||||
import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserPlaylogRepository
|
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.GetMapping
|
||||||
import org.springframework.web.bind.annotation.RequestMapping
|
import org.springframework.web.bind.annotation.RequestMapping
|
||||||
import org.springframework.web.bind.annotation.RequestParam
|
|
||||||
import org.springframework.web.bind.annotation.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
import kotlin.jvm.optionals.getOrNull
|
import kotlin.jvm.optionals.getOrNull
|
||||||
|
|
||||||
|
@ -52,7 +50,7 @@ class Maimai2New(
|
||||||
fun userSummary(@RP userId: Long): Map<String, Any> {
|
fun userSummary(@RP userId: Long): Map<String, Any> {
|
||||||
// Summary values: total plays, player rating, server-wide ranking
|
// Summary values: total plays, player rating, server-wide ranking
|
||||||
// number of each rank, max combo, number of full combo, number of all perfect
|
// 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 plays = userPlaylogRepository.findByUser_Card_ExtId(userId)
|
||||||
val extra = userGeneralDataRepository.findByUser_Card_ExtId(userId)
|
val extra = userGeneralDataRepository.findByUser_Card_ExtId(userId)
|
||||||
.associate { it.propertyKey to it.propertyValue }
|
.associate { it.propertyKey to it.propertyValue }
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
package icu.samnyan.aqua.net
|
package icu.samnyan.aqua.net
|
||||||
|
|
||||||
import ext.*
|
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.AquaNetUser
|
||||||
import icu.samnyan.aqua.net.db.AquaNetUserRepo
|
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 jakarta.servlet.http.HttpServletRequest
|
||||||
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
|
||||||
|
@ -17,53 +18,57 @@ class UserRegistrar(
|
||||||
val userRepo: AquaNetUserRepo,
|
val userRepo: AquaNetUserRepo,
|
||||||
val hasher: PasswordEncoder,
|
val hasher: PasswordEncoder,
|
||||||
val turnstileService: TurnstileService,
|
val turnstileService: TurnstileService,
|
||||||
val geoIP: GeoIP
|
val geoIP: GeoIP,
|
||||||
|
val jwt: JWT
|
||||||
) {
|
) {
|
||||||
/**
|
/**
|
||||||
* Register a new user
|
* Register a new user
|
||||||
*/
|
*/
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
suspend fun register(@RP username: Str, @RP email: Str, @RP password: Str,
|
suspend fun register(
|
||||||
@RP turnstile: Str, request: HttpServletRequest) {
|
@RP username: Str, @RP email: Str, @RP password: Str,
|
||||||
|
@RP turnstile: Str, request: HttpServletRequest
|
||||||
|
) {
|
||||||
val ip = geoIP.getIP(request)
|
val ip = geoIP.getIP(request)
|
||||||
|
|
||||||
// Check captcha
|
// Check captcha
|
||||||
if (!turnstileService.validate(turnstile, ip)) 400 > "Invalid captcha"
|
if (!turnstileService.validate(turnstile, ip)) 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 (async { userRepo.findByEmailIgnoreCase(email) != null })
|
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
|
// Check if username is valid
|
||||||
if (username.length < 2) 400 > "Username must be at least 2 letters"
|
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.length > 32) 400 - "Username too long (max 32 letters)"
|
||||||
if (username.contains(" ")) 400 > "Username cannot contain spaces"
|
if (username.contains(" ")) 400 - "Username cannot contain spaces"
|
||||||
|
|
||||||
// Check if username is within A-Za-z0-9_-~.
|
// Check if username is within A-Za-z0-9_-~.
|
||||||
username.find { !it.isLetterOrDigit() && it != '_' && it != '-' && it != '~' && it != '.' }?.let {
|
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. " +
|
400 - "Username cannot contain `$it`. Please only use letters (A-Z), numbers (0-9), and `_-~.` characters. You can set a display name later."
|
||||||
"You can set a display name later."
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if user with the same username exists
|
// Check if user with the same username exists
|
||||||
if (async { userRepo.findByUsernameIgnoreCase(username) != null })
|
if (async { userRepo.findByUsernameIgnoreCase(username) != null })
|
||||||
400 > "User with username `$username` already exists"
|
400 - "User with username `$username` already exists"
|
||||||
|
|
||||||
// Validate password
|
// 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
|
// GeoIP check to infer country
|
||||||
val country = geoIP.getCountry(ip)
|
val country = geoIP.getCountry(ip)
|
||||||
|
|
||||||
val u = AquaNetUser(username = username, email = email, pwHash = hasher.encode(password),
|
val u = AquaNetUser(
|
||||||
regTime = millis(), lastLogin = millis(), country = country)
|
username = username, email = email, pwHash = hasher.encode(password),
|
||||||
|
regTime = millis(), lastLogin = millis(), country = country
|
||||||
|
)
|
||||||
async { userRepo.save(u) }
|
async { userRepo.save(u) }
|
||||||
|
|
||||||
// TODO: Send confirmation email
|
// TODO: Send confirmation email
|
||||||
|
|
||||||
200 > "User created"
|
200 - "User created"
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue