Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xezzon committed Dec 8, 2024
1 parent b1d4cac commit 022811f
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 80 deletions.
33 changes: 17 additions & 16 deletions geom-open-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,24 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<description>构造与geom开放平台交互的HTTP接口的SDK</description>
<modelVersion>4.0.0</modelVersion>
<artifactId>geom-open-sdk</artifactId>
<name>${project.groupId}:${project.artifactId}</name>

<packaging>jar</packaging>
<parent>
<artifactId>geom</artifactId>
<groupId>io.github.xezzon</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<artifactId>feign-core</artifactId>
Expand All @@ -29,20 +46,4 @@
<scope>test</scope>
</dependency>
</dependencies>
<description>构造与geom开放平台交互的HTTP接口的SDK</description>

<modelVersion>4.0.0</modelVersion>
<name>${project.groupId}:${project.artifactId}</name>

<parent>
<artifactId>geom</artifactId>
<groupId>io.github.xezzon</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.xezzon.geom;

/**
* @author xezzon
*/
public class GeomOpenConstant {

public static final String DIGEST_ALGORITHM = "HmacSHA256";
public static final String ACCESS_KEY_HEADER = "X-Access-Key";
public static final String TIMESTAMP_HEADER = "X-Timestamp";
public static final String SIGNATURE_HEADER = "X-Signature";

private GeomOpenConstant() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
*/
public class GeomOpenRequestBuilder {

public static final String DIGEST_ALGORITHM = "HmacSHA256";
public static final String ACCESS_KEY_HEADER = "X-Access-Key";
public static final String TIMESTAMP_HEADER = "X-Timestamp";
public static final String SIGNATURE_HEADER = "X-Signature";

static {
Security.addProvider(new BouncyCastleProvider());
}
Expand Down Expand Up @@ -48,18 +43,18 @@ class GeomOpenRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
// 应用标识
requestTemplate.header(ACCESS_KEY_HEADER, accessKey);
requestTemplate.header(GeomOpenConstant.ACCESS_KEY_HEADER, accessKey);
// 时间戳
long timestamp = Instant.now().toEpochMilli();
requestTemplate.header(TIMESTAMP_HEADER, String.valueOf(timestamp));
requestTemplate.header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp));
// 摘要
byte[] body = requestTemplate.body();
try {
Mac mac = Mac.getInstance(DIGEST_ALGORITHM);
mac.init(new SecretKeySpec(secretKey, DIGEST_ALGORITHM));
Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM);
mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM));
mac.update(body);
String signature = Base64.getEncoder().encodeToString(mac.doFinal());
requestTemplate.header(SIGNATURE_HEADER, signature);
requestTemplate.header(GeomOpenConstant.SIGNATURE_HEADER, signature);
} catch (Exception e) {
throw new GeomOpenException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.xezzon.geom;

import static io.github.xezzon.geom.GeomOpenRequestBuilder.DIGEST_ALGORITHM;
import static io.github.xezzon.geom.GeomOpenConstant.DIGEST_ALGORITHM;
import static io.github.xezzon.geom.TestApplication.SECRET_KEY;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -31,9 +31,9 @@ public class TestController {
@PostMapping("/test")
public String test(
@RequestBody byte[] body,
@RequestHeader(GeomOpenRequestBuilder.ACCESS_KEY_HEADER) String accessKey,
@RequestHeader(GeomOpenRequestBuilder.TIMESTAMP_HEADER) Instant timestamp,
@RequestHeader(GeomOpenRequestBuilder.SIGNATURE_HEADER) String signature
@RequestHeader(GeomOpenConstant.ACCESS_KEY_HEADER) String accessKey,
@RequestHeader(GeomOpenConstant.TIMESTAMP_HEADER) Instant timestamp,
@RequestHeader(GeomOpenConstant.SIGNATURE_HEADER) String signature
) throws NoSuchAlgorithmException, InvalidKeyException, IOException {
Assertions.assertEquals("hello", accessKey);
Assertions.assertTrue(timestamp.isBefore(Instant.now()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static com.google.auth.http.AuthHttpConstants.BEARER;

import io.github.xezzon.geom.GeomOpenRequestBuilder;
import io.github.xezzon.geom.GeomOpenConstant;
import jakarta.servlet.http.HttpServletResponse;
import java.time.Instant;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -31,9 +31,9 @@ public SubscriptionCallController(SubscriptionCallService subscriptionCallServic
public void validateCall(
@RequestBody byte[] body,
@RequestParam("path") String path,
@RequestHeader(GeomOpenRequestBuilder.ACCESS_KEY_HEADER) String accessKey,
@RequestHeader(GeomOpenRequestBuilder.TIMESTAMP_HEADER) Instant timestamp,
@RequestHeader(GeomOpenRequestBuilder.SIGNATURE_HEADER) String signature,
@RequestHeader(GeomOpenConstant.ACCESS_KEY_HEADER) String accessKey,
@RequestHeader(GeomOpenConstant.TIMESTAMP_HEADER) Instant timestamp,
@RequestHeader(GeomOpenConstant.SIGNATURE_HEADER) String signature,
HttpServletResponse response
) {
/* 所有校验通过后发放令牌 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ protected void auditSubscription(String id) {
subscriptionDAO.get().save(entity);
}

@Override
public List<Subscription> listSubscriptionsOfApp(String appId) {
return subscriptionDAO.get().findByAppId(appId);
}

@Override
public Page<Subscription> listSubscription(ODataQueryOption odata, String appId) {
thirdPartyAppService.checkPermission(appId);
Expand All @@ -87,9 +92,4 @@ public Page<Subscription> listSubscription(ODataQueryOption odata, String appId)
.toList();
return new PageImpl<>(subscriptions, openapiPage.getPageable(), openapiPage.getTotalElements());
}

@Override
public List<Subscription> listSubscriptionsOfApp(String appId) {
return subscriptionDAO.get().findByAppId(appId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
*/
public interface ISubscriptionService4ThirdPartyApp {

/**
* 获取指定应用ID下订阅的所有OpenAPI列表
* @param appId 应用ID
* @return 返回订阅的所有OpenAPI列表
*/
List<Subscription> listSubscriptionsOfApp(String appId);

/**
* 获取订阅列表
* @param into OData查询选项,用于指定查询条件、排序方式等。
* @param appId 应用程序ID。
* @return 包含订阅信息的分页对象。
*/
Page<Subscription> listSubscription(ODataQueryOption into, String appId);

/**
* 获取指定应用ID下订阅的所有OpenAPI列表
* @param appId 应用ID
* @return 返回订阅的所有OpenAPI列表
*/
List<Subscription> listSubscriptionsOfApp(String appId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.xezzon.geom.third_party_app;

import cn.dev33.satoken.stp.StpUtil;
import io.github.xezzon.geom.GeomOpenRequestBuilder;
import io.github.xezzon.geom.GeomOpenConstant;
import io.github.xezzon.geom.common.exception.DataPermissionForbiddenException;
import io.github.xezzon.geom.common.exception.InvalidAccessKeyException;
import io.github.xezzon.geom.core.odata.ODataQueryOption;
Expand Down Expand Up @@ -95,9 +95,9 @@ public void validateSignature(String appId, byte[] body, String signature) {
AccessSecret accessSecret = accessSecretRepository.findById(appId)
.orElseThrow(InvalidAccessKeyException::new);
try {
Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM);
Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM);
byte[] secretKey = Base64.getDecoder().decode(accessSecret.getSecretKey());
mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM));
mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM));
mac.update(body);
if (!Objects.equals(
signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static io.github.xezzon.geom.common.exception.GlobalExceptionHandler.ERROR_CODE_HEADER;

import cn.hutool.core.util.RandomUtil;
import io.github.xezzon.geom.GeomOpenRequestBuilder;
import io.github.xezzon.geom.GeomOpenConstant;
import io.github.xezzon.geom.auth.JwtAuth;
import io.github.xezzon.geom.auth.JwtClaim;
import io.github.xezzon.geom.common.exception.OpenErrorCode;
Expand Down Expand Up @@ -75,9 +75,9 @@ void validate() throws NoSuchAlgorithmException, InvalidKeyException {
final String rawBody = "{\"id\":\"1234567890\"}";
long timestamp = Instant.now().toEpochMilli();
Tuple3<ThirdPartyApp, Subscription, AccessSecret> dataset = this.initData();
Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM);
Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM);
byte[] secretKey = Base64.getDecoder().decode(dataset.getT3().getSecretKey());
mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM));
mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM));
mac.update(rawBody.getBytes());
String signature = Base64.getEncoder().encodeToString(mac.doFinal());

Expand All @@ -87,9 +87,9 @@ void validate() throws NoSuchAlgorithmException, InvalidKeyException {
.queryParam("path", dataset.getT2().getOpenapiCode())
.build()
)
.header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey())
.header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature)
.header(GeomOpenConstant.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey())
.header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenConstant.SIGNATURE_HEADER, signature)
.bodyValue(rawBody)
.exchange()
.expectStatus().isOk()
Expand All @@ -112,9 +112,9 @@ void validate_notSubscribed() throws NoSuchAlgorithmException, InvalidKeyExcepti
final String rawBody = "{\"id\":\"1234567890\"}";
long timestamp = Instant.now().toEpochMilli();
Tuple3<ThirdPartyApp, Subscription, AccessSecret> dataset = this.initData();
Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM);
Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM);
byte[] secretKey = Base64.getDecoder().decode(dataset.getT3().getSecretKey());
mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM));
mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM));
mac.update(rawBody.getBytes());
String signature = Base64.getEncoder().encodeToString(mac.doFinal());

