Skip to content

Commit

Permalink
feat(core): 支持api和ws配置不同的access token
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Jun 21, 2024
1 parent 9ad2d35 commit d8c56b2
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
8 changes: 6 additions & 2 deletions Writerside/topics/onebot11-bot-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/P.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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的唯一标识。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
* **额外的**序列化模块信息。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public suspend fun OneBotApi<*>.requestBy(
): HttpResponse = request(
client = bot.apiClient,
host = bot.apiHost,
accessToken = bot.accessToken
accessToken = bot.apiAccessToken
)

/**
Expand All @@ -59,7 +59,7 @@ public suspend fun OneBotApi<*>.requestRawBy(
): String = requestRaw(
client = bot.apiClient,
host = bot.apiHost,
accessToken = bot.accessToken
accessToken = bot.apiAccessToken
)

/**
Expand All @@ -77,7 +77,7 @@ public suspend fun <T : Any> OneBotApi<T>.requestResultBy(
): OneBotApiResult<T> = requestResult(
client = bot.apiClient,
host = bot.apiHost,
accessToken = bot.accessToken,
accessToken = bot.apiAccessToken,
decoder = bot.decoderJson
)

Expand All @@ -99,6 +99,6 @@ public suspend fun <T : Any> OneBotApi<T>.requestDataBy(
): T = requestData(
client = bot.apiClient,
host = bot.apiHost,
accessToken = bot.accessToken,
accessToken = bot.apiAccessToken,
decoder = bot.decoderJson
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -284,7 +285,7 @@ internal class OneBotBotImpl(
return wsClient.webSocketSession {
url {
takeFrom(eventServerHost)
accessToken?.also { bearerAuth(it) }
eventAccessToken?.also { bearerAuth(it) }
}
}
}
Expand Down

0 comments on commit d8c56b2

Please sign in to comment.