<commit_before>﻿using BatteryCommander.Web.Models;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Converters;
using Serilog;
using Swashbuckle.AspNetCore.Swagger;
using System;
using System.IO;
using System.Net;
using System.Security.Claims;
using System.Threading.Tasks;

namespace BatteryCommander.Web
{
    public class Startup
    {
        public static String API_Version => "v1";

        public static String API_Name => $"Battery Commander {API_Version}";

        public static String APP_INSIGHTS_KEY = "66d7081f-e4a1-421f-b57a-38656917ee3d";

        private static Boolean IsDevelopment;

        public Startup(IHostingEnvironment env)
        {
            Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
                .WriteTo.RollingFile(pathFormat: @"logs\{Date}.log")
                .WriteTo.ApplicationInsightsTraces(APP_INSIGHTS_KEY)
                .MinimumLevel.Information()
                .CreateLogger();

            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
            IsDevelopment = env.IsDevelopment();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMemoryCache();

            // Add functionality to inject IOptions<T>
            services.AddOptions();

            // Add the Auth0 Settings object so it can be injected
            services.Configure<Auth0Settings>(Configuration.GetSection("Auth0"));

            var auth0Settings = new Auth0Settings { };

            Configuration.GetSection("Auth0").Bind(auth0Settings);

            services.AddCors(options =>
            {
                options.AddPolicy("Policy", builder =>
                    builder
                        .AllowAnyHeader()
                        .AllowAnyMethod()
                        .AllowAnyOrigin()
                        .AllowCredentials()
                        .Build()
                    );
            });

            // Add authentication services

            services
                .AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                })
                .AddJwtBearer(o =>
                {
                    o.Authority = $"https://{auth0Settings.Domain}/";
                    o.Audience = auth0Settings.ApiIdentifier;
                    o.RequireHttpsMetadata = !IsDevelopment;

                    //o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
                    //{
                    //    ValidateIssuer = true,
                    //    ValidateAudience = true,
                    //    ValidateLifetime = true,

                    //    ValidateIssuerSigningKey = true,
                    //    ValidIssuer = "",
                    //    ValidAudience = auth0Settings.ApiIdentifier
                    //};
                })
                .AddCookie(o => o.LoginPath = new PathString("/Home/Login"))
                .AddOpenIdConnect("Auth0", options =>
                {
                    options.GetClaimsFromUserInfoEndpoint = true;

                    // Set the authority to your Auth0 Domain
                    options.Authority = $"https://{auth0Settings.Domain}";

                    // Configure the Auth0 Client ID and Client Secret<commit_msg>Try not setting the authority?<commit_after>﻿using BatteryCommander.Web.Models;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Converters;
using Serilog;
using Swashbuckle.AspNetCore.Swagger;
using System;
using System.IO;
using System.Net;
using System.Security.Claims;
using System.Threading.Tasks;

namespace BatteryCommander.Web
{
    public class Startup
    {
        public static String API_Version => "v1";

        public static String API_Name => $"Battery Commander {API_Version}";

        public static String APP_INSIGHTS_KEY = "66d7081f-e4a1-421f-b57a-38656917ee3d";

        private static Boolean IsDevelopment;

        public Startup(IHostingEnvironment env)
        {
            Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
                .WriteTo.RollingFile(pathFormat: @"logs\{Date}.log")
                .WriteTo.ApplicationInsightsTraces(APP_INSIGHTS_KEY)
                .MinimumLevel.Information()
                .CreateLogger();

            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
            IsDevelopment = env.IsDevelopment();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMemoryCache();

            // Add functionality to inject IOptions<T>
            services.AddOptions();

            // Add the Auth0 Settings object so it can be injected
            services.Configure<Auth0Settings>(Configuration.GetSection("Auth0"));

            var auth0Settings = new Auth0Settings { };

            Configuration.GetSection("Auth0").Bind(auth0Settings);

            services.AddCors(options =>
            {
                options.AddPolicy("Policy", builder =>
                    builder
                        .AllowAnyHeader()
                        .AllowAnyMethod()
                        .AllowAnyOrigin()
                        .AllowCredentials()
                        .Build()
                    );
            });

            // Add authentication services

            services
                .AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                })
                .AddJwtBearer(o =>
                {
                    //o.Authority = $"https://{auth0Settings.Domain}/";
                    o.Audience = auth0Settings.ApiIdentifier;
                    o.RequireHttpsMetadata = !IsDevelopment;

                    //o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
                    //{
                    //    ValidateIssuer = true,
                    //    ValidateAudience = true,
                    //    ValidateLifetime = true,

                    //    ValidateIssuerSigningKey = true,
                    //    ValidIssuer = "",
                    //    ValidAudience = auth0Settings.ApiIdentifier
                    //};
                })
                .AddCookie(o => o.LoginPath = new PathString("/Home/Login"))
                .AddOpenIdConnect("Auth0", options =>
                {
                    options.GetClaimsFromUserInfoEndpoint = true;

                    // Set the authority to your Auth0 Domain
                    options.Authority = $"https://{auth0Settings.Domain}";

                    // Configure the Auth0 Client ID and Client Secret
{'fraction_non_alphanumeric': 0.08358782943105983, 'fraction_numerical': 0.008370093880782716, 'mean_word_length': 1.8078755160368372, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '499169', 'n_tokens_mistral': 2159, 'n_tokens_neox': 1996, 'n_words': 413}