Expand All @@ -124,9 +124,9 @@ void validate_notSubscribed() throws NoSuchAlgorithmException, InvalidKeyExcepti
.queryParam("path", RandomUtil.randomString(8))
.build()
)
.header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey())
.header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature)
.header(GeomOpenConstant.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey())
.header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenConstant.SIGNATURE_HEADER, signature)
.bodyValue(rawBody)
.exchange()
.expectStatus().isForbidden()
Expand All @@ -138,9 +138,9 @@ void validate_incorrectAccessKey() throws NoSuchAlgorithmException, InvalidKeyEx
final String rawBody = "{\"id\":\"1234567890\"}";
long timestamp = Instant.now().toEpochMilli();
Tuple3<ThirdPartyApp, Subscription, AccessSecret> dataset = this.initData();
Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM);
Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM);
byte[] secretKey = Base64.getDecoder().decode(dataset.getT3().getSecretKey());
mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM));
mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM));
mac.update(rawBody.getBytes());
String signature = Base64.getEncoder().encodeToString(mac.doFinal());

Expand All @@ -150,9 +150,9 @@ void validate_incorrectAccessKey() throws NoSuchAlgorithmException, InvalidKeyEx
.queryParam("path", dataset.getT2().getOpenapiCode())
.build()
)
.header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, RandomUtil.randomString(8))
.header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature)
.header(GeomOpenConstant.ACCESS_KEY_HEADER, RandomUtil.randomString(8))
.header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenConstant.SIGNATURE_HEADER, signature)
.bodyValue(rawBody)
.exchange()
.expectStatus().isForbidden()
Expand All @@ -164,9 +164,9 @@ void validate_incorrectSecretKey() throws NoSuchAlgorithmException, InvalidKeyEx
final String rawBody = "{\"id\":\"1234567890\"}";
long timestamp = Instant.now().toEpochMilli();
Tuple3<ThirdPartyApp, Subscription, AccessSecret> dataset = this.initData();
Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM);
Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM);
byte[] secretKey = Base64.getDecoder().decode(RandomUtil.randomString(8));
mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM));
mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM));
mac.update(rawBody.getBytes());
String signature = Base64.getEncoder().encodeToString(mac.doFinal());

