46 lines
1.8 KiB
Plaintext
46 lines
1.8 KiB
Plaintext
/*
|
||||
|
|
* Copyright (c) 2025 SimcuTeam. All rights reserved.
|
|||
|
|
* 移植自 C# 项目 SimApi(E:\simcu\simapi-net),遵循 MIT 许可证。
|
|||
|
|
* AuthSDK/SimApiAuthCenterMiddleware:网关透传认证中间件。
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
package simapi.authsdk
|
|||
|
|
|
|||
|
|
import soulsoft_web_http.*
|
|||
|
|
import simapi.communications.*
|
|||
|
|
import simapi.configurations.*
|
|||
|
|
import simapi.helpers.*
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 网关透传认证中间件(对齐 C# SimApiAuthCenterMiddleware):
|
|||
|
|
* 当请求带 X-SimApi-Gate-Auth / X-SimApi-Gate-Time / X-SimApi-Gate-Sign 三头时,
|
|||
|
|
* 校验 MD5 签名(appId=..&auth=..&time=..&appKey=..),通过则 Base64 解码登录信息写入 LoginInfo。
|
|||
|
|
*/
|
|||
|
|
public class SimApiAuthCenterMiddleware <: IMiddleware {
|
|||
|
|
private let _options: SimApiOptions
|
|||
|
|
|
|||
|
|
public init(options: SimApiOptions) {
|
|||
|
|
this._options = options
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public func invoke(context: HttpContext, next: RequestDelegate): Unit {
|
|||
|
|
let auth = context.request.headers.get("X-SimApi-Gate-Auth")
|
|||
|
|
let time = context.request.headers.get("X-SimApi-Gate-Time")
|
|||
|
|
let sign = context.request.headers.get("X-SimApi-Gate-Sign")
|
|||
|
|
if (auth != None && time != None && sign != None) {
|
|||
|
|
let authValue = auth.getOrThrow()
|
|||
|
|
let timeValue = time.getOrThrow()
|
|||
|
|
let signValue = sign.getOrThrow()
|
|||
|
|
if (!authValue.isEmpty()) {
|
|||
|
|
let authOptions = _options.simApiAuthCenterOptions
|
|||
|
|
let signStr = "appId=${authOptions.appId}&auth=${authValue}&time=${timeValue}&appKey=${authOptions.appKey}"
|
|||
|
|
if (SimApiUtil.md5(signStr) == signValue) {
|
|||
|
|
let login = SimApiUtil.base64DecodeTo<SimApiLoginItem>(authValue)
|
|||
|
|
context.items["LoginInfo"] = login
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
next(context)
|
|||
|
|
}
|
|||
|
|
}
|