41 lines
1.1 KiB
Plaintext
41 lines
1.1 KiB
Plaintext
/*
|
|
* Copyright (c) 2025 SimcuTeam. All rights reserved.
|
|
* 遵循 MIT 许可证。
|
|
*/
|
|
|
|
package simcu::simapi.middlewares
|
|
|
|
import soulsoft_web_http.*
|
|
import simcu::simapi.communications.*
|
|
import simcu::simapi.helpers.*
|
|
|
|
/**
|
|
* 认证信息获取中间件:从 Header Token 或 Query token 解析登录信息并注入上下文。
|
|
*/
|
|
public class SimApiAuthMiddleware <: IMiddleware {
|
|
private let _auth: SimApiAuth
|
|
|
|
public init(auth: SimApiAuth) {
|
|
_auth = auth
|
|
}
|
|
|
|
/**
|
|
* 中间件入口(IMiddleware 风格)。
|
|
*/
|
|
public func invoke(context: HttpContext, next: RequestDelegate): Unit {
|
|
var token = context.request.headers.get("Token")
|
|
if (token == None || token == Some("")) {
|
|
token = context.request.query.get("token")
|
|
}
|
|
if (let Some(token) <- token) {
|
|
if (!token.isEmpty()) {
|
|
if (let Some(login) <- _auth.getLogin(token)) {
|
|
context.items["LoginToken"] = token
|
|
context.items["LoginInfo"] = login
|
|
}
|
|
}
|
|
}
|
|
next(context)
|
|
}
|
|
}
|