Expand All @@ -176,9 +176,9 @@ void validate_incorrectSecretKey() throws NoSuchAlgorithmException, InvalidKeyEx
.queryParam("path", dataset.getT2().getOpenapiCode())
.build()
)
.header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey())
.header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature)
.header(GeomOpenConstant.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey())
.header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenConstant.SIGNATURE_HEADER, signature)
.bodyValue(rawBody)
.exchange()
.expectStatus().isForbidden()
Expand All @@ -190,9 +190,9 @@ void validate_mismatchSignature() throws NoSuchAlgorithmException, InvalidKeyExc
final String rawBody = "{\"id\":\"1234567890\"}";
long timestamp = Instant.now().toEpochMilli();
Tuple3<ThirdPartyApp, Subscription, AccessSecret> dataset = this.initData();
Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM);
Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM);
byte[] secretKey = Base64.getDecoder().decode(dataset.getT3().getSecretKey());
mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM));
mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM));
mac.update("tampered message".getBytes());
String signature = Base64.getEncoder().encodeToString(mac.doFinal());

Expand All @@ -202,9 +202,9 @@ void validate_mismatchSignature() throws NoSuchAlgorithmException, InvalidKeyExc
.queryParam("path", dataset.getT2().getOpenapiCode())
.build()
)
.header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey())
.header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature)
.header(GeomOpenConstant.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey())
.header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenConstant.SIGNATURE_HEADER, signature)
.bodyValue(rawBody)
.exchange()
.expectStatus().isForbidden()
Expand All @@ -216,9 +216,9 @@ void validate_timeout() throws NoSuchAlgorithmException, InvalidKeyException {
final String rawBody = "{\"id\":\"1234567890\"}";
long timestamp = Instant.now().getEpochSecond();
Tuple3<ThirdPartyApp, Subscription, AccessSecret> dataset = this.initData();
Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM);
Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM);
byte[] secretKey = Base64.getDecoder().decode(dataset.getT3().getSecretKey());
mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM));
mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM));
mac.update(rawBody.getBytes());
String signature = Base64.getEncoder().encodeToString(mac.doFinal());

Expand All @@ -228,9 +228,9 @@ void validate_timeout() throws NoSuchAlgorithmException, InvalidKeyException {
.queryParam("path", dataset.getT2().getOpenapiCode())
.build()
)
.header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey())
.header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature)
.header(GeomOpenConstant.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey())
.header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp))
.header(GeomOpenConstant.SIGNATURE_HEADER, signature)
.bodyValue(rawBody)
.exchange()
.expectStatus().isOk()
Expand Down

0 comments on commit 022811f

Please sign in to comment.