Browse Source
签名: - 签名范围改为「业务参数 + access_token + app_key + timestamp」,按 key 升序拼接 - MD5 输出 32 位大写,再用 appId 做 AES/ECB/PKCS5Padding 加密并 Base64 - 集合参数先 JSON 序列化再参与签名 - appId 不是 16/24/32 字节时直接抛 IllegalArgumentException,不静默补齐 请求层: - 新增 LingxingEnvelope,业务错误看 body.code,映射限流/鉴权/业务异常 - 新增 LingxingRateLimiter,按接口路径分桶,订单列表 10 req/s - 新增 LingxingRequestFactory,access_token 只进 Authorization 但参与签名 - 订单列表请求体改为 start_time/end_time(epoch 秒)+ platform_code/store_id 数组 - 分页改为「满页 + total 兜底」,避免最后一页多发一次请求 - 令牌缓存键从 store_id 改为 appKey,同 app 多店铺共用 token - 映射异常包装成 ValidationException 并保留原始报文,避免以 NPE 形式冒出feature/supplier-lingxing
12 changed files with 638 additions and 148 deletions
@ -0,0 +1,120 @@ |
|||||||
|
package common.ecommerce.lingxing; |
||||||
|
|
||||||
|
import common.ecommerce.error.AuthException; |
||||||
|
import common.ecommerce.error.ChannelException; |
||||||
|
import common.ecommerce.error.PlatformBusinessException; |
||||||
|
import common.ecommerce.error.RateLimitException; |
||||||
|
import common.ecommerce.http.HttpResponse; |
||||||
|
import common.ecommerce.http.JacksonJsonCodec; |
||||||
|
import common.ecommerce.http.Responses; |
||||||
|
|
||||||
|
/** |
||||||
|
* 领星响应信封。 |
||||||
|
* |
||||||
|
* <p>领星业务错误一律返回 HTTP 200,只能看 body 里的 {@code code}: |
||||||
|
* {@link common.ecommerce.http.Responses#throwIfNotSuccess} 只判 HTTP 状态码,不够用。</p> |
||||||
|
* |
||||||
|
* <ul> |
||||||
|
* <li>{@code 0} / {@code "0"} / {@code "200"}:成功(令牌接口用 "200")</li> |
||||||
|
* <li>{@code 3001008}:限流</li> |
||||||
|
* <li>{@code 2001003}:token 失效</li> |
||||||
|
* <li>{@code 2001006} / {@code 2001007}:签名错误 / 签名过期</li> |
||||||
|
* <li>{@code 3001002}:IP 不在白名单</li> |
||||||
|
* </ul> |
||||||
|
*/ |
||||||
|
public final class LingxingEnvelope { |
||||||
|
|
||||||
|
static final String CODE_RATE_LIMITED = "3001008"; |
||||||
|
static final String CODE_TOKEN_INVALID = "2001003"; |
||||||
|
static final String CODE_SIGN_ERROR = "2001006"; |
||||||
|
static final String CODE_SIGN_EXPIRED = "2001007"; |
||||||
|
static final String CODE_IP_DENIED = "3001002"; |
||||||
|
|
||||||
|
/** 限流响应不带 Retry-After,按 1s 退避。 */ |
||||||
|
private static final long DEFAULT_RETRY_AFTER_MILLIS = 1_000L; |
||||||
|
|
||||||
|
private final String body; |
||||||
|
private final String code; |
||||||
|
private final String message; |
||||||
|
private final int total; |
||||||
|
|
||||||
|
private LingxingEnvelope(String body, String code, String message, int total) { |
||||||
|
this.body = body; |
||||||
|
this.code = code; |
||||||
|
this.message = message; |
||||||
|
this.total = total; |
||||||
|
} |
||||||
|
|
||||||
|
public static LingxingEnvelope of(HttpResponse resp, JacksonJsonCodec json, |
||||||
|
String operation, String omsStoreId, String requestId) { |
||||||
|
Responses.throwIfNotSuccess(resp, operation); |
||||||
|
|
||||||
|
String body = resp.body(); |
||||||
|
String code = json.stringAt(body, "code"); |
||||||
|
String message = json.stringAt(body, "message"); |
||||||
|
if (message == null) { |
||||||
|
message = json.stringAt(body, "msg"); |
||||||
|
} |
||||||
|
|
||||||
|
ChannelException.Builder b = ChannelException.builder() |
||||||
|
.message("lingxing code=" + code + " msg=" + message + " on " + operation) |
||||||
|
.operation(operation) |
||||||
|
.provider(LingxingConstants.ADAPTER_ID) |
||||||
|
.omsStoreId(omsStoreId) |
||||||
|
.requestId(requestId) |
||||||
|
.platformCode(code) |
||||||
|
.rawResponse(body); |
||||||
|
|
||||||
|
if (code == null) { |
||||||
|
throw new PlatformBusinessException(b); |
||||||
|
} |
||||||
|
if (!"0".equals(code) && !"200".equals(code)) { |
||||||
|
throw failure(code, b); |
||||||
|
} |
||||||
|
return new LingxingEnvelope(body, code, message, parseTotal(body, json)); |
||||||
|
} |
||||||
|
|
||||||
|
private static ChannelException failure(String code, ChannelException.Builder b) { |
||||||
|
if (CODE_RATE_LIMITED.equals(code)) { |
||||||
|
return new RateLimitException(b, DEFAULT_RETRY_AFTER_MILLIS); |
||||||
|
} |
||||||
|
if (CODE_TOKEN_INVALID.equals(code) || CODE_SIGN_ERROR.equals(code) |
||||||
|
|| CODE_SIGN_EXPIRED.equals(code) || CODE_IP_DENIED.equals(code)) { |
||||||
|
return new AuthException(b); |
||||||
|
} |
||||||
|
return new PlatformBusinessException(b); |
||||||
|
} |
||||||
|
|
||||||
|
/** 顶层 {@code total} 优先,其次 {@code data.total};都没有返回 -1。 */ |
||||||
|
private static int parseTotal(String body, JacksonJsonCodec json) { |
||||||
|
String raw = json.stringAt(body, "total"); |
||||||
|
if (raw == null) { |
||||||
|
raw = json.stringAt(body, "data.total"); |
||||||
|
} |
||||||
|
if (raw == null) { |
||||||
|
return -1; |
||||||
|
} |
||||||
|
try { |
||||||
|
return Integer.parseInt(raw.trim()); |
||||||
|
} catch (NumberFormatException e) { |
||||||
|
return -1; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public String body() { |
||||||
|
return body; |
||||||
|
} |
||||||
|
|
||||||
|
public String code() { |
||||||
|
return code; |
||||||
|
} |
||||||
|
|
||||||
|
public String message() { |
||||||
|
return message; |
||||||
|
} |
||||||
|
|
||||||
|
/** 总数;响应里没有 total 时为 -1。 */ |
||||||
|
public int total() { |
||||||
|
return total; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,64 @@ |
|||||||
|
package common.ecommerce.lingxing; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.function.LongSupplier; |
||||||
|
|
||||||
|
/** |
||||||
|
* 按接口路径的令牌桶。 |
||||||
|
* |
||||||
|
* <p>领星各接口容量不同:亚马逊 / VC / Walmart 是 1 req/s,多平台 / 结算是 10 req/s, |
||||||
|
* 统一按 10 会直接把亚马逊接口打成 {@code 3001008}。</p> |
||||||
|
* |
||||||
|
* <p>进程内实现,多实例部署会叠加;需要跨实例限流时由宿主替换。</p> |
||||||
|
*/ |
||||||
|
final class LingxingRateLimiter { |
||||||
|
|
||||||
|
/** 便于测试注入,生产用 {@link Thread#sleep}。 */ |
||||||
|
interface Sleeper { |
||||||
|
void sleep(long millis, int nanos); |
||||||
|
} |
||||||
|
|
||||||
|
private final Map<String, Long> nextAllowed = new HashMap<String, Long>(); |
||||||
|
private final Sleeper sleeper; |
||||||
|
private final LongSupplier nanoClock; |
||||||
|
|
||||||
|
LingxingRateLimiter() { |
||||||
|
this(new Sleeper() { |
||||||
|
@Override |
||||||
|
public void sleep(long millis, int nanos) { |
||||||
|
try { |
||||||
|
Thread.sleep(millis, nanos); |
||||||
|
} catch (InterruptedException e) { |
||||||
|
Thread.currentThread().interrupt(); |
||||||
|
} |
||||||
|
} |
||||||
|
}, new LongSupplier() { |
||||||
|
@Override |
||||||
|
public long getAsLong() { |
||||||
|
return System.nanoTime(); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
LingxingRateLimiter(Sleeper sleeper, LongSupplier nanoClock) { |
||||||
|
this.sleeper = sleeper; |
||||||
|
this.nanoClock = nanoClock; |
||||||
|
} |
||||||
|
|
||||||
|
/** 同一路径两次调用之间至少间隔 {@code 1e9 / permitsPerSecond} 纳秒。 */ |
||||||
|
synchronized void acquire(String path, int permitsPerSecond) { |
||||||
|
if (permitsPerSecond <= 0) { |
||||||
|
return; |
||||||
|
} |
||||||
|
long interval = 1_000_000_000L / permitsPerSecond; |
||||||
|
long now = nanoClock.getAsLong(); |
||||||
|
Long next = nextAllowed.get(path); |
||||||
|
if (next != null && now < next) { |
||||||
|
long wait = next - now; |
||||||
|
sleeper.sleep(wait / 1_000_000L, (int) (wait % 1_000_000L)); |
||||||
|
now = nanoClock.getAsLong(); |
||||||
|
} |
||||||
|
nextAllowed.put(path, now + interval); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,95 @@ |
|||||||
|
package common.ecommerce.lingxing; |
||||||
|
|
||||||
|
import common.ecommerce.http.HttpRequest; |
||||||
|
import common.ecommerce.http.JacksonJsonCodec; |
||||||
|
import common.ecommerce.sdk.ChannelContext; |
||||||
|
import common.ecommerce.spi.AccessToken; |
||||||
|
import common.ecommerce.spi.ChannelSecret; |
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException; |
||||||
|
import java.net.URLEncoder; |
||||||
|
import java.time.Clock; |
||||||
|
import java.time.Instant; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.LinkedHashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* 构造已签名的领星请求。 |
||||||
|
* |
||||||
|
* <p>签名范围是「业务参数 + {@code access_token} + {@code app_key} + {@code timestamp}」。 |
||||||
|
* 集合参数先序列化成 JSON 字符串参与签名,请求体里仍然是数组 |
||||||
|
* (与官方 SDK {@code OrderListDemo} 的做法一致)。</p> |
||||||
|
* |
||||||
|
* <p>{@code access_token} 只放 {@code Authorization} 头、不进 query,但必须参与签名。</p> |
||||||
|
*/ |
||||||
|
final class LingxingRequestFactory { |
||||||
|
|
||||||
|
private final String baseUrl; |
||||||
|
private final JacksonJsonCodec json; |
||||||
|
private final Clock clock; |
||||||
|
|
||||||
|
LingxingRequestFactory(String baseUrl, JacksonJsonCodec json, Clock clock) { |
||||||
|
this.baseUrl = baseUrl; |
||||||
|
this.json = json; |
||||||
|
this.clock = clock; |
||||||
|
} |
||||||
|
|
||||||
|
HttpRequest post(String path, Map<String, Object> body, ChannelSecret secret, |
||||||
|
AccessToken token, ChannelContext ctx, String operation) { |
||||||
|
Map<String, Object> payload = withoutNulls(body); |
||||||
|
|
||||||
|
Map<String, String> signParams = new LinkedHashMap<String, String>(); |
||||||
|
for (Map.Entry<String, Object> e : payload.entrySet()) { |
||||||
|
signParams.put(e.getKey(), stringify(e.getValue())); |
||||||
|
} |
||||||
|
signParams.put("access_token", token.value()); |
||||||
|
signParams.put("app_key", secret.appKey()); |
||||||
|
String timestamp = String.valueOf(Instant.now(clock).getEpochSecond()); |
||||||
|
signParams.put("timestamp", timestamp); |
||||||
|
|
||||||
|
String sign = LingxingSigner.sign(signParams, secret); |
||||||
|
|
||||||
|
String url = baseUrl + path |
||||||
|
+ "?app_key=" + urlEncode(secret.appKey()) |
||||||
|
+ "×tamp=" + timestamp |
||||||
|
+ "&sign=" + urlEncode(sign); |
||||||
|
|
||||||
|
return HttpRequest.builder() |
||||||
|
.method("POST") |
||||||
|
.url(url) |
||||||
|
.contentType("application/json") |
||||||
|
.header("Authorization", token.tokenType() + " " + token.value()) |
||||||
|
.header("X-Request-Id", ctx.requestId()) |
||||||
|
.body(json.toJson(payload)) |
||||||
|
.operation(operation) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
/** 集合按 JSON 参与签名;其余按字符串。 */ |
||||||
|
private String stringify(Object value) { |
||||||
|
if (value instanceof Collection || value instanceof Map) { |
||||||
|
return json.toJson(value); |
||||||
|
} |
||||||
|
return String.valueOf(value); |
||||||
|
} |
||||||
|
|
||||||
|
/** 值为 null 的字段既不签名也不发送,避免两端对「null 算不算参数」理解不一致。 */ |
||||||
|
private static Map<String, Object> withoutNulls(Map<String, Object> body) { |
||||||
|
Map<String, Object> out = new LinkedHashMap<String, Object>(); |
||||||
|
for (Map.Entry<String, Object> e : body.entrySet()) { |
||||||
|
if (e.getValue() != null) { |
||||||
|
out.put(e.getKey(), e.getValue()); |
||||||
|
} |
||||||
|
} |
||||||
|
return out; |
||||||
|
} |
||||||
|
|
||||||
|
private static String urlEncode(String v) { |
||||||
|
try { |
||||||
|
return URLEncoder.encode(v == null ? "" : v, "UTF-8"); |
||||||
|
} catch (UnsupportedEncodingException e) { |
||||||
|
throw new IllegalStateException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,67 @@ |
|||||||
|
package common.ecommerce.lingxing; |
||||||
|
|
||||||
|
import common.ecommerce.error.AuthException; |
||||||
|
import common.ecommerce.error.PlatformBusinessException; |
||||||
|
import common.ecommerce.error.RateLimitException; |
||||||
|
import common.ecommerce.http.HttpResponse; |
||||||
|
import common.ecommerce.http.JacksonJsonCodec; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import java.util.Collections; |
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||||
|
|
||||||
|
class LingxingEnvelopeTest { |
||||||
|
|
||||||
|
private final JacksonJsonCodec json = new JacksonJsonCodec(); |
||||||
|
|
||||||
|
private LingxingEnvelope parse(String body) { |
||||||
|
return LingxingEnvelope.of( |
||||||
|
HttpResponse.of(200, Collections.<String, String>emptyMap(), body, 1L), |
||||||
|
json, "salesOrders.pull", "oms-1", "req-1"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void acceptsZeroCode() { |
||||||
|
LingxingEnvelope e = parse("{\"code\":0,\"message\":\"success\",\"data\":{\"total\":3,\"list\":[]}}"); |
||||||
|
assertEquals("0", e.code()); |
||||||
|
assertEquals(3, e.total()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void acceptsTwoHundredCode() { |
||||||
|
LingxingEnvelope e = parse("{\"code\":\"200\",\"msg\":\"OK\",\"data\":{}}"); |
||||||
|
assertEquals("200", e.code()); |
||||||
|
assertEquals(-1, e.total()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void rateLimitCodeBecomesRateLimitException() { |
||||||
|
assertThrows(RateLimitException.class, |
||||||
|
() -> parse("{\"code\":3001008,\"msg\":\"too many requests\"}")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void signErrorBecomesAuthException() { |
||||||
|
assertThrows(AuthException.class, |
||||||
|
() -> parse("{\"code\":2001006,\"msg\":\"sign not correct\"}")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void unknownCodeBecomesBusinessException() { |
||||||
|
PlatformBusinessException e = assertThrows(PlatformBusinessException.class, |
||||||
|
() -> parse("{\"code\":3001001,\"msg\":\"bad param\"}")); |
||||||
|
assertEquals("3001001", e.platformCode()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void totalFallsBackToDataTotal() { |
||||||
|
assertEquals(7, parse("{\"code\":0,\"data\":{\"total\":\"7\",\"list\":[]}}").total()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void topLevelTotalWins() { |
||||||
|
assertEquals(9, parse("{\"code\":0,\"total\":9,\"data\":{\"total\":7}}").total()); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,66 @@ |
|||||||
|
package common.ecommerce.lingxing; |
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||||
|
|
||||||
|
class LingxingRateLimiterTest { |
||||||
|
|
||||||
|
/** 可推进的纳秒时钟,sleep 时同步前进。 */ |
||||||
|
private static final class FakeNano { |
||||||
|
private long nanos; |
||||||
|
|
||||||
|
long now() { |
||||||
|
return nanos; |
||||||
|
} |
||||||
|
|
||||||
|
void advance(long delta) { |
||||||
|
nanos += delta; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private final FakeNano clock = new FakeNano(); |
||||||
|
private final List<Long> sleeps = new ArrayList<Long>(); |
||||||
|
private final LingxingRateLimiter limiter = new LingxingRateLimiter( |
||||||
|
(millis, nanos) -> { |
||||||
|
long total = millis * 1_000_000L + nanos; |
||||||
|
sleeps.add(total); |
||||||
|
clock.advance(total); |
||||||
|
}, |
||||||
|
clock::now); |
||||||
|
|
||||||
|
@Test |
||||||
|
void firstCallDoesNotSleep() { |
||||||
|
limiter.acquire("/a", 1); |
||||||
|
assertTrue(sleeps.isEmpty()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void onePerSecondWaitsOneSecond() { |
||||||
|
limiter.acquire("/a", 1); |
||||||
|
limiter.acquire("/a", 1); |
||||||
|
|
||||||
|
assertEquals(1, sleeps.size()); |
||||||
|
assertEquals(1_000_000_000L, sleeps.get(0).longValue()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void tenPerSecondWaitsOneHundredMillis() { |
||||||
|
limiter.acquire("/a", 10); |
||||||
|
limiter.acquire("/a", 10); |
||||||
|
|
||||||
|
assertEquals(1, sleeps.size()); |
||||||
|
assertEquals(100_000_000L, sleeps.get(0).longValue()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void differentPathsHaveIndependentBuckets() { |
||||||
|
limiter.acquire("/a", 1); |
||||||
|
limiter.acquire("/b", 1); |
||||||
|
assertTrue(sleeps.isEmpty()); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,73 @@ |
|||||||
|
package common.ecommerce.lingxing; |
||||||
|
|
||||||
|
import common.ecommerce.http.HttpRequest; |
||||||
|
import common.ecommerce.http.JacksonJsonCodec; |
||||||
|
import common.ecommerce.model.ChannelProvider; |
||||||
|
import common.ecommerce.model.Platform; |
||||||
|
import common.ecommerce.model.account.AccountKey; |
||||||
|
import common.ecommerce.model.account.ChannelAccount; |
||||||
|
import common.ecommerce.sdk.ChannelContext; |
||||||
|
import common.ecommerce.spi.AccessToken; |
||||||
|
import common.ecommerce.spi.ChannelSecret; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import java.net.URLEncoder; |
||||||
|
import java.time.Clock; |
||||||
|
import java.time.Instant; |
||||||
|
import java.time.ZoneOffset; |
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.LinkedHashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||||
|
|
||||||
|
class LingxingRequestFactoryTest { |
||||||
|
|
||||||
|
private static final String APP_ID = "ak_1234567890123"; |
||||||
|
private static final String TOKEN = "805181e7-4365-468c-bf5c-8c0762a1d358"; |
||||||
|
|
||||||
|
@Test |
||||||
|
void signsAccessTokenAndBusinessParamsAndEncodesSign() throws Exception { |
||||||
|
Clock clock = Clock.fixed(Instant.ofEpochSecond(1760000000L), ZoneOffset.UTC); |
||||||
|
ChannelSecret secret = ChannelSecret.builder().appKey(APP_ID).appSecret("not-used").build(); |
||||||
|
AccessToken token = AccessToken.builder() |
||||||
|
.value(TOKEN) |
||||||
|
.tokenType("Bearer") |
||||||
|
.expiresAt(Instant.ofEpochSecond(1760007200L)) |
||||||
|
.build(); |
||||||
|
|
||||||
|
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||||
|
body.put("offset", 0); |
||||||
|
body.put("length", 200); |
||||||
|
body.put("platform_code", Arrays.asList(10008, 10011)); |
||||||
|
|
||||||
|
ChannelAccount account = ChannelAccount.builder() |
||||||
|
.key(AccountKey.of(ChannelProvider.LINGXING, Platform.WALMART_SC, "oms-1")) |
||||||
|
.externalStoreId("1108413237731") |
||||||
|
.platformCode(10008) |
||||||
|
.credentialRef("ref") |
||||||
|
.marketplaceZone("America/Los_Angeles") |
||||||
|
.currency("USD") |
||||||
|
.build(); |
||||||
|
ChannelContext ctx = ChannelContext.of(account, "req-1", clock); |
||||||
|
|
||||||
|
HttpRequest req = new LingxingRequestFactory("https://openapi.lingxing.com", |
||||||
|
new JacksonJsonCodec(), clock) |
||||||
|
.post("/pb/mp/order/v2/list", body, secret, token, ctx, "salesOrders.pull"); |
||||||
|
|
||||||
|
Map<String, String> signParams = new LinkedHashMap<String, String>(); |
||||||
|
signParams.put("offset", "0"); |
||||||
|
signParams.put("length", "200"); |
||||||
|
signParams.put("platform_code", "[10008,10011]"); |
||||||
|
signParams.put("access_token", TOKEN); |
||||||
|
signParams.put("app_key", APP_ID); |
||||||
|
signParams.put("timestamp", "1760000000"); |
||||||
|
String sign = LingxingSigner.sign(signParams, APP_ID); |
||||||
|
|
||||||
|
assertTrue(req.url().endsWith("&sign=" + URLEncoder.encode(sign, "UTF-8")), req.url()); |
||||||
|
assertEquals("Bearer " + TOKEN, req.headers().get("Authorization")); |
||||||
|
assertEquals("req-1", req.headers().get("X-Request-Id")); |
||||||
|
assertEquals("{\"offset\":0,\"length\":200,\"platform_code\":[10008,10011]}", req.body()); |
||||||
|
} |
||||||
|
} |
||||||
@ -1,54 +1,51 @@ |
|||||||
package common.ecommerce.lingxing; |
package common.ecommerce.lingxing; |
||||||
|
|
||||||
import common.ecommerce.http.JsonCodec; |
|
||||||
import org.junit.jupiter.api.Test; |
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
import java.util.LinkedHashMap; |
import java.util.LinkedHashMap; |
||||||
import java.util.Map; |
import java.util.Map; |
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||||
import static org.junit.jupiter.api.Assertions.assertNotEquals; |
|
||||||
|
|
||||||
class LingxingSignerTest { |
class LingxingSignerTest { |
||||||
|
|
||||||
@Test |
private static final String APP_ID = "ak_1234567890123"; |
||||||
void signatureIsOrderIndependent() { |
|
||||||
Map<String, String> a = new LinkedHashMap<String, String>(); |
|
||||||
a.put("app_key", "k1"); |
|
||||||
a.put("timestamp", "1793001600"); |
|
||||||
|
|
||||||
Map<String, String> b = new LinkedHashMap<String, String>(); |
|
||||||
b.put("timestamp", "1793001600"); |
|
||||||
b.put("app_key", "k1"); |
|
||||||
|
|
||||||
assertEquals(LingxingSigner.sign(a, "secret"), LingxingSigner.sign(b, "secret")); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
@Test |
||||||
void signatureIs32LowercaseHex() { |
void canonicalIsSortedAndTrimmed() { |
||||||
Map<String, String> p = new LinkedHashMap<String, String>(); |
Map<String, String> p = new LinkedHashMap<String, String>(); |
||||||
p.put("app_key", "k1"); |
p.put("timestamp", "1760000000"); |
||||||
p.put("timestamp", "1793001600"); |
p.put("app_key", APP_ID); |
||||||
String sign = LingxingSigner.sign(p, "secret"); |
p.put("offset", " 0 "); |
||||||
assertEquals(32, sign.length()); |
assertEquals("app_key=" + APP_ID + "&offset=0×tamp=1760000000", |
||||||
assertEquals(sign.toLowerCase(), sign); |
LingxingSigner.canonical(p)); |
||||||
assertEquals(true, sign.matches("[0-9a-f]{32}")); |
|
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
void differentSecretDifferentSignature() { |
void nullValuesAreSkipped() { |
||||||
Map<String, String> p = new LinkedHashMap<String, String>(); |
Map<String, String> p = new LinkedHashMap<String, String>(); |
||||||
p.put("app_key", "k1"); |
p.put("b", null); |
||||||
assertNotEquals(LingxingSigner.sign(p, "a"), LingxingSigner.sign(p, "b")); |
p.put("a", "1"); |
||||||
|
assertEquals("a=1", LingxingSigner.canonical(p)); |
||||||
} |
} |
||||||
|
|
||||||
|
/** 官方固定向量;MD5 与最终 sign 均已离线复算确认。 */ |
||||||
@Test |
@Test |
||||||
void knownVector() { |
void matchesOfficialVector() { |
||||||
// 固定向量:appSecret + "app_key" + "k1" + "timestamp" + "1793001600" + appSecret
|
|
||||||
Map<String, String> p = new LinkedHashMap<String, String>(); |
Map<String, String> p = new LinkedHashMap<String, String>(); |
||||||
p.put("app_key", "k1"); |
p.put("timestamp", "1760000000"); |
||||||
p.put("timestamp", "1793001600"); |
p.put("access_token", "805181e7-4365-468c-bf5c-8c0762a1d358"); |
||||||
assertEquals(LingxingSigner.md5Hex("secretapp_keyk1timestamp1793001600secret"), |
p.put("app_key", APP_ID); |
||||||
LingxingSigner.sign(p, "secret")); |
p.put("offset", "0"); |
||||||
|
p.put("length", "200"); |
||||||
|
p.put("platform_code", "[10008,10011]"); |
||||||
|
|
||||||
|
String expected = "access_token=805181e7-4365-468c-bf5c-8c0762a1d358" |
||||||
|
+ "&app_key=" + APP_ID |
||||||
|
+ "&length=200&offset=0&platform_code=[10008,10011]×tamp=1760000000"; |
||||||
|
assertEquals(expected, LingxingSigner.canonical(p)); |
||||||
|
assertEquals("7B96947C5EBDC4A48600F13476982A25", LingxingSigner.md5HexUpper(expected)); |
||||||
|
assertEquals("dt+kVetT8x7alI8wS1ClLRLxLX/bs9TKjyBUZL5zde8jNWg6lba39sPCfqtfAdVT", |
||||||
|
LingxingSigner.sign(p, APP_ID)); |
||||||
} |
} |
||||||
} |
} |
||||||
Loading…
Reference in new issue