public final class TokensFacade extends Base
Modifier and Type | Method and Description |
---|---|
static CompletionStage<String> |
fetchAccessToken(SphereAuthConfig authConfig) |
static CompletionStage<Tokens> |
fetchCustomerPasswordFlowTokens(SphereAuthConfig authConfig,
String email,
String password)
Fetches a new access token using the customer password flow.
|
static CompletionStage<Tokens> |
fetchTokens(SphereAuthConfig authConfig)
Fetches a new access token using the client credentials flow.
|
public static CompletionStage<String> fetchAccessToken(SphereAuthConfig authConfig)
public static CompletionStage<Tokens> fetchTokens(SphereAuthConfig authConfig)
final SphereAuthConfig config = getSphereClientConfig();
final CompletionStage<String> stringCompletionStage = TokensFacade.fetchAccessToken(config);
final String accessToken = blockingWait(stringCompletionStage, 2, SECONDS);
assertThat(accessToken).isNotEmpty();
See the test code.
authConfig
- the Project which the token should belong topublic static CompletionStage<Tokens> fetchCustomerPasswordFlowTokens(SphereAuthConfig authConfig, String email, String password)
withApiClient(client(), singletonList(SphereProjectScope.VIEW_PRODUCTS), apiClient -> {
withCustomer(client(), (Customer customer) -> {
final SphereAuthConfig authConfig = toSphereAuthConfig(getSphereClientConfig(),apiClient);
final String email = customer.getEmail();
// final String pw = customer.getPassword();//won;t work since it is obfusciated
final String pw = CustomerFixtures.PASSWORD;
final Tokens tokens = blockingWait(TokensFacade.
fetchCustomerPasswordFlowTokens(authConfig, email, pw), 2, SECONDS);
final String accessToken = tokens.getAccessToken();
assertThat(accessToken).isNotEmpty();
try (final SphereClient client = SphereClientFactory.of()
.createClient(getSphereClientConfig(), SphereAccessTokenSupplier.ofConstantToken(accessToken))) {
final PagedQueryResult<ProductProjection> customerPagedQueryResult =
blockingWait(client.execute(ProductProjectionQuery.ofCurrent().withLimit(1)), 2, SECONDS);
assertThat(customerPagedQueryResult).isNotNull();
}
});
});
See the test code.
authConfig
- authConfigemail
- emailpassword
- password