fix: bot name and id return, websocket

This commit is contained in:
Lala, Shahd
2026-05-31 21:28:04 +00:00
committed by LQ63
parent 35d7e0dc85
commit 587e34babc
6 changed files with 21 additions and 7 deletions
+5
View File
@@ -49,3 +49,8 @@ graphify-out/
/jacoco-reporter/.venv/ /jacoco-reporter/.venv/
/.claude/settings.local.json /.claude/settings.local.json
/.claude/worktrees/ /.claude/worktrees/
modules/tournament/src/main/resources/keys/dev-public.pem
modules/account/src/main/resources/keys/dev-private.pem
modules/account/src/main/resources/keys/dev-public.pem
modules/core/src/main/resources/keys/dev-public.pem
java_pid2736.hprof
@@ -51,7 +51,7 @@ class BotAccount extends PanacheEntityBase:
@JoinColumn(name = "owner_id", nullable = false) @JoinColumn(name = "owner_id", nullable = false)
var owner: UserAccount = uninitialized var owner: UserAccount = uninitialized
@Column(unique = true, nullable = false, length = 256) @Column(unique = true, nullable = false, length = 1024)
var token: String = uninitialized var token: String = uninitialized
var rating: Int = 1500 var rating: Int = 1500
@@ -153,9 +153,10 @@ class AccountService:
val bot = new BotAccount() val bot = new BotAccount()
bot.name = botName bot.name = botName
bot.owner = owner bot.owner = owner
bot.token = generateBotToken(bot.id) bot.token = UUID.randomUUID().toString
bot.createdAt = Instant.now() bot.createdAt = Instant.now()
botAccountRepository.persist(bot) botAccountRepository.persist(bot)
bot.token = generateBotToken(bot.id, bot.name)
log.infof("Bot account %s created for owner %s", botName, ownerId.toString) log.infof("Bot account %s created for owner %s", botName, ownerId.toString)
Right(bot) Right(bot)
@@ -194,7 +195,7 @@ class AccountService:
case Some(bot) => case Some(bot) =>
if bot.owner.id != ownerId then Left(AccountError.NotAuthorized) if bot.owner.id != ownerId then Left(AccountError.NotAuthorized)
else else
bot.token = generateBotToken(botId) bot.token = generateBotToken(botId, bot.name)
botAccountRepository.persist(bot) botAccountRepository.persist(bot)
Right(bot) Right(bot)
@@ -228,12 +229,13 @@ class AccountService:
officialBotAccountRepository.delete(botId) officialBotAccountRepository.delete(botId)
Right(()) Right(())
private def generateBotToken(botId: UUID): String = private def generateBotToken(botId: UUID, botName: String): String =
Jwt Jwt
.issuer("nowchess") .issuer("nowchess")
.subject(botId.toString) .subject(botId.toString)
.expiresAt(Long.MaxValue) .expiresAt(Long.MaxValue)
.claim("type", "bot") .claim("type", "bot")
.claim("name", botName)
.sign() .sign()
@Transactional @Transactional
@@ -140,7 +140,7 @@ class TournamentService:
Some(CorePlayerInfo(white.botId, white.botName)), Some(CorePlayerInfo(white.botId, white.botName)),
Some(CorePlayerInfo(black.botId, black.botName)), Some(CorePlayerInfo(black.botId, black.botName)),
Some(tc), Some(tc),
if t.rated then Some("Rated") else Some("Casual"), Some("Authenticated"),
) )
Try(coreGameClient.createGame(req)) match Try(coreGameClient.createGame(req)) match
case Failure(ex) => log.errorf(ex, "Failed to create game for round %d in tournament %s", round, tournamentId) case Failure(ex) => log.errorf(ex, "Failed to create game for round %d in tournament %s", round, tournamentId)
@@ -96,6 +96,8 @@ class GameWebSocketResource:
private def resolvePlayerId(handshake: HandshakeRequest): Option[String] = private def resolvePlayerId(handshake: HandshakeRequest): Option[String] =
Option(handshake.header("Authorization")) Option(handshake.header("Authorization"))
.filter(_.nonEmpty) .filter(_.nonEmpty)
.map(h => if h.startsWith("Bearer ") then h.drop(7) else h)
.orElse(Option(handshake.query("token")).filter(_.nonEmpty))
.flatMap(token => Try(jwtParser.parse(token)).toOption) .flatMap(token => Try(jwtParser.parse(token)).toOption)
.map(_.getSubject) .map(_.getSubject)
@@ -35,8 +35,7 @@ class UserWebSocketResource:
@OnOpen @OnOpen
def onOpen(connection: WebSocketConnection, handshake: HandshakeRequest): Unit = def onOpen(connection: WebSocketConnection, handshake: HandshakeRequest): Unit =
val userIdOpt = Option(handshake.header("Authorization")) val userIdOpt = resolveToken(handshake)
.filter(_.nonEmpty)
.flatMap(token => Try(jwtParser.parse(token)).toOption) .flatMap(token => Try(jwtParser.parse(token)).toOption)
.map(_.getSubject) .map(_.getSubject)
@@ -52,6 +51,12 @@ class UserWebSocketResource:
val connectedMsg = s"""{"type":"CONNECTED","userId":"$userId"}""" val connectedMsg = s"""{"type":"CONNECTED","userId":"$userId"}"""
connection.sendText(connectedMsg).subscribe().`with`(_ => (), _ => ()) connection.sendText(connectedMsg).subscribe().`with`(_ => (), _ => ())
private def resolveToken(handshake: HandshakeRequest): Option[String] =
Option(handshake.header("Authorization"))
.filter(_.nonEmpty)
.map(h => if h.startsWith("Bearer ") then h.drop(7) else h)
.orElse(Option(handshake.query("token")).filter(_.nonEmpty))
@OnClose @OnClose
def onClose(connection: WebSocketConnection): Unit = def onClose(connection: WebSocketConnection): Unit =
log.infof("User WebSocket closed — connectionId=%s", connection.id()) log.infof("User WebSocket closed — connectionId=%s", connection.id())