Compare commits

..

No commits in common. "ba9fb052cd6a552ed61d6e75d164e635b1651459" and "77eecaca34bcd718365720094b25b47b905c7d2a" have entirely different histories.

3 changed files with 20 additions and 7 deletions

View File

@ -46,7 +46,7 @@ object ChzzkHandler {
UserService.getAllUsers().map { UserService.getAllUsers().map {
if(!it.isDisabled) if(!it.isDisabled)
try { try {
getChannel(it.token)?.let { token -> addUser(token, it) } Connector.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,7 @@ class UserHandler(
throw RuntimeException("AccessToken or RefreshToken is not valid.") throw RuntimeException("AccessToken or RefreshToken is not valid.")
} }
try { try {
val tokens = user.refreshToken?.let { token -> Connector.client.refreshAccessToken(token)} val tokens = user.refreshToken?.let { token -> Connector.client.refreshAccessToken(token) }
if(tokens == null) { if(tokens == null) {
throw RuntimeException("AccessToken is not valid.") throw RuntimeException("AccessToken is not valid.")
} }

View File

@ -1,15 +1,27 @@
package space.mori.chzzk_bot.chatbot.utils package space.mori.chzzk_bot.chatbot.utils
import com.google.gson.reflect.TypeToken import com.google.gson.Gson
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
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.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,
@ -36,9 +48,9 @@ fun ChzzkClient.refreshAccessToken(refreshToken: String): Pair<String, String> {
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, object: TypeToken<IData<RefreshTokenResponse>>() {}) val data = gson.fromJson(body, RefreshTokenResponse::class.java)
return Pair(data.content.accessToken, data.content.refreshToken) return Pair(data.accessToken, data.refreshToken)
} catch(e: Exception) { } catch(e: Exception) {
throw e throw e
} }

View File

@ -228,7 +228,8 @@ val server = embeddedServer(Netty, port = 8080, ) {
// common: logout // common: logout
get("/logout") { get("/logout") {
call.sessions.clear<UserSession>() call.sessions.clear<UserSession>()
call.respondRedirect(getFrontendURL("")) call.response.status(HttpStatusCode.OK)
return@get
} }
} }