All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vrap.rmf.base.client.http.OAuthHandler Maven / Gradle / Ivy

There is a newer version: 17.17.0
Show newest version

package io.vrap.rmf.base.client.http;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;

import com.spotify.futures.CompletableFutures;

import io.vrap.rmf.base.client.AuthenticationToken;
import io.vrap.rmf.base.client.AutoCloseableService;
import io.vrap.rmf.base.client.oauth2.InMemoryTokenSupplier;
import io.vrap.rmf.base.client.oauth2.RefreshableTokenSupplier;
import io.vrap.rmf.base.client.oauth2.TokenSupplier;
import io.vrap.rmf.base.client.utils.ClientUtils;

/**
 * Handler for retrieving an oauth authentication token
 */
public class OAuthHandler extends AutoCloseableService {
    public static final Duration WAIT_TIMEOUT = Duration.ofSeconds(5);
    private final Duration waitTimeout;
    private final RefreshableTokenSupplier supplier;

    public OAuthHandler(final TokenSupplier supplier) {
        this(supplier, WAIT_TIMEOUT);
    }

    public OAuthHandler(final TokenSupplier supplier, Duration waitTimeout) {
        if (supplier instanceof RefreshableTokenSupplier) {
            this.supplier = (RefreshableTokenSupplier) supplier;
        }
        else {
            this.supplier = new InMemoryTokenSupplier(supplier);
        }
        this.waitTimeout = waitTimeout;
    }

    static String authHeader(final AuthenticationToken token) {
        return "Bearer " + token.getAccessToken();
    }

    public AuthenticationToken getToken() {
        return ClientUtils.blockingWait(getTokenAsync(), waitTimeout);
    }

    public AuthenticationToken refreshToken() {
        return ClientUtils.blockingWait(refreshTokenAsync(), waitTimeout);
    }

    public CompletableFuture getTokenAsync() {
        return CompletableFutures.exceptionallyCompose(supplier.getToken().thenCompose(token -> {
            if (token.isExpired()) {
                return refreshTokenAsync();
            }
            return CompletableFuture.completedFuture(token);
        }), throwable -> refreshTokenAsync()).toCompletableFuture();
    }

    public CompletableFuture refreshTokenAsync() {
        return supplier.refreshToken();
    }

    @Override
    protected void internalClose() {
        if (supplier instanceof AutoCloseable)
            closeQuietly((AutoCloseable) supplier);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy