Skip to main content

Introducing Automatic Auth-Token Retrieval

· 2 min read

We are introducing automatic token retrieval and refreshing in the OAuth 2.0 grant types.

Details

SDKs now have automated auth-token retrieval or refreshing feature prior to any API call necessitating OAuth 2.0 Grant Type.

What's New?

Earlier, it was user's responsibility to fetch/refresh the auth-token and update the SDK client with newly fetched auth-token.

Now, after configuring OAuth 2.0 grant type credentials in the SDK client, the SDK handles this automatically before making any API call that requires an auth-token.

note

This feature is currently limited to only Java and TS SDKs using OAuth 2.0 client credentials grant type.

The SDK automatically retrieves or refreshes the auth-token from the auth server if it's expired or undefined by default.

For those wanting a custom auth-token provider, you can register a hook to supply the auth-token from a unique source. Additionally, you can register for the auth-token update event, which triggers whenever the auth-token is updated.

Here's how to set up callbacks in the client for the auth-token provider and auth-token update event.

SDKClient client = new SDKClient.Builder()
.clientCredentialsAuth(new ClientCredentialsAuthModel.Builder(
"OAuthClientId",
"OAuthClientSecret"
)
.oAuthTokenProvider((lastOAuthToken, credentialsManager) -> {
// Add the callback handler to provide a new OAuth token
// It will be triggered whenever the lastOAuthToken is null or expired
OAuthToken token = loadTokenFromDatabase();
if (token == null) {
return credentialsManager.fetchToken();
}
return token;
})
.oAuthOnTokenUpdate(oAuthToken -> {
// Add the callback handler to perform operations like save to DB or file etc.
// It will be triggered whenever the token gets updated
saveTokenToDatabase(oAuthToken);
})
.build())
.build();