|
|
@@ -10,6 +10,7 @@
|
|
|
package net.mamoe.mirai.api.http.adapter.http.router
|
|
|
|
|
|
import io.ktor.server.application.*
|
|
|
+import io.ktor.server.request.*
|
|
|
import io.ktor.server.response.*
|
|
|
import io.ktor.server.routing.*
|
|
|
import net.mamoe.mirai.api.http.adapter.common.IllegalSessionException
|
|
|
@@ -17,6 +18,8 @@ import net.mamoe.mirai.api.http.adapter.common.StateCode
|
|
|
import net.mamoe.mirai.api.http.adapter.http.HttpAdapterSetting
|
|
|
import net.mamoe.mirai.api.http.adapter.http.session.asHttpSession
|
|
|
import net.mamoe.mirai.api.http.adapter.http.session.unloadHttpSession
|
|
|
+import net.mamoe.mirai.api.http.adapter.internal.dto.BindDTO
|
|
|
+import net.mamoe.mirai.api.http.adapter.internal.dto.VerifyDTO
|
|
|
import net.mamoe.mirai.api.http.adapter.internal.dto.VerifyRetDTO
|
|
|
import net.mamoe.mirai.api.http.context.MahContextHolder
|
|
|
import net.mamoe.mirai.api.http.util.getBotOrThrow
|
|
|
@@ -29,40 +32,42 @@ internal fun Application.authRouter(setting: HttpAdapterSetting) = routing {
|
|
|
/**
|
|
|
* 进行认证
|
|
|
*/
|
|
|
- httpVerify("/verify") {
|
|
|
- if (!MahContextHolder.enableVerify
|
|
|
- || it.verifyKey == MahContextHolder.sessionManager.verifyKey
|
|
|
- ) {
|
|
|
- val session = if (MahContextHolder.singleMode) {
|
|
|
- MahContextHolder.createSingleSession(verified = true)
|
|
|
- .asHttpSession(setting.unreadQueueMaxSize)
|
|
|
- } else {
|
|
|
- MahContextHolder.sessionManager.createTempSession()
|
|
|
- }
|
|
|
+ post("/verify") {
|
|
|
+ val verifyDTO = call.receive<VerifyDTO>()
|
|
|
+ if (MahContextHolder.enableVerify && verifyDTO.verifyKey != MahContextHolder.sessionManager.verifyKey) {
|
|
|
+ call.respond(StateCode.AuthKeyFail)
|
|
|
+ return@post
|
|
|
+ }
|
|
|
|
|
|
- call.respond(VerifyRetDTO(0, session.key))
|
|
|
- return@httpVerify
|
|
|
+ val session = if (MahContextHolder.singleMode) {
|
|
|
+ MahContextHolder.createSingleSession(verified = true)
|
|
|
+ .asHttpSession(setting.unreadQueueMaxSize)
|
|
|
+ } else {
|
|
|
+ MahContextHolder.sessionManager.createTempSession()
|
|
|
}
|
|
|
|
|
|
- call.respond(StateCode.AuthKeyFail)
|
|
|
+ call.respond(VerifyRetDTO(0, session.key))
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 验证并分配session
|
|
|
*/
|
|
|
- httpBind("/bind") {
|
|
|
+ post("/bind") {
|
|
|
if (MahContextHolder.singleMode) {
|
|
|
call.respond(StateCode.NoOperateSupport)
|
|
|
- return@httpBind
|
|
|
+ return@post
|
|
|
}
|
|
|
- val session = MahContextHolder[it.sessionKey] ?: kotlin.run {
|
|
|
+
|
|
|
+ val bindDTO = call.receive<BindDTO>()
|
|
|
+
|
|
|
+ val session = MahContextHolder[bindDTO.sessionKey] ?: kotlin.run {
|
|
|
call.respond(StateCode.IllegalSession)
|
|
|
- return@httpBind
|
|
|
+ return@post
|
|
|
}
|
|
|
|
|
|
if (!session.isAuthed) {
|
|
|
- val bot = getBotOrThrow(it.qq)
|
|
|
- MahContextHolder.sessionManager.authSession(bot, it.sessionKey)
|
|
|
+ val bot = getBotOrThrow(bindDTO.qq)
|
|
|
+ MahContextHolder.sessionManager.authSession(bot, bindDTO.sessionKey)
|
|
|
.asHttpSession(setting.unreadQueueMaxSize)
|
|
|
}
|
|
|
call.respond(StateCode.Success)
|
|
|
@@ -71,9 +76,11 @@ internal fun Application.authRouter(setting: HttpAdapterSetting) = routing {
|
|
|
/**
|
|
|
* 释放session
|
|
|
*/
|
|
|
- httpBind("/release") {
|
|
|
- val bot = getBotOrThrow(it.qq)
|
|
|
- val session = MahContextHolder[it.sessionKey] ?: throw IllegalSessionException
|
|
|
+ post("/release") {
|
|
|
+ val bindDTO = call.receive<BindDTO>()
|
|
|
+
|
|
|
+ val bot = getBotOrThrow(bindDTO.qq)
|
|
|
+ val session = MahContextHolder[bindDTO.sessionKey] ?: throw IllegalSessionException
|
|
|
if (bot.id == session.bot.id) {
|
|
|
session.apply {
|
|
|
unloadHttpSession()
|