diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 9d7628016b9..9a884ec0c00 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -1,5 +1,7 @@ a8ec45c8ea4ba559247b654d01b0d35b21a68865 33f3224cb40e3fa8c56ddb88962e3a4e9319685d 430a1a0a5dd4efe78e21526c37bec9dbce036401 +c4d3b5f96f8dca23235dfd3f75882e01a849b54a +7c713fb498eba102edbd8eb50d5072e16f1b188d d0129c1095216d5c830900c8a6223ef5d4274de1 -4bc5c823b8ebf5a00491c7e63e1ea49d29bf5ee7 +4bc5c823b8ebf5a00491c7e63e1ea49d29bf5ee7 \ No newline at end of file diff --git a/commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/oauth/GlobalCustomerPasswordAuthIntegrationTest.java b/commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/oauth/GlobalCustomerPasswordAuthIntegrationTest.java index d967f10a14c..606b6e0ec6c 100644 --- a/commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/oauth/GlobalCustomerPasswordAuthIntegrationTest.java +++ b/commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/oauth/GlobalCustomerPasswordAuthIntegrationTest.java @@ -3,17 +3,32 @@ import static commercetools.utils.CommercetoolsTestUtils.*; +import java.security.MessageDigest; +import java.time.Duration; +import java.util.Arrays; import java.util.concurrent.ExecutionException; +import com.commercetools.api.client.ApiRoot; +import com.commercetools.api.defaultconfig.ApiRootBuilder; +import com.commercetools.api.defaultconfig.ServiceRegion; import com.commercetools.api.models.customer.Customer; import com.commercetools.api.models.customer.CustomerDraft; import com.commercetools.api.models.customer.CustomerDraftBuilder; +import com.commercetools.api.models.project.Project; import commercetools.customer.CustomerFixtures; import commercetools.utils.CommercetoolsTestUtils; +import io.vrap.rmf.base.client.ApiHttpResponse; +import io.vrap.rmf.base.client.AuthenticationToken; import io.vrap.rmf.base.client.HttpClientSupplier; import io.vrap.rmf.base.client.VrapHttpClient; +import io.vrap.rmf.base.client.http.HttpStatusCode; +import io.vrap.rmf.base.client.http.OAuthHandler; +import io.vrap.rmf.base.client.http.OAuthMiddleware; import io.vrap.rmf.base.client.oauth2.GlobalCustomerPasswordTokenSupplier; +import io.vrap.rmf.base.client.oauth2.InMemoryTokenStorage; +import io.vrap.rmf.base.client.oauth2.RefreshFlowTokenSupplier; +import io.vrap.rmf.base.client.utils.ClientUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -59,6 +74,70 @@ public void execute() { } } + @Test + public void refreshTokenFlow() { + CustomerFixtures.withCustomer(customer -> { + GlobalCustomerPasswordTokenSupplier globalCustomerPasswordTokenSupplier = new GlobalCustomerPasswordTokenSupplier( + getClientId(), getClientSecret(), customer.getEmail(), CustomerFixtures.TEST_CUSTOMER_PASSWORD, null, + ServiceRegion.GCP_EUROPE_WEST1.getPasswordFlowTokenURL(CommercetoolsTestUtils.getProjectKey()), + vrapHttpClient); + + final AuthenticationToken authenticationToken = ClientUtils + .blockingWait(globalCustomerPasswordTokenSupplier.getToken(), Duration.ofSeconds(10)); + + InMemoryTokenStorage tokenStorage = new InMemoryTokenStorage(authenticationToken); + + RefreshFlowTokenSupplier refreshFlowTokenSupplier = new RefreshFlowTokenSupplier(getClientId(), + getClientSecret(), ServiceRegion.GCP_EUROPE_WEST1.getOAuthTokenUrl(), tokenStorage, vrapHttpClient); + AuthenticationToken newAuthenticationToken = ClientUtils + .blockingWait(refreshFlowTokenSupplier.refreshToken(), Duration.ofSeconds(10)); + + Assertions.assertNotEquals(calculateSha256(authenticationToken.getAccessToken()), + calculateSha256(newAuthenticationToken.getAccessToken())); + Assertions.assertTrue(authenticationToken.getExpiresInZonedDateTime() + .isBefore(newAuthenticationToken.getExpiresInZonedDateTime())); + }); + } + + @Test + public void getNewAccessTokenFromApiRoot() { + CustomerFixtures.withCustomer(customer -> { + GlobalCustomerPasswordTokenSupplier globalCustomerPasswordTokenSupplier = new GlobalCustomerPasswordTokenSupplier( + getClientId(), getClientSecret(), customer.getEmail(), CustomerFixtures.TEST_CUSTOMER_PASSWORD, null, + ServiceRegion.GCP_EUROPE_WEST1.getPasswordFlowTokenURL(CommercetoolsTestUtils.getProjectKey()), + vrapHttpClient); + + AuthenticationToken authenticationToken = ClientUtils + .blockingWait(globalCustomerPasswordTokenSupplier.getToken(), Duration.ofSeconds(10)); + String oldAccessToken = authenticationToken.getAccessToken(); + authenticationToken.setExpiresIn(0L); + + InMemoryTokenStorage tokenStorage = new InMemoryTokenStorage(authenticationToken); + + RefreshFlowTokenSupplier refreshFlowTokenSupplier = new RefreshFlowTokenSupplier(getClientId(), + getClientSecret(), ServiceRegion.GCP_EUROPE_WEST1.getOAuthTokenUrl(), tokenStorage, vrapHttpClient); + + OAuthHandler oAuthHandler = new OAuthHandler(refreshFlowTokenSupplier, Duration.ofSeconds(60)); + + ApiRoot apiRoot = ApiRootBuilder.ofEnvironmentVariables() + .withOAuthMiddleware(OAuthMiddleware.of(oAuthHandler)) + .withPolicies(policyBuilder -> policyBuilder.withRetry(builder -> builder.maxRetries(5) + .statusCodes(Arrays.asList(HttpStatusCode.BAD_GATEWAY_502, + HttpStatusCode.SERVICE_UNAVAILABLE_503, HttpStatusCode.GATEWAY_TIMEOUT_504)))) + .build(); + + apiRoot.withProjectKey(getProjectKey()) + .get() + .execute() + .thenApply(ApiHttpResponse::getBody) + .thenApply(Project::getName) + .join(); + + Assertions.assertNotEquals(calculateSha256(oldAccessToken), + calculateSha256(tokenStorage.getToken().getAccessToken())); + }); + } + @Test public void throwExceptionWrongCredentials() { Assertions.assertThrows(ExecutionException.class, () -> { @@ -70,4 +149,23 @@ public void throwExceptionWrongCredentials() { globalCustomerPasswordTokenSupplier.getToken().get(); }); } + + public static String calculateSha256(final String base) { + try { + final MessageDigest digest = MessageDigest.getInstance("SHA-256"); + final byte[] hash = digest.digest(base.getBytes("UTF-8")); + final StringBuilder hexString = new StringBuilder(); + for (int i = 0; i < hash.length; i++) { + final String hex = Integer.toHexString(0xff & hash[i]); + if (hex.length() == 1) + hexString.append('0'); + hexString.append(hex); + } + return hexString.toString(); + } + catch (Exception ex) { + throw new RuntimeException(ex); + } + } + }