Skip to content

Commit

Permalink
added support of buyer tokens with different environments
Browse files Browse the repository at this point in the history
  • Loading branch information
xuelianhan007 committed Nov 1, 2024
1 parent ed6dc12 commit 58ee049
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec:
- classpath:/mef-sonata/api-specs/api-spec.quote.yaml
- classpath:/mef-sonata/api-specs/api-spec.product.offering.yaml
- classpath:/mef-sonata/api-targets/api-target.order.uni.add.yaml
- classpath:/mef-sonata/api-targets/api-target.order.eline.add.yam
- classpath:/mef-sonata/api-targets/api-target.order.eline.add.yaml
- classpath:/mef-sonata/api-targets/api-target.order.uni.add.yaml
- classpath:/mef-sonata/api-targets/api-target.order.eline.add.yaml
- classpath:/mef-sonata/api-targets/api-target.order.uni.read.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public OAuth2TokenValidatorResult validate(Jwt token) {
return OAuth2TokenValidatorResult.failure(error);
}
}
return null;
return OAuth2TokenValidatorResult.success();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.consoleconnect.kraken.operator.controller.dto.BuyerAssetDto;
import com.consoleconnect.kraken.operator.controller.dto.CreateBuyerRequest;
import com.consoleconnect.kraken.operator.controller.mapper.BuyerAssetDtoMapper;
import com.consoleconnect.kraken.operator.controller.model.Environment;
import com.consoleconnect.kraken.operator.controller.model.MgmtProperty;
import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.entity.UnifiedAssetEntity;
Expand All @@ -25,6 +26,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import lombok.AllArgsConstructor;
Expand All @@ -50,6 +52,7 @@ public class BuyerService extends AssetStatusManager {
private final UnifiedAssetRepository unifiedAssetRepository;
private final AuthDataProperty.AuthServer authServer;
private final MgmtProperty appProperty;
private final EnvironmentService environmentService;

@Transactional
public BuyerAssetDto create(String productId, CreateBuyerRequest buyerOnboard, String createdBy) {
Expand Down Expand Up @@ -142,23 +145,33 @@ public BuyerAssetDto regenerate(
private BuyerAssetDto generateBuyer(
UnifiedAssetDto unifiedAssetDto, String buyerId, Long tokenExpiredInSeconds) {
BuyerAssetDto buyerAssetDto = BuyerAssetDtoMapper.INSTANCE.toBuyerAssetDto(unifiedAssetDto);
BuyerAssetDto.BuyerToken buyerToken = generateBuyerToken(buyerId, tokenExpiredInSeconds);
Map<String, String> labels = buyerAssetDto.getMetadata().getLabels();
String envId = (labels == null ? "" : labels.getOrDefault(LABEL_ENV_ID, ""));
BuyerAssetDto.BuyerToken buyerToken = generateBuyerToken(buyerId, tokenExpiredInSeconds, envId);

buyerAssetDto.setBuyerToken(buyerToken);
return buyerAssetDto;
}

private BuyerAssetDto.BuyerToken generateBuyerToken(String buyerId, Long tokenExpiredInSeconds) {
private BuyerAssetDto.BuyerToken generateBuyerToken(
String buyerId, Long tokenExpiredInSeconds, String envId) {
if (null == tokenExpiredInSeconds || tokenExpiredInSeconds <= 0) {
tokenExpiredInSeconds =
appProperty.getBuyerTokenExpiredSeconds() == null
? Long.valueOf(MgmtProperty.DEFAULT_TOKEN_EXPIRED_SECONDS)
: Long.valueOf(appProperty.getBuyerTokenExpiredSeconds());
}
BuyerAssetDto.BuyerToken buyerToken = new BuyerAssetDto.BuyerToken();
Map<String, Object> claims = new HashMap<>();
if (StringUtils.isNotBlank(envId)) {
Environment environment = environmentService.findOne(envId);
log.info("generateBuyerToken, envId:{}, envName:{}", envId, environment.getName());
claims.put("env", environment.getName());
}
if (authServer.isEnabled()) {
String token =
JwtEncoderToolkit.get(authServer.getJwt())
.generateToken(buyerId, null, tokenExpiredInSeconds);
.generateToken(buyerId, claims, tokenExpiredInSeconds);
buyerToken.setExpiredAt(
Date.from(ZonedDateTime.now().plusSeconds(tokenExpiredInSeconds).toInstant()));
buyerToken.setAccessToken(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.consoleconnect.kraken.operator.config.TestApplication;
import com.consoleconnect.kraken.operator.controller.dto.BuyerAssetDto;
import com.consoleconnect.kraken.operator.controller.dto.CreateBuyerRequest;
import com.consoleconnect.kraken.operator.controller.model.Environment;
import com.consoleconnect.kraken.operator.controller.service.EnvironmentService;
import com.consoleconnect.kraken.operator.core.dto.Tuple2;
import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.enums.AssetKindEnum;
Expand All @@ -18,6 +20,7 @@
import com.consoleconnect.kraken.operator.test.AbstractIntegrationTest;
import com.consoleconnect.kraken.operator.test.MockIntegrationTest;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.MethodOrderer;
Expand All @@ -36,13 +39,14 @@
@ContextConfiguration(classes = {TestApplication.class})
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@ActiveProfiles("test-rs256")
class BuyerControllerTest extends AbstractIntegrationTest {
class BuyerControllerTest extends AbstractIntegrationTest implements EnvCreator {
private static final String PRODUCT_ID = "product.mef.sonata.api";
public static final String BASE_URL = String.format("/products/%s/buyers", PRODUCT_ID);
public static final String BUYER_ID = "consolecore-poping-company";

private final WebTestClientHelper webTestClient;
@Autowired private UnifiedAssetService unifiedAssetService;
@Getter @Autowired EnvironmentService environmentService;

@Autowired
public BuyerControllerTest(WebTestClient webTestClient) {
Expand All @@ -52,9 +56,10 @@ public BuyerControllerTest(WebTestClient webTestClient) {
@Test
@Order(2)
void givenBuyer_whenCreate_thenOK() {
Environment envStage = createStage(PRODUCT_ID);
CreateBuyerRequest requestEntity = new CreateBuyerRequest();
requestEntity.setBuyerId(BUYER_ID);
requestEntity.setEnvId("stage");
requestEntity.setEnvId(envStage.getId());
requestEntity.setCompanyName("console connect");

String resp =
Expand Down Expand Up @@ -107,7 +112,8 @@ void givenBuyer_whenSearch_thenOK() {
void givenDuplicatedBuyer_whenCreate_thenNot200() {
CreateBuyerRequest requestEntity = new CreateBuyerRequest();
requestEntity.setBuyerId(BUYER_ID);
requestEntity.setEnvId("stage");
Environment envStage = createStage(PRODUCT_ID);
requestEntity.setEnvId(envStage.getId());
requestEntity.setCompanyName("console connect");

webTestClient.requestAndVerify(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -66,7 +67,8 @@ public Mono<Object> internalRun(ServerWebExchange exchange) {
.orElse(Instant.MIN);
Jwt principal = (Jwt) authentication.getPrincipal();
Instant issuedAt = principal.getIssuedAt();
if (issuedAt.isBefore(dbGeneratedAt.minusSeconds(INTERVAL))) {
if (Objects.nonNull(issuedAt)
&& issuedAt.isBefore(dbGeneratedAt.minusSeconds(INTERVAL))) {
sink.error(KrakenException.badRequest("Token expired "));
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,4 @@ links:
group: mef.sonata.api-target.address.validate
- targetAssetKey: mef.sonata.api-target-mapper.address.retrieve
relationship: implementation.target-mapper
group: mef.sonata.api-target.address.retrieve



group: mef.sonata.api-target.address.retrieve

0 comments on commit 58ee049

Please sign in to comment.