using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Text.Json.Serialization; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Tokens; using MyCore.Framework.Models; using MyCore.Interfaces.Models; using MyCore.Service; using MyCore.Services; using MyCore.Services.MyControlPanel; using NSwag; using NSwag.Generation.AspNetCore; using NSwag.Generation.Processors.Security; using MyCore.Framework.Business; using MyCore.Service.Services; using MQTTnet; using MQTTnet.AspNetCore; using MQTTnet.AspNetCore.Extensions; using MyCore.Service.Extensions; using Mqtt.Client.AspNetCore.Services; using Mqtt.Client.AspNetCore.Settings; namespace MyCore { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; //MQTTService mQTTService = new MQTTService(); //MerossService merossService = new MerossService(); //ArloService arloService = new ArloService(); /*YeelightService yeelighService = new YeelightService(); yeelighService.GetDevices();*/ MapConfiguration(); } public IConfiguration Configuration { get; } private void MapConfiguration() { MapBrokerHostSettings(); MapClientSettings(); } private void MapBrokerHostSettings() { BrokerHostSettings brokerHostSettings = new BrokerHostSettings(); Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerHostSettings); AppSettingsProvider.BrokerHostSettings = brokerHostSettings; BrokerOnlineHostSettings brokerOnlineHostSettings = new BrokerOnlineHostSettings(); Configuration.GetSection(nameof(BrokerOnlineHostSettings)).Bind(brokerOnlineHostSettings); AppSettingsOnlineProvider.BrokerHostOnlineSettings = brokerOnlineHostSettings; } private void MapClientSettings() { ClientSettings clientSettings = new ClientSettings(); Configuration.GetSection(nameof(ClientSettings)).Bind(clientSettings); AppSettingsProvider.ClientSettings = clientSettings; ClientOnlineSettings clientOnlineSettings = new ClientOnlineSettings(); Configuration.GetSection(nameof(ClientOnlineSettings)).Bind(clientOnlineSettings); AppSettingsOnlineProvider.ClientOnlineSettings = clientOnlineSettings; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); // Register the Swagger generator, defining 1 or more Swagger documents /*services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "MyCoreApi", Version = "v1" }); // Set the comments path for the Swagger JSON and UI. var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = "apiKey" }); c.AddSecurityRequirement(new Dictionary> { { "Bearer", Enumerable.Empty() }, }); }); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = "JwtBearer"; options.DefaultChallengeScheme = "JwtBearer"; }) .AddJwtBearer("JwtBearer", jwtBearerOptions => { jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("%G2YZ=\tgN7fC9M$FXDt#q*a&]Z")), // Put the secret in a file or something ValidateIssuer = true, ValidIssuer = "MyCore App", ValidateAudience = true, ValidAudience = "Miotecher", ValidateLifetime = true, //validate the expiration and not before values in the token ClockSkew = TimeSpan.FromMinutes(5) //5 minute tolerance for the expiration date }; });*/ // Swagger services.AddControllers() .AddJsonOptions(options => { options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); }); services.AddOpenApiDocument(config => { ConfigureSwagger(config); }); services.AddCors(o => o.AddPolicy("AllowAll", builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); })); // Authentication var tokensConfiguration = Configuration.GetSection("Tokens"); var tokenSettings = tokensConfiguration.Get(); services.Configure(tokensConfiguration); foreach (var policy in Security.PoliciesConfiguration) services.AddAuthorization(options => { options.AddPolicy(policy.Name, policyAdmin => { foreach (var claim in policy.Claims) policyAdmin.RequireClaim(Security.ClaimTypes.Permission, claim); }); }); services .AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => { x.RequireHttpsMetadata = false; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenSettings.Secret)), ValidateIssuer = false, ValidateAudience = false, RequireExpirationTime = false, ValidateLifetime = true }; }); services.AddScoped(); services.AddScoped(typeof(ProfileLogic)); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddMqttClientHostedService(); services.AddMqttClientOnlineHostedService(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { /*app.UseCors( options => options.WithOrigins("http://localhost:4200").AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials() );*/ if (env.IsDevelopment()) { //app.UseDeveloperExceptionPage(); app.UseExceptionHandler(HandleError); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseOpenApi(); app.UseSwaggerUi3(); } private void ConfigureSwagger(AspNetCoreOpenApiDocumentGeneratorSettings config) { config.AddSecurity("bearer", Enumerable.Empty(), new OpenApiSecurityScheme { Type = OpenApiSecuritySchemeType.OAuth2, Description = "MyCore Authentication", Flow = OpenApiOAuth2Flow.Password, Flows = new OpenApiOAuthFlows() { Password = new OpenApiOAuthFlow() { Scopes = new Dictionary { {Security.Scope, "MyCore WebAPI"} }, TokenUrl = "/api/authentication/Token", AuthorizationUrl = "/authentication/Token", } } }); config.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer")); config.PostProcess = document => { document.Info.Title = "MyCore Service"; document.Info.Description = "API description"; document.Info.Version = "Version Pre-Alpha"; }; config.GenerateEnumMappingDescription = true; } private void HandleError(IApplicationBuilder error) { error.Run(async context => { var exceptionHandlerPathFeature = context.Features.Get(); var exception = exceptionHandlerPathFeature?.Error as RequestException; if (exception != null) { var json = exception.GetJson(); context.Response.ContentType = "application/json"; context.Response.StatusCode = exception.StatusCode; await context.Response.WriteAsync(json); } }); } } }