2020-12-16 21:35:51 +01:00

262 lines
9.5 KiB
C#

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;
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();*/
}
public IConfiguration Configuration { get; }
// 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<string, IEnumerable<string>> {
{ "Bearer", Enumerable.Empty<string>() },
});
});
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<TokensSettings>();
services.Configure<TokensSettings>(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<TokensService>();
services.AddScoped(typeof(ProfileLogic));
// Add the service (test purpose)
services.AddScoped<BookService>();
services.AddScoped<IoTDeviceService>();
services.AddScoped<UserDatabaseService>();
services.AddScoped<ProviderDatabaseService>();
services.AddScoped<DeviceDatabaseService>();
services.AddScoped<LocationDatabaseService>();
services.AddScoped<ScreenDeviceDatabaseService>();
services.AddScoped<MQTTService>(c => {
MQTTService mQTTService = new MQTTService("192.168.31.140", "mqtt", "mqtt");
mQTTService.GetDevices();
return mQTTService;
});
}
// 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<string>(), new OpenApiSecurityScheme
{
Type = OpenApiSecuritySchemeType.OAuth2,
Description = "MyCore Authentication",
Flow = OpenApiOAuth2Flow.Password,
Flows = new OpenApiOAuthFlows()
{
Password = new OpenApiOAuthFlow()
{
Scopes = new Dictionary<string, string>
{
{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<IExceptionHandlerPathFeature>();
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);
}
});
}
}
}