[O] Disallow using card/summary to query others' card

pull/81/head
Clansty 2024-11-04 19:00:44 +08:00
parent fb96e93184
commit b28a1986c9
No known key found for this signature in database
GPG Key ID: 3A6BE8BAF2EDE134
1 changed files with 30 additions and 26 deletions

View File

@ -37,11 +37,13 @@ class CardController(
@API("/summary") @API("/summary")
@Doc("Get a summary of the card, including the user's name, rating, and last login date.", "Summary of the card") @Doc("Get a summary of the card, including the user's name, rating, and last login date.", "Summary of the card")
suspend fun summary(@RP cardId: Str): Any suspend fun summary(@RP cardId: Str, @RP token: Str): Any {
{ val user = jwt.auth(token)
// DO NOT CHANGE THIS ERROR MESSAGE - The frontend uses it to detect if the card is not found // DO NOT CHANGE THIS ERROR MESSAGE - The frontend uses it to detect if the card is not found
val card = cardService.tryLookup(cardId) ?: (404 - "Card not found") val card = cardService.tryLookup(cardId) ?: (404 - "Card not found")
if (card.aquaUser != null && card.aquaUser?.auId != user.auId) (404 - "Card not found")
// Lookup data for each game // Lookup data for each game
return mapOf( return mapOf(
"card" to card, "card" to card,
@ -133,20 +135,21 @@ class CardController(
* *
* Assumption: The card is already linked to the user. * Assumption: The card is already linked to the user.
*/ */
suspend fun <T : IUserData> migrateCard(repo: GenericUserDataRepo<T>, cardRepo: CardRepository, card: Card): Bool suspend fun <T : IUserData> migrateCard(repo: GenericUserDataRepo<T>, cardRepo: CardRepository, card: Card): Bool {
{
val ghost = card.aquaUser!!.ghostCard val ghost = card.aquaUser!!.ghostCard
// Check if data already exists in the user's ghost card // Check if data already exists in the user's ghost card
async { repo.findByCard(ghost) }?.let { async { repo.findByCard(ghost) }?.let {
// Create a new dummy card for deleted data // Create a new dummy card for deleted data
it.card = async { cardRepo.save(Card().apply { it.card = async {
luid = "Migrated data of ghost card ${ghost.id} for user ${card.aquaUser!!.auId} on ${LocalDateTime.now(ZoneOffset.UTC).isoDateTime()}" cardRepo.save(Card().apply {
// Randomize an extId outside the normal range luid = "Migrated data of ghost card ${ghost.id} for user ${card.aquaUser!!.auId} on ${LocalDateTime.now(ZoneOffset.UTC).isoDateTime()}"
extId = Random.nextLong(0x7FFFFFF7L shl 32, 0x7FFFFFFFL shl 32) // Randomize an extId outside the normal range
registerTime = LocalDateTime.now() extId = Random.nextLong(0x7FFFFFF7L shl 32, 0x7FFFFFFFL shl 32)
accessTime = registerTime registerTime = LocalDateTime.now()
}) } accessTime = registerTime
})
}
async { repo.save(it) } async { repo.save(it) }
} }
@ -158,8 +161,7 @@ suspend fun <T : IUserData> migrateCard(repo: GenericUserDataRepo<T>, cardRepo:
return true return true
} }
suspend fun getSummaryFor(repo: GenericUserDataRepo<*>, card: Card): Map<Str, Any>? suspend fun getSummaryFor(repo: GenericUserDataRepo<*>, card: Card): Map<Str, Any>? {
{
val data = async { repo.findByCard(card) } ?: return null val data = async { repo.findByCard(card) } ?: return null
return mapOf( return mapOf(
"name" to data.userName, "name" to data.userName,
@ -199,18 +201,20 @@ class CardGameService(
} }
} }
suspend fun getSummary(card: Card) = async { mapOf( suspend fun getSummary(card: Card) = async {
"mai2" to getSummaryFor(maimai2, card), mapOf(
"chu3" to getSummaryFor(chusan, card), "mai2" to getSummaryFor(maimai2, card),
"ongeki" to getSummaryFor(ongeki, card), "chu3" to getSummaryFor(chusan, card),
"wacca" to getSummaryFor(wacca, card), "ongeki" to getSummaryFor(ongeki, card),
"diva" to diva.findByPdId(card.extId).getOrNull()?.let { "wacca" to getSummaryFor(wacca, card),
mapOf( "diva" to diva.findByPdId(card.extId).getOrNull()?.let {
"name" to it.playerName, mapOf(
"rating" to it.level, "name" to it.playerName,
) "rating" to it.level,
}, )
) } },
)
}
// Every hour // Every hour
@Scheduled(fixedDelay = 3600000) @Scheduled(fixedDelay = 3600000)
@ -228,4 +232,4 @@ class CardGameService(
} }
} }
} }
} }