Search in sources :

Example 1 with ResourceOwnerGrant

use of org.apache.cxf.rs.security.oauth2.grants.owner.ResourceOwnerGrant in project camel by apache.

the class OAuthToken method getOrRefreshAccessToken.

private synchronized void getOrRefreshAccessToken() {
    if (token == null) {
        LOGGER.debug("Generate OAuth token");
        token = OAuthClientUtils.getAccessToken(WebClient.create(configuration.getOauthTokenUrl()), new Consumer(configuration.getOauthClientId(), configuration.getOauthClientSecret()), new ResourceOwnerGrant(configuration.getUserName(), configuration.getPassword()), true);
        LOGGER.debug("OAuth token expires in {}s", token.getExpiresIn());
        // Set expiration time related info in milliseconds
        token.setIssuedAt(System.currentTimeMillis());
        token.setExpiresIn(TimeUnit.MILLISECONDS.convert(token.getExpiresIn(), TimeUnit.SECONDS));
        authString = token.toString();
        if (token.getExpiresIn() > 0) {
            expireAt = token.getIssuedAt() + token.getExpiresIn();
        }
    } else if (expireAt > 0 && System.currentTimeMillis() >= expireAt) {
        LOGGER.debug("OAuth token is expired, refresh it");
        token = OAuthClientUtils.refreshAccessToken(WebClient.create(configuration.getOauthTokenUrl()), new Consumer(configuration.getOauthClientId(), configuration.getOauthClientSecret()), token, null, false);
        LOGGER.debug("Refreshed OAuth token expires in {}s", token.getExpiresIn());
        // Set expiration time related info in milliseconds
        token.setIssuedAt(System.currentTimeMillis());
        token.setExpiresIn(TimeUnit.MILLISECONDS.convert(token.getExpiresIn(), TimeUnit.SECONDS));
        authString = token.toString();
        if (token.getExpiresIn() > 0) {
            expireAt = token.getIssuedAt() + token.getExpiresIn();
        }
    }
}
Also used : Consumer(org.apache.cxf.rs.security.oauth2.client.Consumer) ResourceOwnerGrant(org.apache.cxf.rs.security.oauth2.grants.owner.ResourceOwnerGrant)

Aggregations

Consumer (org.apache.cxf.rs.security.oauth2.client.Consumer)1 ResourceOwnerGrant (org.apache.cxf.rs.security.oauth2.grants.owner.ResourceOwnerGrant)1