// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.http.policy;

import com.azure.core.credentials.SimpleTokenCache;
import com.azure.core.credentials.TokenCredential;
import com.azure.core.http.HttpPipelineCallContext;
import com.azure.core.http.HttpPipelineNextPolicy;
import com.azure.core.http.HttpResponse;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * The Pipeline policy that applies a token credential to an HTTP request
 * with "Bearer" scheme.
 */
public class BearerTokenAuthenticationPolicy implements HttpPipelinePolicy {
    private static final String AUTHORIZATION_HEADER = "Authorization";
    private static final String BEARER = "Bearer";

    private final TokenCredential credential;
    private final String[] scopes;
    private final SimpleTokenCache cache;

    /**
     * Creates BearerTokenAuthenticationPolicy.
     *
     * @param credential the token credential to authenticate the request
     * @param scope the scope of authentication the credential should get token for
     */
    public BearerTokenAuthenticationPolicy(TokenCredential credential, String scope) {
        this(credential, new String[] { scope });
    }

    /**
     * Creates BearerTokenAuthenticationPolicy.
     *
     * @param credential the token credential to authenticate the request
     * @param scopes the scopes of authentication the credential should get token for
     */
    public BearerTokenAuthenticationPolicy(TokenCredential credential, String... scopes) {
        Objects.requireNonNull(credential);
        Objects.requireNonNull(scopes);
        assert scopes.length > 0;
        this.credential = credential;
        this.scopes = scopes;
        this.cache = new SimpleTokenCache(() -> credential.getToken(scopes));
    }

    @Override
    public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
        return cache.getToken()
            .flatMap(token -> {
                context.getHttpRequest().getHeaders().put(AUTHORIZATION_HEADER, BEARER + " " + token.getToken());
                return next.process();
            });
    }
}

@@ -874,381 +874,8 @@
 e;%0A%0A
-    /**%0A     * Creates BearerTokenAuthenticationPolicy.%0A     *%0A     * @param credential the token credential to authenticate the request%0A     * @param scope the scope of authentication the credential should get token for%0A     */%0A    public BearerTokenAuthenticationPolicy(TokenCredential credential, String scope) %7B%0A        this(credential, new String%5B%5D %7B scope %7D);%0A    %7D%0A%0A
     

Remove BearerTokenAuthenticationPolicy ctor overload (#5411)