From 4a5bd3135fdf90acc7e8b9f87843218d2d62a1ec Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sun, 3 Mar 2024 00:47:57 -0500 Subject: [PATCH] [O] More logging --- src/main/java/ext/Ext.kt | 5 ++++- src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt | 11 +++++++++-- .../java/icu/samnyan/aqua/net/utils/ErrorResponse.kt | 7 ++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/ext/Ext.kt b/src/main/java/ext/Ext.kt index 26a3a9b7..405e0a4a 100644 --- a/src/main/java/ext/Ext.kt +++ b/src/main/java/ext/Ext.kt @@ -30,7 +30,10 @@ annotation class Doc( // Make it easier to throw a ResponseStatusException operator fun HttpStatus.invoke(message: String? = null): Nothing = throw ApiException(value(), message ?: this.reasonPhrase) -operator fun Int.minus(message: String): Nothing = throw ApiException(this, message) +operator fun Int.minus(message: String): Nothing { + ApiException.log.info("> Error $this: $message") + throw ApiException(this, message) +} // Email validation // https://www.baeldung.com/java-email-validation-regex diff --git a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt index 73a436e8..309a6bc2 100644 --- a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt +++ b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt @@ -9,6 +9,7 @@ import icu.samnyan.aqua.sega.general.dao.CardRepository import icu.samnyan.aqua.sega.general.model.Card import icu.samnyan.aqua.sega.general.service.CardService import jakarta.servlet.http.HttpServletRequest +import org.slf4j.LoggerFactory import org.springframework.security.crypto.password.PasswordEncoder import org.springframework.web.bind.annotation.RestController import java.time.Instant @@ -35,6 +36,8 @@ class UserRegistrar( // This is because games can only take uint32 for card ID, which is at max 10 digits (4294967295) const val cardExtIdStart = 1e9.toLong() const val cardExtIdEnd = 4294967295 + + val log = LoggerFactory.getLogger(UserRegistrar::class.java) } @API("/register") @@ -43,8 +46,8 @@ class UserRegistrar( @RP username: Str, @RP email: Str, @RP password: Str, @RP turnstile: Str, request: HttpServletRequest ): Any { - val ip = geoIP.getIP(request) + log.info("Net: /user/register from $ip : $username") // Check captcha if (!turnstileService.validate(turnstile, ip)) 400 - "Invalid captcha" @@ -89,9 +92,9 @@ class UserRegistrar( @RP email: Str, @RP password: Str, @RP turnstile: Str, request: HttpServletRequest ): Any { - // Check captcha val ip = geoIP.getIP(request) + log.info("Net: /user/login from $ip : $email") if (!turnstileService.validate(turnstile, ip)) 400 - "Invalid captcha" // Treat email as email / username @@ -124,6 +127,7 @@ class UserRegistrar( // Set last login time async { userRepo.save(user.apply { lastLogin = millis() }) } + log.info("> Login success: ${user.username} ${user.auId}") return mapOf("token" to token) } @@ -131,6 +135,8 @@ class UserRegistrar( @API("/confirm-email") @Doc("Confirm email address with a token sent through email to the user.", "Success message") suspend fun confirmEmail(@RP token: Str): Any { + log.info("Net: /user/confirm-email with token $token") + // Find the confirmation val confirmation = async { confirmationRepo.findByToken(token) } @@ -173,6 +179,7 @@ class UserRegistrar( @Doc("Get a Keychip ID so that the user can connect to the server.", "Success message") suspend fun setupConnection(@RP token: Str) = jwt.auth(token) { u -> if (u.keychip != null) return mapOf("keychip" to u.keychip) + log.info("Net: /user/keychip setup: ${u.auId} for ${u.username}") // Generate a keychip id with 10 digits (e.g. A1234567890) var new = "A" + keychipRange.random() diff --git a/src/main/java/icu/samnyan/aqua/net/utils/ErrorResponse.kt b/src/main/java/icu/samnyan/aqua/net/utils/ErrorResponse.kt index 269ee309..5232557f 100644 --- a/src/main/java/icu/samnyan/aqua/net/utils/ErrorResponse.kt +++ b/src/main/java/icu/samnyan/aqua/net/utils/ErrorResponse.kt @@ -1,6 +1,7 @@ package icu.samnyan.aqua.net.utils import ext.Str +import org.slf4j.LoggerFactory import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.ControllerAdvice import org.springframework.web.bind.annotation.ExceptionHandler @@ -8,7 +9,11 @@ import org.springframework.web.bind.annotation.ExceptionHandler val SUCCESS = ResponseEntity.ok().body(mapOf("status" to "ok")) -class ApiException(val code: Int, message: Str) : RuntimeException(message) +class ApiException(val code: Int, message: Str) : RuntimeException(message) { + companion object { + val log = LoggerFactory.getLogger(ApiException::class.java) + } +} @ControllerAdvice class GlobalExceptionHandler {