@ -1,6 +1,4 @@
@@ -1,6 +1,4 @@
package common.ecommerce.lingxing.auth ;
import common.ecommerce.lingxing.LingxingApi ;
import common.ecommerce.lingxing.LingxingConstants ;
import common.ecommerce.error.AuthException ;
import common.ecommerce.error.ChannelException ;
@ -9,6 +7,8 @@ import common.ecommerce.http.HttpResponse;
@@ -9,6 +7,8 @@ import common.ecommerce.http.HttpResponse;
import common.ecommerce.http.HttpTransport ;
import common.ecommerce.http.JsonCodec ;
import common.ecommerce.http.Responses ;
import common.ecommerce.lingxing.LingxingApi ;
import common.ecommerce.lingxing.LingxingConstants ;
import common.ecommerce.spi.AccessToken ;
import java.time.Clock ;
@ -17,16 +17,16 @@ import java.util.LinkedHashMap;
@@ -17,16 +17,16 @@ import java.util.LinkedHashMap;
import java.util.Map ;
/ * *
* 领星令牌获取 : { @code POST / api / auth - server / oauth / access - token } 。
* 领星令牌接口 。 两个端点 , 按官方文档 ( 2026 - 10 核对 ) :
*
* < p > 按官方文档 ( 2026 - 10 核对 ) : < / p >
* < ul >
* < li > 请求头 { @code Content - Type : multipart / form - data } < / li >
* < li > 表单参数 { @code appId } / { @code appSecret } < / li >
* < li > 响应 { @code code = = "200" } 为成功 , { @code data } 含
* { @code access_token } / { @code refresh_token } / { @code expires_in } ( 秒 ) < / li >
* < li > 失败时 HTTP 仍是 200 , 靠 { @code code } 区分 , { @code data } 为 null < / li >
* < li > < b > 换取 < / b > { @code POST / api / auth - server / oauth / access - token } : 表单 { @code appId } / { @code appSecret } < / li >
* < li > < b > 续约 < / b > { @code POST / api / auth - server / oauth / refresh } : 表单 { @code appId } / { @code refreshToken } ,
* < b > 每个 refresh_token 只能使用一次 < / b > , 响应里会返回新的 refresh_token < / li >
* < / ul >
*
* < p > 两个接口的共同点 : 请求头 { @code Content - Type : multipart / form - data } ;
* 业务成功码为 { @code "200" } ; 失败时 HTTP 仍是 200 , 靠 { @code code } 区分 , { @code data } 为 null 。 < / p >
* /
public final class LingxingTokenClient {
@ -37,6 +37,9 @@ public final class LingxingTokenClient {
@@ -37,6 +37,9 @@ public final class LingxingTokenClient {
private static final String BOUNDARY = "----commonEcommerceLingxingBoundary" ;
private static final String MULTIPART_CONTENT_TYPE = "multipart/form-data; boundary=" + BOUNDARY ;
private static final String OP_FETCH = "token.fetch" ;
private static final String OP_REFRESH = "token.refresh" ;
private final HttpTransport transport ;
private final JsonCodec json ;
private final Clock clock ;
@ -47,29 +50,54 @@ public final class LingxingTokenClient {
@@ -47,29 +50,54 @@ public final class LingxingTokenClient {
this . clock = clock ;
}
/** 端点由调用方每次传入,与其它接口一致。 */
/** 首次换取令牌:appId + appSecret。 端点由调用方每次传入,与其它接口一致。 */
public AccessToken fetch ( String baseUrl , String appId , String appSecret ) {
Map < String , String > form = new LinkedHashMap < String , String > ( ) ;
form . put ( "appId" , appId ) ;
form . put ( "appSecret" , appSecret ) ;
return request ( baseUrl , LingxingApi . TOKEN , form , OP_FETCH ) ;
}
/ * *
* 续约令牌 : appId + refreshToken 。
*
* < p > 注意每个 refresh_token 只能使用一次 — — 调用方必须用响应里的新 refresh_token
* 覆盖旧的 , 否则下次续约会失败 。 < / p >
* /
public AccessToken refresh ( String baseUrl , String appId , String refreshToken ) {
if ( refreshToken = = null | | refreshToken . trim ( ) . isEmpty ( ) ) {
throw new AuthException ( ChannelException . builder ( )
. message ( "refreshToken must not be blank" )
. operation ( OP_REFRESH )
. provider ( LingxingConstants . ADAPTER_ID )
. retryable ( false ) ) ;
}
Map < String , String > form = new LinkedHashMap < String , String > ( ) ;
form . put ( "appId" , appId ) ;
form . put ( "refreshToken" , refreshToken ) ;
return request ( baseUrl , LingxingApi . TOKEN_REFRESH , form , OP_REFRESH ) ;
}
// ---------------------------------------------------------------- internals
private AccessToken request ( String baseUrl , LingxingApi api , Map < String , String > form , String operation ) {
HttpRequest req = HttpRequest . builder ( )
. method ( "POST" )
. url ( trimTrailingSlash ( baseUrl ) + LingxingApi . TOKEN . path ( ) )
. url ( trimTrailingSlash ( baseUrl ) + api . path ( ) )
. contentType ( MULTIPART_CONTENT_TYPE )
. body ( multipartBody ( form ) )
. operation ( "token.refresh" )
. operation ( operation )
. build ( ) ;
HttpResponse resp = transport . execute ( req ) ;
Responses . throwIfNotSuccess ( resp , "token.refresh" ) ;
Responses . throwIfNotSuccess ( resp , operation ) ;
String code = json . stringAt ( resp . body ( ) , "code" ) ;
if ( ! SUCCESS_CODE . equals ( code ) ) {
throw new AuthException ( ChannelException . builder ( )
. message ( "lingxing token rejected: code=" + code
+ ", msg=" + json . stringAt ( resp . body ( ) , "msg" ) )
. operation ( "token.refresh" )
. operation ( operation )
. provider ( LingxingConstants . ADAPTER_ID )
. platformCode ( code )
. rawResponse ( resp . body ( ) ) ) ;
@ -79,7 +107,7 @@ public final class LingxingTokenClient {
@@ -79,7 +107,7 @@ public final class LingxingTokenClient {
if ( token = = null | | token . trim ( ) . isEmpty ( ) ) {
throw new AuthException ( ChannelException . builder ( )
. message ( "lingxing token response has no data.access_token" )
. operation ( "token.refresh" )
. operation ( operation )
. provider ( LingxingConstants . ADAPTER_ID )
. rawResponse ( resp . body ( ) ) ) ;
}
@ -87,13 +115,13 @@ public final class LingxingTokenClient {
@@ -87,13 +115,13 @@ public final class LingxingTokenClient {
return AccessToken . builder ( )
. value ( token )
. refreshToken ( json . stringAt ( resp . body ( ) , "data.refresh_token" ) )
. expiresAt ( Instant . now ( clock ) . plusSeconds ( expiresInSeconds ( resp . body ( ) ) ) )
. expiresAt ( Instant . now ( clock ) . plusSeconds ( expiresInSeconds ( resp . body ( ) , operation ) ) )
. tokenType ( "Bearer" )
. build ( ) ;
}
/** {@code expires_in} 单位为秒;缺失或非法时按文档示例值兜底。 */
private long expiresInSeconds ( String body ) {
private long expiresInSeconds ( String body , String operation ) {
String raw = json . stringAt ( body , "data.expires_in" ) ;
if ( raw = = null | | raw . trim ( ) . isEmpty ( ) ) {
return 7199L ;
@ -103,7 +131,7 @@ public final class LingxingTokenClient {
@@ -103,7 +131,7 @@ public final class LingxingTokenClient {
} catch ( NumberFormatException e ) {
throw new AuthException ( ChannelException . builder ( )
. message ( "lingxing token expires_in is not a number: " + raw )
. operation ( "token.refresh" )
. operation ( operation )
. provider ( LingxingConstants . ADAPTER_ID )
. rawResponse ( body ) ) ;
}
@ -117,7 +145,7 @@ public final class LingxingTokenClient {
@@ -117,7 +145,7 @@ public final class LingxingTokenClient {
return url . substring ( 0 , end ) ;
}
/** multipart/form-data 请求体;字段值不做转义,appId/appSecret 只含安全字符。 */
/** multipart/form-data 请求体;字段值不做转义,appId/appSecret/refreshToken 只含安全字符。 */
private static String multipartBody ( Map < String , String > form ) {
StringBuilder sb = new StringBuilder ( ) ;
for ( Map . Entry < String , String > e : form . entrySet ( ) ) {