4 Commits

Author SHA1 Message Date
dalbodeule
ba9fb052cd [hotfix] fixed logout redirect and removed unused debug print. 2025-06-06 15:37:52 +09:00
dalbodeule
51232ad593 [hotfix] please, this patch is the final patch! 2025-06-06 15:23:52 +09:00
dalbodeule
77eecaca34 [hotfix] token claim fixed. (2x) 2025-06-04 16:27:32 +09:00
dalbodeule
90230c4691 [hotfix] token claim fixed. 2025-06-04 16:18:02 +09:00
3 changed files with 17 additions and 24 deletions

View File

@@ -46,7 +46,7 @@ object ChzzkHandler {
UserService.getAllUsers().map { UserService.getAllUsers().map {
if(!it.isDisabled) if(!it.isDisabled)
try { try {
Connector.getChannel(it.token)?.let { token -> addUser(token, it) } getChannel(it.token)?.let { token -> addUser(token, it) }
} catch(e: Exception) { } catch(e: Exception) {
logger.info("Exception: ${it.token}(${it.username}) not found. ${e.stackTraceToString()}") logger.info("Exception: ${it.token}(${it.username}) not found. ${e.stackTraceToString()}")
} }
@@ -223,7 +223,10 @@ class UserHandler(
throw RuntimeException("AccessToken or RefreshToken is not valid.") throw RuntimeException("AccessToken or RefreshToken is not valid.")
} }
try { try {
val tokens = Connector.client.refreshAccessToken(user.refreshToken!!) val tokens = user.refreshToken?.let { token -> Connector.client.refreshAccessToken(token)}
if(tokens == null) {
throw RuntimeException("AccessToken is not valid.")
}
client = Connector.getClient(tokens.first, tokens.second) client = Connector.getClient(tokens.first, tokens.second)
UserService.setRefreshToken(user, tokens.first, tokens.second) UserService.setRefreshToken(user, tokens.first, tokens.second)
chatChannelId = getChzzkChannelId(channel.channelId) chatChannelId = getChzzkChannelId(channel.channelId)

View File

@@ -1,24 +1,15 @@
package space.mori.chzzk_bot.chatbot.utils package space.mori.chzzk_bot.chatbot.utils
import com.google.gson.Gson import com.google.gson.reflect.TypeToken
import okhttp3.OkHttpClient import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Request import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import space.mori.chzzk_bot.chatbot.chzzk.dotenv
import space.mori.chzzk_bot.common.utils.IData
import space.mori.chzzk_bot.common.utils.client import space.mori.chzzk_bot.common.utils.client
import xyz.r2turntrue.chzzk4j.ChzzkClient import xyz.r2turntrue.chzzk4j.ChzzkClient
import java.io.IOException import java.io.IOException
val client = OkHttpClient.Builder()
.addNetworkInterceptor { chain ->
chain.proceed(
chain.request()
.newBuilder()
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
.build()
)
}
.build()
val gson = Gson()
data class RefreshTokenResponse( data class RefreshTokenResponse(
val accessToken: String, val accessToken: String,
@@ -29,25 +20,25 @@ data class RefreshTokenResponse(
) )
fun ChzzkClient.refreshAccessToken(refreshToken: String): Pair<String, String> { fun ChzzkClient.refreshAccessToken(refreshToken: String): Pair<String, String> {
val url = "https://chzzk.naver.com/auth/v1/token" val url = "https://openapi.chzzk.naver.com/auth/v1/token"
val request = Request.Builder() val request = Request.Builder()
.url(url) .url(url)
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.post(gson.toJson(mapOf( .post(gson.toJson(mapOf(
"grantType" to "refresh_token", "grantType" to "refresh_token",
"refreshToken" to refreshToken, "refreshToken" to refreshToken,
"clientId" to this.apiClientId, "clientId" to dotenv["NAVER_CLIENT_ID"],
"clientSecret" to this.apiSecret "clientSecret" to dotenv["NAVER_CLIENT_SECRET"]
)).toRequestBody()) )).toRequestBody("application/json; charset=utf-8".toMediaType()))
.build() .build()
client.newCall(request).execute().use { response -> client.newCall(request).execute().use { response ->
try { try {
if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}") if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}")
val body = response.body?.string() val body = response.body?.string()
val data = gson.fromJson(body, RefreshTokenResponse::class.java) val data = gson.fromJson(body, object: TypeToken<IData<RefreshTokenResponse>>() {})
return Pair(data.accessToken, data.refreshToken) return Pair(data.content.accessToken, data.content.refreshToken)
} catch(e: Exception) { } catch(e: Exception) {
throw e throw e
} }

View File

@@ -181,7 +181,7 @@ val server = embeddedServer(Netty, port = 8080, ) {
clientSecret = dotenv["NAVER_CLIENT_SECRET"] clientSecret = dotenv["NAVER_CLIENT_SECRET"]
) )
val response = applicationHttpClient.post("https://chzzk.naver.com/auth/v1/token") { val response = applicationHttpClient.post("https://openapi.chzzk.naver.com/auth/v1/token") {
contentType(ContentType.Application.Json) contentType(ContentType.Application.Json)
setBody(tokenRequest) setBody(tokenRequest)
} }
@@ -228,8 +228,7 @@ val server = embeddedServer(Netty, port = 8080, ) {
// common: logout // common: logout
get("/logout") { get("/logout") {
call.sessions.clear<UserSession>() call.sessions.clear<UserSession>()
call.response.status(HttpStatusCode.OK) call.respondRedirect(getFrontendURL(""))
return@get
} }
} }