Skip to content

Commit

Permalink
Fix HTTP API server and Websocket not parsing Authorization header pr…
Browse files Browse the repository at this point in the history
…operly
  • Loading branch information
yyuueexxiinngg committed Aug 22, 2020
1 parent 5c866e4 commit 25e0e75
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/main/kotlin/tech/mihoyo/mirai/web/http/HttpApiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ internal suspend fun ApplicationCall.responseDTO(dto: CQResponseDTO) {

suspend fun checkAccessToken(call: ApplicationCall, serviceConfig: HttpApiServerServiceConfig): Boolean {
if (serviceConfig.accessToken != null && serviceConfig.accessToken != "") {
val accessToken = call.parameters["access_token"] ?: call.request.headers["Authorization"]
val accessToken =
call.parameters["access_token"] ?: call.request.headers["Authorization"]?.let {
Regex("""(?:[Tt]oken|Bearer)\s+(.*)""").find(it)?.groupValues?.get(1)
}
if (accessToken != null) {
if (accessToken != serviceConfig.accessToken) {
call.respond(HttpStatusCode.Forbidden)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ private inline fun Route.cqWebsocket(
) {
webSocket(path) {
if (serviceConfig.accessToken != null && serviceConfig.accessToken != "") {
val accessToken = call.parameters["access_token"]
val accessToken =
call.parameters["access_token"] ?: call.request.headers["Authorization"]?.let {
Regex("""(?:[Tt]oken|Bearer)\s+(.*)""").find(it)?.groupValues?.get(1)
}
if (accessToken != serviceConfig.accessToken) {
close(CloseReason(CloseReason.Codes.NORMAL, "accessToken不正确"))
return@webSocket
Expand Down

0 comments on commit 25e0e75

Please sign in to comment.