diff --git a/Writerside/topics/onebot11-bot-config.md b/Writerside/topics/onebot11-bot-config.md index d107303..47071de 100644 --- a/Writerside/topics/onebot11-bot-config.md +++ b/Writerside/topics/onebot11-bot-config.md @@ -26,8 +26,12 @@ Bot配置文件通常情况下是配合Spring Boot starter的时候用的。 "apiServerHost": "http://localhost:3000", // 订阅事件的服务器地址,是个ws/wss路径,默认localhost:3001 "eventServerHost": "ws://localhost:3001", - // 配置的 token,可以是null - "accessToken": null + // 配置的 token,可以是null, 代表同时配置 apiAccessToken 和 eventAccessToken + "accessToken": null, + // 用于API请求时用的 token,默认 null + "apiAccessToken": null, + // 用于连接事件订阅ws时用的 token,默认 null + "eventAccessToken": null }, // 额外的可选配置 // config本身以及其内的各项属性绝大多数都可省略或null diff --git a/buildSrc/src/main/kotlin/P.kt b/buildSrc/src/main/kotlin/P.kt index eb07e7c..e466c04 100644 --- a/buildSrc/src/main/kotlin/P.kt +++ b/buildSrc/src/main/kotlin/P.kt @@ -41,7 +41,7 @@ object P { override val homepage: String get() = HOMEPAGE - private val baseVersion = v(0, 8, 0) + private val baseVersion = v(0, 9, 0) val snapshotVersion = baseVersion - Version.SNAPSHOT override val version = if (isSnapshot()) snapshotVersion else baseVersion diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBot.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBot.kt index 25bd56a..a17edec 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBot.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBot.kt @@ -89,8 +89,24 @@ public interface OneBotBot : Bot { /** * [OneBotBotConfiguration] 中配置的 accessToken。 + * + * 分开使用 [apiAccessToken] 或 [eventAccessToken], + * [accessToken] 将会在 v1.0.0 版本后移除。 */ + @Deprecated("Use `apiAccessToken` or `eventAccessToken`", ReplaceWith("apiAccessToken")) public val accessToken: String? + get() = apiAccessToken + // TODO Removal + + /** + * [OneBotBotConfiguration] 中配置的 [apiAccessToken][OneBotBotConfiguration.apiAccessToken]。 + */ + public val apiAccessToken: String? + + /** + * [OneBotBotConfiguration] 中配置的 [eventAccessToken][OneBotBotConfiguration.eventAccessToken]。 + */ + public val eventAccessToken: String? /** * 当前Bot的唯一标识。 diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotConfiguration.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotConfiguration.kt index f4416b2..08b310f 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotConfiguration.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotConfiguration.kt @@ -58,8 +58,42 @@ public class OneBotBotConfiguration { /** * 参考 [鉴权](https://github.com/botuniverse/onebot-11/blob/master/communication/authorization.md) * + * 可分开配置 [apiAccessToken] 或 [eventAccessToken],或使用函数 [accessToken] 统一配置。 + * + * @see apiAccessToken + * @see eventAccessToken */ - public var accessToken: String? = null + @Deprecated("Use `apiAccessToken` or `eventAccessToken`") + public var accessToken: String? + get() = null + set(value) { + accessToken(value) + } + + /** + * 用于正向请求API的 accessToken。 + * 参考 [鉴权](https://github.com/botuniverse/onebot-11/blob/master/communication/authorization.md) + * + */ + public var apiAccessToken: String? = null + + /** + * 用于连接正向ws接收事件的 accessToken。 + * 参考 [鉴权](https://github.com/botuniverse/onebot-11/blob/master/communication/authorization.md) + * + */ + public var eventAccessToken: String? = null + + /** + * 同时配置 [apiAccessToken] 和 [eventAccessToken]。 + * + * @see apiAccessToken + * @see eventAccessToken + */ + public fun accessToken(value: String?) { + apiAccessToken = value + eventAccessToken = value + } /** * **额外的**序列化模块信息。 diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotRequests.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotRequests.kt index bc3a671..79a08f6 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotRequests.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotRequests.kt @@ -41,7 +41,7 @@ public suspend fun OneBotApi<*>.requestBy( ): HttpResponse = request( client = bot.apiClient, host = bot.apiHost, - accessToken = bot.accessToken + accessToken = bot.apiAccessToken ) /** @@ -59,7 +59,7 @@ public suspend fun OneBotApi<*>.requestRawBy( ): String = requestRaw( client = bot.apiClient, host = bot.apiHost, - accessToken = bot.accessToken + accessToken = bot.apiAccessToken ) /** @@ -77,7 +77,7 @@ public suspend fun OneBotApi.requestResultBy( ): OneBotApiResult = requestResult( client = bot.apiClient, host = bot.apiHost, - accessToken = bot.accessToken, + accessToken = bot.apiAccessToken, decoder = bot.decoderJson ) @@ -99,6 +99,6 @@ public suspend fun OneBotApi.requestDataBy( ): T = requestData( client = bot.apiClient, host = bot.apiHost, - accessToken = bot.accessToken, + accessToken = bot.apiAccessToken, decoder = bot.decoderJson ) diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotSerializableConfiguration.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotSerializableConfiguration.kt index 06ee067..15b3f8c 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotSerializableConfiguration.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/OneBotBotSerializableConfiguration.kt @@ -47,6 +47,8 @@ public data class OneBotBotSerializableConfiguration( val apiServerHost: String? = null, val eventServerHost: String? = null, val accessToken: String? = null, + val apiAccessToken: String? = null, + val eventAccessToken: String? = null, ) @Serializable @@ -101,7 +103,9 @@ public data class OneBotBotSerializableConfiguration( conf.botUniqueId = authorization.botUniqueId authorization.apiServerHost?.also { conf.apiServerHost = Url(it) } authorization.eventServerHost?.also { conf.eventServerHost = Url(it) } - authorization.accessToken?.also { conf.accessToken = it } + authorization.accessToken?.also { conf.accessToken(it) } + authorization.apiAccessToken?.also { conf.apiAccessToken = it } + authorization.eventAccessToken?.also { conf.eventAccessToken = it } config?.apply { apiHttpRequestTimeoutMillis?.also { conf.apiHttpRequestTimeoutMillis = it } diff --git a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/internal/OneBotBotImpl.kt b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/internal/OneBotBotImpl.kt index fa725a4..cdde775 100644 --- a/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/internal/OneBotBotImpl.kt +++ b/simbot-component-onebot-v11/simbot-component-onebot-v11-core/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/bot/internal/OneBotBotImpl.kt @@ -199,7 +199,8 @@ internal class OneBotBotImpl( override val apiHost: Url = configuration.apiServerHost - override val accessToken: String? = configuration.accessToken + override val apiAccessToken: String? = configuration.apiAccessToken + override val eventAccessToken: String? = configuration.eventAccessToken override val id: ID = uniqueId.ID @@ -284,7 +285,7 @@ internal class OneBotBotImpl( return wsClient.webSocketSession { url { takeFrom(eventServerHost) - accessToken?.also { bearerAuth(it) } + eventAccessToken?.also { bearerAuth(it) } } } }