
com.trimble.id.ClientCredentialsGrantTokenProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trimble-id Show documentation
Show all versions of trimble-id Show documentation
Trimble Identity OAuth Client library holds the client classes that are used for communicating with Trimble Identity Service
The newest version!
package com.trimble.id;
import static com.trimble.id.AuthenticationConstants.CLIENT_CREDENTIALS;
import static com.trimble.id.AuthenticationConstants.GRANT_TYPE;
import static com.trimble.id.AuthenticationConstants.SCOPE;
import static com.trimble.id.AuthenticationConstants.SDK_VARIANT;
import static com.trimble.id.AuthenticationConstants.SDK_VERSION;
import static com.trimble.id.AuthenticationConstants.SPACE;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import com.trimble.id.analytics.AnalyticsHttpClient;
/** A token provider based on the OAuth Client Credential grant type */
public class ClientCredentialsGrantTokenProvider implements ITokenProvider {
private IHttpTokenClient httpTokenClient;
private String clientId;
private String clientSecret;
private List scopes;
private String accessToken;
private Long tokenExpiry;
/**
* Public constructor of ClientCredentialsGrantTokenProvider
*
* @param endpointProvider The endpoint provider that will be used to get the
* OAuth endpoints
* @param clientId The client id of the application
* @param clientSecret The client secret of the application
*/
public ClientCredentialsGrantTokenProvider(IEndpointProvider endpointProvider, String clientId,
String clientSecret) {
this.httpTokenClient = new HttpTokenClient(endpointProvider);
this.clientId = clientId;
this.clientSecret = clientSecret;
this.scopes = new ArrayList();
AnalyticsHttpClient.sendInitEvent(this.getClass().getSimpleName(), this.getClass().getPackage().getName(),
SDK_VERSION, clientId);
}
/**
* Fluent extension to add scopes to the token provider
*
* @param scopes The scopes to be added
* @return The token provider with the scopes added
*/
public ClientCredentialsGrantTokenProvider withScopes(String[] scopes) {
this.scopes.addAll(Arrays.asList(scopes));
return this;
}
/**
* Get the access token
*
* @return A CompletableFuture that contains the access token
*/
@Override
public CompletableFuture getAccessToken() throws SDKClientException {
AnalyticsHttpClient.sendMethodEvent("getAccessToken", SDK_VARIANT, this.getClass().getPackage().getName(),
SDK_VERSION, clientId);
long timeStampWithBuffer = new Date().getTime() + TimeUnit.MINUTES.toMillis(5);
if (this.accessToken == null
|| (this.getTokenExpiry() != null && this.getTokenExpiry() < timeStampWithBuffer)) {
List nameValuePairs = new ArrayList();
nameValuePairs.add(new NameValuePair(GRANT_TYPE, CLIENT_CREDENTIALS));
try {
nameValuePairs.add(new NameValuePair(SCOPE, URLEncoder.encode(String.join(SPACE, this.scopes), StandardCharsets.UTF_8.name())));
} catch (Exception e) {
throw new SDKClientException(e, e.getCause(), e.getMessage());
}
return this.httpTokenClient
.getOauthTokens(new TokenRequest(this.clientId, this.clientSecret, nameValuePairs))
.thenApply((tokenResponse) -> {
if (tokenResponse.error_description != null) {
AnalyticsHttpClient.sendExceptionEvent("getAccessToken", SDK_VARIANT,
this.getClass().getPackage().getName(), SDK_VERSION,
tokenResponse.error_description, clientId);
throw new SDKClientException(tokenResponse.error, tokenResponse.error_description);
}
setAccessToken(tokenResponse.accessToken);
setTokenExpiry(Long.valueOf(new Date().getTime()) + (tokenResponse.expiresIn * 1000));
return this.accessToken;
});
}
return CompletableFuture.completedFuture(this.accessToken);
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
/**
* Get the token expiry
*
* @return The token expiry
*/
public Long getTokenExpiry() {
return this.tokenExpiry;
}
public void setTokenExpiry(Long tokenExpiry) {
this.tokenExpiry = tokenExpiry;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy