refactor: 移除 soulsoft_serialization / soulsoft_net_http 依赖——全部 HTTP 改 stdx.net.http
- SimApiStorage 4 处 S3 请求(PUT 上传/HEAD 检桶/PUT 建桶/POST 删除)改 stdx ClientBuilder/HttpRequestBuilder - SimApiAuthCenter.verifySign 改 stdx(post + body + 读响应) - cjpm.toml 删除 soulsoft_serialization、soulsoft_net_http(soulsoft web 栈传递依赖提供序列化) - S3 全链路实测通过(上传/预签名/删除)
This commit is contained in:
@@ -7,10 +7,10 @@
|
||||
package simapi.authsdk
|
||||
|
||||
import std.collection.*
|
||||
import std.io.*
|
||||
import stdx.net.http.*
|
||||
import stdx.net.tls.*
|
||||
import stdx.net.tls.common.*
|
||||
import soulsoft_net_http.{HttpClient, HttpRequestMessage, JsonContent}
|
||||
import soulsoft_net_http.{HttpMethod as NetHttpMethod}
|
||||
import simapi_serialization.*
|
||||
import simapi.communications.*
|
||||
import simapi.helpers.*
|
||||
@@ -39,31 +39,22 @@ public class SimApiAuthCenter {
|
||||
*/
|
||||
public func verifySign(appId: String, timestamp: String, nonce: String, sign: String): Unit {
|
||||
let url = "${_client.server}/api/auth/sign/verify?appId=${appId}×tamp=${timestamp}&nonce=${nonce}&sign=${sign}"
|
||||
let http = HttpClient.create { builder =>
|
||||
builder.noProxy()
|
||||
var tls = TlsClientConfig()
|
||||
tls.verifyMode = CertificateVerifyMode.TrustAll
|
||||
match (_client.server.indexOf("://")) {
|
||||
case Some(i) =>
|
||||
let rest = _client.server[i + 3..]
|
||||
let slash = rest.indexOf("/") ?? rest.size
|
||||
let q = rest.indexOf("?") ?? rest.size
|
||||
let end = if (slash < q) { slash } else { q }
|
||||
let host = rest[0..end]
|
||||
if (!host.isEmpty()) {
|
||||
tls.serverName = Some(host)
|
||||
}
|
||||
case None => ()
|
||||
}
|
||||
builder.tlsConfig(tls)
|
||||
}
|
||||
let http = ClientBuilder().
|
||||
noProxy().
|
||||
tlsConfig(buildTlsConfig(_client.server)).
|
||||
build()
|
||||
try {
|
||||
let request = HttpRequestMessage(NetHttpMethod.Post, url)
|
||||
request.content = JsonContent.create("{}")
|
||||
let request = HttpRequestBuilder().
|
||||
post().
|
||||
url(url).
|
||||
header("Content-Type", "application/json").
|
||||
body("{}").
|
||||
build()
|
||||
let response = http.send(request)
|
||||
try {
|
||||
response.ensureSuccessStatusCode()
|
||||
let json = response.content.readAsString()
|
||||
SimApiError.errorWhenFalse(isSuccess(response.status), code: Int64(response.status),
|
||||
message: "HTTP ERROR: ${response.status}")
|
||||
let json = readBodyText(response.body)
|
||||
let resp = JsonSerializer.Deserialize<SimApiBaseResponse>(json)
|
||||
SimApiError.errorWhen(resp.code != 200, code: 400, message: "签名验证失败")
|
||||
} finally {
|
||||
@@ -74,6 +65,42 @@ public class SimApiAuthCenter {
|
||||
}
|
||||
}
|
||||
|
||||
/// 构建 TLS 配置:信任所有证书 + SNI 域名
|
||||
private static func buildTlsConfig(server: String): TlsClientConfig {
|
||||
var tls = TlsClientConfig()
|
||||
tls.verifyMode = CertificateVerifyMode.TrustAll
|
||||
match (server.indexOf("://")) {
|
||||
case Some(i) =>
|
||||
let rest = server[i + 3..]
|
||||
let slash = rest.indexOf("/") ?? rest.size
|
||||
let q = rest.indexOf("?") ?? rest.size
|
||||
let end = if (slash < q) { slash } else { q }
|
||||
let host = rest[0..end]
|
||||
if (!host.isEmpty()) {
|
||||
tls.serverName = Some(host)
|
||||
}
|
||||
case None => ()
|
||||
}
|
||||
tls
|
||||
}
|
||||
|
||||
/// 2xx 视为成功
|
||||
private static func isSuccess(status: UInt16): Bool {
|
||||
status >= 200 && status < 300
|
||||
}
|
||||
|
||||
/// 读取响应体 InputStream 为字符串
|
||||
private static func readBodyText(body: InputStream): String {
|
||||
var buffer = Array<Byte>(4096, repeat: 0)
|
||||
var sb = StringBuilder()
|
||||
var read = body.read(buffer)
|
||||
while (read > 0) {
|
||||
sb.appendFromUtf8(buffer.slice(0, read))
|
||||
read = body.read(buffer)
|
||||
}
|
||||
sb.toString()
|
||||
}
|
||||
|
||||
// ===== 群组相关 =====
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user