fix: bot name and id return, websocket
This commit is contained in:
@@ -51,7 +51,7 @@ class BotAccount extends PanacheEntityBase:
|
||||
@JoinColumn(name = "owner_id", nullable = false)
|
||||
var owner: UserAccount = uninitialized
|
||||
|
||||
@Column(unique = true, nullable = false, length = 256)
|
||||
@Column(unique = true, nullable = false, length = 1024)
|
||||
var token: String = uninitialized
|
||||
|
||||
var rating: Int = 1500
|
||||
|
||||
@@ -153,9 +153,10 @@ class AccountService:
|
||||
val bot = new BotAccount()
|
||||
bot.name = botName
|
||||
bot.owner = owner
|
||||
bot.token = generateBotToken(bot.id)
|
||||
bot.token = UUID.randomUUID().toString
|
||||
bot.createdAt = Instant.now()
|
||||
botAccountRepository.persist(bot)
|
||||
bot.token = generateBotToken(bot.id, bot.name)
|
||||
log.infof("Bot account %s created for owner %s", botName, ownerId.toString)
|
||||
Right(bot)
|
||||
|
||||
@@ -194,7 +195,7 @@ class AccountService:
|
||||
case Some(bot) =>
|
||||
if bot.owner.id != ownerId then Left(AccountError.NotAuthorized)
|
||||
else
|
||||
bot.token = generateBotToken(botId)
|
||||
bot.token = generateBotToken(botId, bot.name)
|
||||
botAccountRepository.persist(bot)
|
||||
Right(bot)
|
||||
|
||||
@@ -228,12 +229,13 @@ class AccountService:
|
||||
officialBotAccountRepository.delete(botId)
|
||||
Right(())
|
||||
|
||||
private def generateBotToken(botId: UUID): String =
|
||||
private def generateBotToken(botId: UUID, botName: String): String =
|
||||
Jwt
|
||||
.issuer("nowchess")
|
||||
.subject(botId.toString)
|
||||
.expiresAt(Long.MaxValue)
|
||||
.claim("type", "bot")
|
||||
.claim("name", botName)
|
||||
.sign()
|
||||
|
||||
@Transactional
|
||||
|
||||
+1
-1
@@ -140,7 +140,7 @@ class TournamentService:
|
||||
Some(CorePlayerInfo(white.botId, white.botName)),
|
||||
Some(CorePlayerInfo(black.botId, black.botName)),
|
||||
Some(tc),
|
||||
if t.rated then Some("Rated") else Some("Casual"),
|
||||
Some("Authenticated"),
|
||||
)
|
||||
Try(coreGameClient.createGame(req)) match
|
||||
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] =
|
||||
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))
|
||||
.flatMap(token => Try(jwtParser.parse(token)).toOption)
|
||||
.map(_.getSubject)
|
||||
|
||||
|
||||
@@ -35,8 +35,7 @@ class UserWebSocketResource:
|
||||
|
||||
@OnOpen
|
||||
def onOpen(connection: WebSocketConnection, handshake: HandshakeRequest): Unit =
|
||||
val userIdOpt = Option(handshake.header("Authorization"))
|
||||
.filter(_.nonEmpty)
|
||||
val userIdOpt = resolveToken(handshake)
|
||||
.flatMap(token => Try(jwtParser.parse(token)).toOption)
|
||||
.map(_.getSubject)
|
||||
|
||||
@@ -52,6 +51,12 @@ class UserWebSocketResource:
|
||||
val connectedMsg = s"""{"type":"CONNECTED","userId":"$userId"}"""
|
||||
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
|
||||
def onClose(connection: WebSocketConnection): Unit =
|
||||
log.infof("User WebSocket closed — connectionId=%s", connection.id())
|
||||
|
||||
Reference in New Issue
Block a user