From 739854935db45069c0f0deb17fea8312a6072d5d Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Mon, 19 Feb 2024 21:05:17 -0500 Subject: [PATCH] [+] Create ghost card on registration --- .../icu/samnyan/aqua/net/UserRegistrar.kt | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt index 5d8986d1..83667a93 100644 --- a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt +++ b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt @@ -7,11 +7,17 @@ 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.db.EmailConfirmationRepo +import icu.samnyan.aqua.sega.general.dao.CardRepository +import icu.samnyan.aqua.sega.general.model.Card import jakarta.servlet.http.HttpServletRequest import org.springframework.security.crypto.password.PasswordEncoder import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController +import java.time.Instant +import java.time.LocalDateTime +import java.util.Random @RestController @RequestMapping("/api/v2/user") @@ -21,8 +27,16 @@ class UserRegistrar( val turnstileService: TurnstileService, val emailService: EmailService, val geoIP: GeoIP, - val jwt: JWT + val jwt: JWT, + val confirmationRepo: EmailConfirmationRepo, + val cardRepo: CardRepository ) { + companion object { + // Random long with length 19 (10^19 possibilities) + const val cardExtIdStart = 10e18.toLong() + const val cardExtIdEnd = 10e19.toLong() + } + /** * Register a new user */ @@ -64,11 +78,25 @@ class UserRegistrar( // GeoIP check to infer country val country = geoIP.getCountry(ip) + // Create a ghost card + val card = Card().apply { + extId = Random().nextLong(cardExtIdStart, cardExtIdEnd) + luid = extId.toString() + registerTime = LocalDateTime.now() + accessTime = registerTime + } val u = AquaNetUser( username = username, email = email, pwHash = hasher.encode(password), - regTime = millis(), lastLogin = millis(), country = country + regTime = millis(), lastLogin = millis(), country = country, + ghostCard = card ) - async { userRepo.save(u) } + card.aquaUser = u + + // Save the user + async { + userRepo.save(u) + cardRepo.save(card) + } // Send confirmation email emailService.sendConfirmation(u)