protected static String accessToken() { final SphereAccessTokenSupplier sphereAccessTokenSupplier = SphereAccessTokenSupplier.ofOneTimeFetchingToken(getSphereClientConfig(), newHttpClient(), true); final String accessToken = sphereAccessTokenSupplier.get().toCompletableFuture().join(); sphereAccessTokenSupplier.close(); return accessToken; }
/** * 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 just returns a fixed token, so the client is usable * for the live time of this token. * * @param token the token which will be passed to the client * @return token service */ public SphereAccessTokenSupplier createSupplierOfFixedToken(final String token) { return SphereAccessTokenSupplier.ofConstantToken(token); }
@Override public SphereAccessTokenSupplier get() { final SphereAccessTokenSupplier sphereAccessTokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(sphereClientConfig, httpClient, false); applicationLifecycle.addStopHook(() -> { sphereAccessTokenSupplier.close(); return completedFuture(null); }); return sphereAccessTokenSupplier; } }
@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); } } } }
private CompletableFuture<String> fetchToken() { final CompletableFuture<String> result = delegate.get(); if (shouldCloseAutomatically) { result.whenComplete((a, b) -> close()); } return result; }
@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 httpRequestsContainUserAgents() throws Exception { final SphereAuthConfig authConfig = SphereAuthConfig.of("a", "b", "c"); final RecordHttpClient httpClient = new RecordHttpClient(); SphereAccessTokenSupplier.ofOneTimeFetchingToken(authConfig, httpClient, true).get(); final String expectedValue = BuildInfo.userAgent(); final String header = httpClient.request.getHeaders().findFlatHeader(HttpHeaders.USER_AGENT).orElse(""); assertThat(header).matches("^(?<sdkOrClient>[^\\s\\/]+)(\\/(?<sdkOrClientVersion>\\S+))?(\\s+\\((?<sdkOrClientInfo>[^(]*)\\))?\\s+(?<environment>[^\\s\\/]+)(\\/(?<environmentVersion>\\S+))?(\\s+\\((?<environmentInfo>[^(]*)\\))?\\s+(?<solution>[^\\s\\/]+)(\\/(?<solutionVersion>\\S+))?(\\s+\\((?<solutionInfo>[^(]*)\\))?$").contains(expectedValue); }
private void checkInvalidCredentialForAuthTokenSupplier(final Function<SphereAuthConfig, SphereAccessTokenSupplier> authTokenSupplierSupplier) { final SphereAuthConfig config = SphereAuthConfig.of(getSphereClientConfig().getProjectKey(), getSphereClientConfig().getClientId(), "wrong-password", getSphereClientConfig().getAuthUrl()); final SphereAccessTokenSupplier authTokenSupplier = authTokenSupplierSupplier.apply(config); assertThatThrownBy(() -> authTokenSupplier.get().toCompletableFuture().join()) .hasCauseInstanceOf(InvalidClientCredentialsException.class); final Throwable throwable = catchThrowable(() -> authTokenSupplier.get().toCompletableFuture().get(20, TimeUnit.MILLISECONDS)); assertThat(throwable) .as("further requests get also an exception and do not hang") .isInstanceOfAny(IllegalStateException.class, ExecutionException.class); authTokenSupplier.close(); } }
@Override protected synchronized void internalClose() { if (shouldCloseAutomatically && !isClosed) { delegate.close(); isClosed = true; } }
public SphereAccessTokenSupplier createSupplierOfOneTimeFetchingToken(final SphereAuthConfig config) { return SphereAccessTokenSupplier.ofOneTimeFetchingToken(config, createHttpClient(), true); }
@Override public <T> CompletableFuture<T> execute(final SphereRequest<T> sphereRequest) { final CompletableFuture<String> tokenFuture = tokenSupplier.get(); return tokenFuture.thenCompose(token -> execute(sphereRequest, token)); }
@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); }); } } }
@Test public void invalidJson() throws Throwable { final String response = "{invalid}"; final SphereAuthConfig config = SphereClientConfig.of(getSphereClientConfig().getProjectKey(), "foo", "bar"); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofOneTimeFetchingToken(config, new HttpClient() { @Override public CompletionStage<HttpResponse> execute(final HttpRequest httpRequest) { return CompletableFuture.completedFuture(HttpResponse.of(200, response)); } @Override public void close() { } }, true); assertThat(tokenSupplier.get().toCompletableFuture()) .hasFailed() .hasFailedWithThrowableThat() .isInstanceOf(JsonException.class) .hasMessageContaining("{invalid}") .hasMessageContaining("http request: HttpRequestImpl[httpMethod=POST,url=https://auth.sphere.io/oauth/token"); }
@Override protected synchronized void internalClose() { if (shouldCloseAutomatically && !isClosed) { delegate.close(); isClosed = true; } }
public SphereAccessTokenSupplier createSupplierOfOneTimeFetchingToken(final SphereAuthConfig config) { return SphereAccessTokenSupplier.ofOneTimeFetchingToken(config, defaultHttpClient(), true); }
protected static String accessToken() { final SphereAccessTokenSupplier sphereAccessTokenSupplier = SphereAccessTokenSupplier.ofOneTimeFetchingToken(getSphereClientConfig(), newHttpClient(), true); final String accessToken = sphereAccessTokenSupplier.get().toCompletableFuture().join(); sphereAccessTokenSupplier.close(); return accessToken; }
/** * Provides a token generator which just returns a fixed token, so the client is usable * for the live time of this token. * * @param token the token which will be passed to the client * @return token service */ public static SphereAccessTokenSupplier createSupplierOfFixedToken(final String token) { return SphereAccessTokenSupplier.ofConstantToken(token); }
/** * 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); }
private CompletionStage<String> fetchToken() { final CompletionStage<String> result = delegate.get(); if (shouldCloseAutomatically) { result.whenComplete((a, b) -> close()); } return result; }