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

com.github.davidmoten.microsoft.authentication.BearerAuthenticator Maven / Gradle / Ivy

There is a newer version: 0.2.2
Show newest version
package com.github.davidmoten.microsoft.authentication;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import com.github.davidmoten.odata.client.ClientException;
import com.github.davidmoten.odata.client.RequestHeader;

public final class BearerAuthenticator implements Authenticator {

    private static final String GRAPH_EXPLORER_BASE_URL= "https://graph.office.net";
	private static final String AUTHORIZATION_HEADER_NAME = "Authorization";
    private static final String OAUTH_BEARER_PREFIX = "Bearer ";

    private final Supplier tokenProvider;
    private final String baseUrl;

    public BearerAuthenticator(Supplier tokenProvider, String baseUrl) {
        this.tokenProvider = tokenProvider;
        this.baseUrl = baseUrl;
    }

    @Override
    public List authenticate(URL url, List m) {
        // chunked upload should not add authorization header hence check on
        // Content-Range
    	String urlString = url.toExternalForm();
        if ( //
                (!urlString.startsWith(baseUrl) //
                 && !urlString.startsWith(GRAPH_EXPLORER_BASE_URL)) //
                || m.stream().anyMatch(x -> x.name().equals(AUTHORIZATION_HEADER_NAME) //
                || x.name().equals("Content-Range"))) {
            return m;
        } else {
            List m2 = new ArrayList<>(m);
            try {
                final String token = tokenProvider.get();
                m2.add(RequestHeader.create(AUTHORIZATION_HEADER_NAME,
                        OAUTH_BEARER_PREFIX + token));
            } catch (Throwable e) {
                throw ClientException.from(e);
            }
            return m2;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy