/** * Provides a token generator which tries to always provide a valid token. * * @param config the configuration to fetch a token * @return token service */ public SphereAccessTokenSupplier createSupplierOfAutoRefresh(final SphereAuthConfig config) { return SphereAccessTokenSupplier.ofAutoRefresh(config, createHttpClient(), true); }
/** * Provides a token generator which tries to always provide a valid token. * * @param config the configuration to fetch a token * @return token service */ public SphereAccessTokenSupplier createSupplierOfAutoRefresh(final SphereAuthConfig config) { return SphereAccessTokenSupplier.ofAutoRefresh(config, defaultHttpClient(), true); }
@Override public SphereAccessTokenSupplier get() { final SphereAccessTokenSupplier sphereAccessTokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(sphereClientConfig, httpClient, false); applicationLifecycle.addStopHook(() -> { sphereAccessTokenSupplier.close(); return completedFuture(null); }); return sphereAccessTokenSupplier; } }
/** * Creates a standard client with configurable service URLs. Intended for commercetools staff * developing with a custom platform instance. * * @param config configuration for the client * @return client */ default SphereClient createClient(SphereClientConfig config) { final HttpClient httpClient = createHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); return SphereClient.of(config, httpClient, tokenSupplier); }
/** * Creates a standard client with configurable service URLs. Intended for commercetools staff * developing with a custom SPHERE.IO instance. * * @param config configuration for the client * @return client */ public SphereClient createClient(final SphereClientConfig config) { final HttpClient httpClient = createHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); return SphereClient.of(config, httpClient, tokenSupplier); }
/** * Creates a standard client with configurable service URLs. Intended for commercetools staff * developing with a custom platform instance. * * @param config configuration for the client * @return client */ default SphereClient createClient(SphereClientConfig config) { final HttpClient httpClient = createHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); return SphereClient.of(config, httpClient, tokenSupplier); }
/** * Creates a standard client with configurable service URLs. Intended for commercetools staff * developing with a custom SPHERE.IO instance. * * @param config configuration for the client * @return client */ public SphereClient createClient(final SphereClientConfig config) { final HttpClient httpClient = defaultHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); return SphereClient.of(config, defaultHttpClient(), tokenSupplier); }
public static void setupClient() { if (client == null) { final SphereClientConfig config = getSphereClientConfig(); final HttpClient httpClient = newHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); final SphereClient underlying = SphereClient.of(config, httpClient, tokenSupplier); final SphereClient underlying1 = withMaybeDeprecationWarnTool(underlying); client = BlockingSphereClient.of(underlying1, 20, TimeUnit.SECONDS); assertProjectSettingsAreFine(client); } }
public static void setupClient() { if (client == null) { final SphereClientConfig config = getSphereClientConfig(); final HttpClient httpClient = newHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); final SphereClient underlying = SphereClient.of(config, httpClient, tokenSupplier); final SphereClient underlying1 = withMaybeDeprecationWarnTool(underlying); client = BlockingSphereClient.of(underlying1, 30, TimeUnit.SECONDS); assertProjectSettingsAreFine(client); } } //
@Test public void invalidCredentialsToGetTokenForAutoRefresh() throws Throwable { checkInvalidCredentialForAuthTokenSupplier(config -> SphereAccessTokenSupplier.ofAutoRefresh(config, newHttpClient(), true)); }
@Test public void misconfigurationDomainInAuth() throws Exception { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { try(final HttpClient httpClient = newHttpClient()) { final String authUrl = "http://sdfjifdsjifdsjfdsjdfsjidsfjidfs.de"; final String apiUrl = getSphereClientConfig().getApiUrl(); final SphereClientConfig config = SphereClientConfig.of("projectKey", "clientId", "clientSecret", authUrl, apiUrl); try (final SphereAccessTokenSupplier supplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false)) { supplier.close(); assertThatThrownBy(() -> supplier.get()).isInstanceOf(IllegalStateException.class); } } } }
@Test public void retryOnInvalidToken() throws Exception { final RetryInvalidTokenHttpClient httpClient = new RetryInvalidTokenHttpClient(getSphereClientConfig()); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(getSphereClientConfig(), httpClient, false); try(final SphereClient client = SphereClient.of(getSphereClientConfig(), httpClient, tokenSupplier)) { assertThat(httpClient.isTokenValid()).isTrue(); assertThat(client.execute(CategoryByIdGet.of("cat-id")).toCompletableFuture().join()).isNull(); //here is the point where the token expires assertThat(httpClient.isTokenValid()).isFalse(); assertThat(client.execute(ChannelByIdGet.of("channel-id")).toCompletableFuture().join()).isNull(); assertThat(httpClient.isTokenValid()).isTrue(); } }
@Test public void serverProblemsFail() throws Exception { final int failCount = 9000; final String token = "foo"; try(final HttpClient httpClient = newFailingThenSuccessfullHttpClient(failCount, token)) { try (final SphereAccessTokenSupplier supplier = SphereAccessTokenSupplier.ofAutoRefresh(getSphereClientConfig(), httpClient, false)) { final Throwable t = catchThrowable(() -> blockingWait(supplier.get(), 2000, TimeUnit.MILLISECONDS)); assertThat(t).isInstanceOf(SphereException.class).hasCauseInstanceOf(HttpException.class); } } }
@Test public void serverProblemsRecover() throws Exception { final int failCount = 5; final String token = "foo"; try(final HttpClient httpClient = newFailingThenSuccessfullHttpClient(failCount, token)) { try (final SphereAccessTokenSupplier supplier = SphereAccessTokenSupplier.ofAutoRefresh(getSphereClientConfig(), httpClient, false)) { final Throwable t = catchThrowable(() -> blockingWait(supplier.get(), 2000, TimeUnit.MILLISECONDS)); assertThat(t).isInstanceOf(SphereException.class).hasCauseInstanceOf(HttpException.class); assertEventually(() -> { final CompletionStage<String> tokenStage = supplier.get(); assertThat(tokenStage).is(successfullyCompletedWithin(2000, TimeUnit.MILLISECONDS)); assertThat(tokenStage.toCompletableFuture().join()).isEqualTo(token); }); } } }