using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;

namespace OnlineShop.ApiService
{
    public static class AuthExtensions
    {
        public static void ConfigureApiJwt(this AuthenticationBuilder authentication)
        {
            // Named options
            authentication.Services
                .AddOptions<JwtBearerOptions>(
                   JwtBearerDefaults.AuthenticationScheme)
                .Configure<
                    IConfiguration,
                    IHttpClientFactory,
                    IHostEnvironment>(Configure);

            // Unnamed options
            authentication.Services.AddOptions<JwtBearerOptions>()
                .Configure<
                    IConfiguration,
                    IHttpClientFactory,
                    IHostEnvironment>(Configure);

            static void Configure(
                JwtBearerOptions options,
                IConfiguration configuration,
                IHttpClientFactory httpClientFactory,
                IHostEnvironment hostEnvironment)
            {
                var backchannelHttpClient =
                    httpClientFactory.CreateClient(
                        "OidcBackchannel");

                options.Backchannel = backchannelHttpClient;
                options.Authority =
                    backchannelHttpClient
                        .GetIdpAuthorityUri().ToString();
                options.RequireHttpsMetadata =
                    !hostEnvironment.IsDevelopment();
                options.MapInboundClaims = false;
                options.TokenValidationParameters =
                    new TokenValidationParameters
                    {
                        ValidateAudience = false
                    };
            }
        }
    }
}
