net8, language features, bugfixes

This commit is contained in:
kkb 2023-12-18 16:31:00 +01:00
parent 1591618c2c
commit ce99053a10
353 changed files with 3245 additions and 3944 deletions

View file

@ -1,4 +1,5 @@
using System.Net;
using System.Diagnostics;
using System.Net;
using System.Reflection;
namespace Insight.Domain.Constants;
@ -7,6 +8,14 @@ public static class Configuration
{
public static string Hostname => Dns.GetHostEntry("localhost").HostName;
public static Version Version => Assembly.GetEntryAssembly()?.GetName().Version ?? throw new ArgumentNullException("Version");
public static DirectoryInfo? AppDirectory => string.IsNullOrWhiteSpace(Environment.ProcessPath) ? null : new DirectoryInfo(Environment.ProcessPath).Parent;
public static DirectoryInfo? AppDirectory => AppDirectoryHelper();
public static string DefaultConfig => Path.Combine(AppDirectory?.FullName ?? string.Empty, "config.json");
private static DirectoryInfo? AppDirectoryHelper()
{
using var proc = Process.GetCurrentProcess();
if (proc?.MainModule?.FileName is null) throw new InvalidOperationException("MainModule not found");
return new DirectoryInfo(proc.MainModule.FileName).Parent;
}
}

View file

@ -2,6 +2,7 @@
public enum CategoryEnum
{
None = 0,
Network = 1,
System = 2,
Application = 3,

View file

@ -1,25 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>true</ImplicitUsings>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Insight.Domain</RootNamespace>
<Product>Insight</Product>
<AssemblyVersion>2023.12.14.0</AssemblyVersion>
<AssemblyVersion>2023.12.15.0</AssemblyVersion>
<SatelliteResourceLanguages>none</SatelliteResourceLanguages>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<!--Ignore MemoryPack Warning-->
<NoWarn>9193</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Vaitr.Bus" Version="2023.12.13" />
<PackageReference Include="Vaitr.Network" Version="2023.12.14" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
<PackageReference Include="Vaitr.Bus" Version="2023.12.15.1" />
<PackageReference Include="Vaitr.Network" Version="2023.12.16" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>none</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>
</PropertyGroup>
</Project>

View file

@ -1,5 +1,4 @@
using MessagePack;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
@ -31,17 +30,17 @@ public class Result
}
[IgnoreDataMember]
public Exception? Exception { get; init; }
public Exception? Exception { get; set; }
[IgnoreDataMember]
[MemberNotNullWhen(true, nameof(Exception))]
//[MemberNotNullWhen(true, nameof(Exception))]
public bool HadException => Exception is not null;
[DataMember]
public bool IsSuccess { get; init; }
public bool IsSuccess { get; set; }
[DataMember]
public string Reason { get; init; } = string.Empty;
public string Reason { get; set; } = string.Empty;
public static Result Fail(string reason)
@ -130,19 +129,19 @@ public class Result<T>
}
[IgnoreDataMember]
public Exception? Exception { get; init; }
public Exception? Exception { get; set; }
[IgnoreDataMember]
[MemberNotNullWhen(true, nameof(Exception))]
//[MemberNotNullWhen(true, nameof(Exception))]
public bool HadException => Exception is not null;
[DataMember]
[MemberNotNullWhen(true, nameof(Value))]
public bool IsSuccess { get; init; }
//[MemberNotNullWhen(true, nameof(Value))]
public bool IsSuccess { get; set; }
[DataMember]
public string Reason { get; init; } = string.Empty;
public string Reason { get; set; } = string.Empty;
[DataMember]
public T? Value { get; init; }
public T? Value { get; set; }
}

View file

@ -27,28 +27,17 @@ public class TokenResponse
public string? RefreshToken { get; set; }
}
public class TokenRevokeRequest
public class TokenRevokeRequest(string token, string? reason)
{
[JsonPropertyName("token"), Required]
public string? Token { get; set; }
public string? Token { get; set; } = token;
[JsonPropertyName("reason")]
public string? Reason { get; set; }
public TokenRevokeRequest(string token, string? reason)
{
Token = token;
Reason = reason;
}
public string? Reason { get; set; } = reason;
}
public class TokenRefreshRequest
public class TokenRefreshRequest(string token)
{
[JsonPropertyName("token"), Required]
public string? Token { get; set; }
public TokenRefreshRequest(string token)
{
Token = token;
}
public string? Token { get; set; } = token;
}

View file

@ -182,6 +182,6 @@ public partial class IPAddress2 : IMessage
IsIPv6Multicast = address.IsIPv6Multicast;
IsIPv6SiteLocal = address.IsIPv6SiteLocal;
IsIPv6Teredo = address.IsIPv6Teredo;
IsIPv6UniqueLocal = address.IsIPv6UniqueLocal;
//IsIPv6UniqueLocal = address.IsIPv6UniqueLocal;
}
}

View file

@ -6,10 +6,6 @@ namespace Insight.Domain.Network;
[MemoryPackUnion(0, typeof(Agent.Messages.AuthenticationRequest))]
[MemoryPackUnion(1, typeof(Agent.Messages.AuthenticationResponse))]
[MemoryPackUnion(2, typeof(Agent.Messages.InventoryRequest))]
//[MemoryPackUnion(3, typeof(Agent.Messages.ConsoleQueryRequest))]
//[MemoryPackUnion(4, typeof(Agent.Messages.ConsoleQueryResponse))]
//[MemoryPackUnion(5, typeof(Proxy<Agent.Messages.ConsoleQueryResponse>))]
//[MemoryPackUnion(6, typeof(Proxy<Agent.Messages.ConsoleQueryRequest>))]
[MemoryPackUnion(7, typeof(Agent.Messages.Event))]
[MemoryPackUnion(8, typeof(Agent.Messages.Trap))]
[MemoryPackUnion(9, typeof(Agent.Messages.Mainboard))]

View file

@ -4,7 +4,7 @@ using Microsoft.AspNetCore.Http.Extensions;
namespace Insight.Infrastructure;
public static class HttpRequestExtensions
public static partial class HttpRequestExtensions
{
public static void AddPagination<TData>(this HttpRequest request, PagedList<TData> pagelist)
{

View file

@ -0,0 +1,18 @@
using Insight.Infrastructure.Models;
using Microsoft.AspNetCore.Http;
using System.Text.Json;
namespace Insight.Infrastructure;
public static partial class HttpResponseExtensions
{
private static readonly JsonSerializerOptions _options = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
public static void AddPagination<TData>(this HttpResponse response, PagedList<TData> pagelist)
{
response.Headers.Append("X-Pagination", JsonSerializer.Serialize(pagelist.Meta as PagedHeaderData, _options));
}
}

View file

@ -0,0 +1,44 @@
using Insight.Infrastructure.Models;
using Microsoft.AspNetCore.Http;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Infrastructure.Web;
public static class MongoCollectionExtensions
{
public static async Task<PagedList<TData>> GetPagedAsync<TData>(
this IMongoCollection<TData> collection,
HttpRequest request,
HttpResponse response,
FilterDefinition<TData>? filter = null,
SortDefinition<TData>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default)
{
var result = await Infrastructure.MongoCollectionExtensions.GetPagedAsync(collection, filter, sort, offset, limit, cancellationToken).ConfigureAwait(false);
request?.AddPagination(result);
response?.AddPagination(result);
return result;
}
public static async Task<PagedList<TResult>> GetPagedAsync<TData, TResult>(
this IMongoCollection<TData> collection,
HttpRequest request,
HttpResponse response,
IAggregateFluent<BsonDocument> query,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default)
{
var result = await Infrastructure.MongoCollectionExtensions.GetPagedAsync<TData, TResult>(collection, query, offset, limit, cancellationToken).ConfigureAwait(false);
request?.AddPagination(result);
response?.AddPagination(result);
return result;
}
}

View file

@ -0,0 +1,229 @@
using AspNetCore.Identity.MongoDbCore.Extensions;
using AspNetCore.Identity.MongoDbCore.Infrastructure;
using Insight.Infrastructure.Entities;
using Insight.Infrastructure.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Net.Http.Headers;
using MongoDB.Bson;
using System.Text;
namespace Insight.Infrastructure;
public static partial class ServiceExtensions
{
public static IServiceCollection AddTokenServices(this IServiceCollection services, IConfiguration configuration)
{
var options = new Models.TokenOptions(
key: configuration.GetValue<string?>(Appsettings.JwtKey) ?? throw new Exception($"{Appsettings.JwtKey} value not set (appsettings)"),
expires: configuration.GetValue<int?>(Appsettings.JwtExp) ?? throw new Exception($"{Appsettings.JwtExp} value not set (appsettings)"),
audience: configuration.GetValue<Uri?>(Appsettings.JwtAudience) ?? throw new Exception($"{Appsettings.JwtAudience} value not set (appsettings)"),
issuer: configuration.GetValue<Uri?>(Appsettings.JwtIssuer) ?? throw new Exception($"{Appsettings.JwtIssuer} value not set (appsettings)"));
services.AddSingleton(options);
services.AddTransient<TokenService>();
return services;
}
public static IServiceCollection AddProxyServices(this IServiceCollection services)
{
// add before routing
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
return services;
}
public static IServiceCollection AddRoutingServices(this IServiceCollection services)
{
// add after proxy
services.AddRouting(options =>
{
options.LowercaseUrls = true;
});
return services;
}
public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration configuration)
{
var connectionString = configuration.GetValue<string?>(Appsettings.Database) ?? throw new Exception($"{Appsettings.Database} value not set (appsettings)");
services.AddIdentity<InsightUser, InsightRole>(options =>
{
})
.AddMongoDbStores<InsightUser, InsightRole, ObjectId>(connectionString, Settings.Database)
.AddDefaultTokenProviders()
.AddSignInManager();
return services;
}
public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
{
// REWRITE TO COOKIE ONLY FOR WEB
services.AddAuthentication(options =>
{
options.DefaultScheme = "Custom";
options.DefaultChallengeScheme = "Custom";
})
.AddCookie("Cookies", options =>
{
//options.Cookie.Domain = "insight.webmatic.de";
options.Cookie.Name = "insight";
options.LoginPath = "/account/login";
options.LogoutPath = "/account/logout";
options.ExpireTimeSpan = TimeSpan.FromHours(1);
options.SlidingExpiration = true;
options.Events.OnRedirectToLogin = options =>
{
if (options.Request.Path.StartsWithSegments("/api") && options.Response.StatusCode == 200)
options.Response.StatusCode = 401;
else
options.Response.Redirect(options.RedirectUri);
return Task.CompletedTask;
};
})
.AddJwtBearer("Bearer", options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters.ValidateActor = false;
options.TokenValidationParameters.ValidAudience = configuration.GetSection("Jwt:Audience").Value;
options.TokenValidationParameters.ValidateAudience = true;
options.TokenValidationParameters.ValidIssuer = configuration.GetSection("Jwt:Issuer").Value;
options.TokenValidationParameters.ValidateIssuer = true;
options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(configuration.GetSection("Jwt:Key").Value ?? throw new Exception("Configuration for [Jwt:Key] not found"))
);
options.TokenValidationParameters.ValidateIssuerSigningKey = true;
options.TokenValidationParameters.ValidateLifetime = true;
})
.AddPolicyScheme("Custom", "Custom", options =>
{
options.ForwardDefaultSelector = context =>
{
if (context.Request.Headers[HeaderNames.Authorization] is StringValues auth && auth.ToString().StartsWith("Bearer "))
return "Bearer";
else
return "Cookies";
};
});
return services;
}
public static IServiceCollection AddBearerAuthentication(this IServiceCollection services, IConfiguration configuration)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters.ValidateActor = false;
options.TokenValidationParameters.ValidAudience = configuration.GetValue<string?>(Appsettings.JwtAudience) ?? throw new Exception($"{Appsettings.JwtAudience} value not set (appsettings)");
options.TokenValidationParameters.ValidateAudience = true;
options.TokenValidationParameters.ValidIssuer = configuration.GetValue<string?>(Appsettings.JwtIssuer) ?? throw new Exception($"{Appsettings.JwtIssuer} value not set (appsettings)");
options.TokenValidationParameters.ValidateIssuer = true;
options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(configuration.GetValue<string?>(Appsettings.JwtKey) ?? throw new Exception($"{Appsettings.JwtKey} value not set (appsettings)"))
);
options.TokenValidationParameters.ValidateIssuerSigningKey = true;
options.TokenValidationParameters.ValidateLifetime = true;
});
return services;
}
//private static IServiceCollection AddIdentityServices2(this IServiceCollection services, IConfiguration configuration)
//{
// var identityOptions = new MongoDbIdentityConfiguration
// {
// MongoDbSettings = new MongoDbSettings
// {
// ConnectionString = configuration.GetSection("ConnectionStrings:Mongo").Value,
// DatabaseName = "insight"
// },
// IdentityOptionsAction = options =>
// {
// options.User.RequireUniqueEmail = true;
// options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-_";
// options.Password.RequireDigit = false;
// options.Password.RequiredLength = 8;
// options.Password.RequireNonAlphanumeric = false;
// options.Password.RequireUppercase = false;
// options.Password.RequireLowercase = false;
// options.SignIn.RequireConfirmedAccount = false;
// options.SignIn.RequireConfirmedEmail = false;
// options.SignIn.RequireConfirmedPhoneNumber = false;
// options.Lockout.MaxFailedAccessAttempts = 5;
// options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
// }
// };
// services.ConfigureMongoDbIdentity<InsightUser, InsightRole, ObjectId>(identityOptions)
// .AddDefaultTokenProviders()
// .AddSignInManager<InsightUser>();
// return services;
//}
//private static IServiceCollection AddIdentityAuthentication(this IServiceCollection services, IConfiguration configuration)
//{
// services.AddAuthentication(options =>
// {
// options.DefaultAuthenticateScheme =
// });
// cookieBuilder.ApplicationCookie = builder.AddApplicationCookie();
// cookieBuilder.ExternalCookie = builder.AddExternalCookie();
// cookieBuilder.TwoFactorRememberMeCookie = builder.AddTwoFactorRememberMeCookie();
// cookieBuilder.TwoFactorUserIdCookie = builder.AddTwoFactorUserIdCookie();
// .AddCookie(options =>
// {
// options.
// };
// .AddIdentityCookies();
// .AddCookie(options =>
// {
// // Specify where to redirect un-authenticated users
// options.LoginPath = "/account/login";
// // Specify the name of the auth cookie.
// // ASP.NET picks a dumb name by default.
// options.Cookie.Name = "insight";
// });
// return services;
//}
}

View file

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Insight.Infrastructure.Web</RootNamespace>
<Product>Insight</Product>
<AssemblyVersion>2023.12.15.0</AssemblyVersion>
<SatelliteResourceLanguages>none</SatelliteResourceLanguages>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.0.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Insight.Infrastructure\Insight.Infrastructure.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,9 @@
namespace Insight.Infrastructure.Models;
public class TokenOptions(string key, int expires, Uri? audience = null, Uri? issuer = null)
{
public string Key { get; set; } = key;
public int Expires { get; set; } = expires;
public Uri? Audience { get; set; } = audience;
public Uri? Issuer { get; set; } = issuer;
}

View file

@ -10,24 +10,17 @@ using System.Text;
namespace Insight.Infrastructure.Services;
public class TokenService
public class TokenService(TokenOptions options, IdentityService identityService, IMongoDatabase database)
{
private readonly TokenOptions _options;
private readonly IdentityService _identityService;
private readonly IMongoDatabase _database;
public TokenService(TokenOptions options, IdentityService identityService, IMongoDatabase database)
{
_options = options;
_identityService = identityService;
_database = database;
}
private readonly TokenOptions _options = options;
private readonly IdentityService _identityService = identityService;
private readonly IMongoDatabase _database = database;
public async Task<TokenResponse> GetAsync(string email, string password, string? code = null, IPAddress? ipa = null)
{
var user = await _identityService.LoginAsync(email, password, code).ConfigureAwait(false);
var accessToken = await CreateAccessTokenAsync(user, ipa).ConfigureAwait(false);
var accessToken = await CreateAccessTokenAsync(user).ConfigureAwait(false);
var refreshToken = await CreateRefreshTokenAsync(user, ipa).ConfigureAwait(false);
return new TokenResponse
@ -42,7 +35,7 @@ public class TokenService
{
if (string.IsNullOrWhiteSpace(refreshToken)) throw new ArgumentNullException(nameof(refreshToken));
var user = await _database.User().Find(p => p.RefreshTokens.Any(t => t.Token == refreshToken)).FirstOrDefaultAsync();
var user = await _database.User().Find(p => p.RefreshTokens != null && p.RefreshTokens.Any(t => t.Token == refreshToken)).FirstOrDefaultAsync();
if (user is null || user.RefreshTokens is null) throw new InvalidDataException("Invalid Refresh Token");
var token = user.RefreshTokens.First(p => p.Token == refreshToken);
@ -73,7 +66,7 @@ public class TokenService
var newRefreshToken = await CreateRefreshTokenAsync(user, ipa).ConfigureAwait(false);
// create access token
var accessToken = await CreateAccessTokenAsync(user, ipa).ConfigureAwait(false);
var accessToken = await CreateAccessTokenAsync(user).ConfigureAwait(false);
return new TokenResponse
{
@ -87,7 +80,7 @@ public class TokenService
{
if (string.IsNullOrWhiteSpace(refreshToken)) throw new ArgumentNullException(nameof(refreshToken));
var user = await _database.User().Find(p => p.RefreshTokens.Any(t => t.Token == refreshToken)).FirstOrDefaultAsync();
var user = await _database.User().Find(p => p.RefreshTokens != null && p.RefreshTokens.Any(t => t.Token == refreshToken)).FirstOrDefaultAsync();
if (user is null || user.RefreshTokens is null) throw new InvalidDataException("Invalid Refresh Token");
var token = user.RefreshTokens.First(p => p.Token == refreshToken);
@ -102,7 +95,7 @@ public class TokenService
token.ReasonRevoked = reason;
}
private async Task<(string, int)> CreateAccessTokenAsync(InsightUser user, IPAddress? ipa = null)
private async Task<(string, int)> CreateAccessTokenAsync(InsightUser user)
{
var claims = await _identityService.GetClaimsAsync(user).ConfigureAwait(false);

View file

@ -1,16 +0,0 @@
using Insight.Infrastructure.Models;
using Microsoft.AspNetCore.Http;
using System.Text.Json;
namespace Insight.Infrastructure;
public static class HttpResponseExtensions
{
public static void AddPagination<TData>(this HttpResponse response, PagedList<TData> pagelist)
{
response.Headers.Add("X-Pagination", JsonSerializer.Serialize(pagelist.Meta as PagedHeaderData, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}));
}
}

View file

@ -1,12 +1,11 @@
using Insight.Infrastructure.Models;
using Microsoft.AspNetCore.Http;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
namespace Insight.Infrastructure;
public static class MongoCollectionExtensions
public static partial class MongoCollectionExtensions
{
private const int _maximumLimit = 100;
@ -30,24 +29,6 @@ public static class MongoCollectionExtensions
return new PagedList<TData>(data, offset, limit, total);
}
public static async Task<PagedList<TData>> GetPagedAsync<TData>(
this IMongoCollection<TData> collection,
HttpRequest request,
HttpResponse response,
FilterDefinition<TData>? filter = null,
SortDefinition<TData>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default)
{
var result = await GetPagedAsync(collection, filter, sort, offset, limit, cancellationToken).ConfigureAwait(false);
request?.AddPagination(result);
response?.AddPagination(result);
return result;
}
public static async Task<PagedList<TResult>> GetPagedAsync<TData, TResult>(
this IMongoCollection<TData> collection,
IAggregateFluent<BsonDocument> query,
@ -62,21 +43,4 @@ public static class MongoCollectionExtensions
return new PagedList<TResult>(data.Select(x => BsonSerializer.Deserialize<TResult>(x)), offset, limit, total);
}
public static async Task<PagedList<TResult>> GetPagedAsync<TData, TResult>(
this IMongoCollection<TData> collection,
HttpRequest request,
HttpResponse response,
IAggregateFluent<BsonDocument> query,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default)
{
var result = await GetPagedAsync<TData, TResult>(collection, query, offset, limit, cancellationToken).ConfigureAwait(false);
request?.AddPagination(result);
response?.AddPagination(result);
return result;
}
}

View file

@ -1,24 +1,13 @@
using AspNetCore.Identity.MongoDbCore.Extensions;
using AspNetCore.Identity.MongoDbCore.Infrastructure;
using Insight.Infrastructure.Entities;
using Insight.Infrastructure.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity;
using Insight.Infrastructure.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Net.Http.Headers;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Core.Configuration;
using System.Text;
namespace Insight.Infrastructure;
public static class ServiceExtensions
public static partial class ServiceExtensions
{
public static IServiceCollection AddDatabase(this IServiceCollection services, IConfiguration configuration, ILoggerFactory? loggerFactory = null)
{
@ -42,221 +31,6 @@ public static class ServiceExtensions
{
services.AddTransient<IdentityService>();
services.AddTransient<AuthenticatorService>();
services.AddTransient<AccountService>();
services.AddTransient<CustomerService>();
services.AddTransient<HostService>();
services.AddTransient<AgentService>();
services.AddTransient<InventoryService>();
return services;
}
public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration configuration)
{
var connectionString = configuration.GetValue<string?>(Appsettings.Database) ?? throw new Exception($"{Appsettings.Database} value not set (appsettings)");
services.AddIdentity<InsightUser, InsightRole>(options =>
{
})
.AddMongoDbStores<InsightUser, InsightRole, ObjectId>(connectionString, Settings.Database)
.AddDefaultTokenProviders()
.AddSignInManager();
return services;
}
public static IServiceCollection AddTokenServices(this IServiceCollection services, IConfiguration configuration)
{
var options = new Models.TokenOptions(
key: configuration.GetValue<string?>(Appsettings.JwtKey) ?? throw new Exception($"{Appsettings.JwtKey} value not set (appsettings)"),
expires: configuration.GetValue<int?>(Appsettings.JwtExp) ?? throw new Exception($"{Appsettings.JwtExp} value not set (appsettings)"),
audience: configuration.GetValue<Uri?>(Appsettings.JwtAudience) ?? throw new Exception($"{Appsettings.JwtAudience} value not set (appsettings)"),
issuer: configuration.GetValue<Uri?>(Appsettings.JwtIssuer) ?? throw new Exception($"{Appsettings.JwtIssuer} value not set (appsettings)"));
services.AddSingleton(options);
services.AddTransient<TokenService>();
return services;
}
public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
{
// REWRITE TO COOKIE ONLY FOR WEB
services.AddAuthentication(options =>
{
options.DefaultScheme = "Custom";
options.DefaultChallengeScheme = "Custom";
})
.AddCookie("Cookies", options =>
{
//options.Cookie.Domain = "insight.webmatic.de";
options.Cookie.Name = "insight";
options.LoginPath = "/account/login";
options.LogoutPath = "/account/logout";
options.ExpireTimeSpan = TimeSpan.FromHours(1);
options.SlidingExpiration = true;
options.Events.OnRedirectToLogin = options =>
{
if (options.Request.Path.StartsWithSegments("/api") && options.Response.StatusCode == 200)
options.Response.StatusCode = 401;
else
options.Response.Redirect(options.RedirectUri);
return Task.CompletedTask;
};
})
.AddJwtBearer("Bearer", options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters.ValidateActor = false;
options.TokenValidationParameters.ValidAudience = configuration.GetSection("Jwt:Audience").Value;
options.TokenValidationParameters.ValidateAudience = true;
options.TokenValidationParameters.ValidIssuer = configuration.GetSection("Jwt:Issuer").Value;
options.TokenValidationParameters.ValidateIssuer = true;
options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(configuration.GetSection("Jwt:Key").Value ?? throw new ArgumentNullException(nameof(TokenValidationParameters), "Jwt:Key"))
);
options.TokenValidationParameters.ValidateIssuerSigningKey = true;
options.TokenValidationParameters.ValidateLifetime = true;
})
.AddPolicyScheme("Custom", "Custom", options =>
{
options.ForwardDefaultSelector = context =>
{
string authorization = context.Request.Headers[HeaderNames.Authorization];
if (!string.IsNullOrEmpty(authorization) && authorization.StartsWith("Bearer ")) return "Bearer";
return "Cookies";
};
});
return services;
}
public static IServiceCollection AddBearerAuthentication(this IServiceCollection services, IConfiguration configuration)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters.ValidateActor = false;
options.TokenValidationParameters.ValidAudience = configuration.GetValue<string?>(Appsettings.JwtAudience) ?? throw new Exception($"{Appsettings.JwtAudience} value not set (appsettings)");
options.TokenValidationParameters.ValidateAudience = true;
options.TokenValidationParameters.ValidIssuer = configuration.GetValue<string?>(Appsettings.JwtIssuer) ?? throw new Exception($"{Appsettings.JwtIssuer} value not set (appsettings)");
options.TokenValidationParameters.ValidateIssuer = true;
options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(configuration.GetValue<string?>(Appsettings.JwtKey) ?? throw new Exception($"{Appsettings.JwtKey} value not set (appsettings)"))
);
options.TokenValidationParameters.ValidateIssuerSigningKey = true;
options.TokenValidationParameters.ValidateLifetime = true;
});
return services;
}
public static IServiceCollection AddProxyServices(this IServiceCollection services, IConfiguration configuration)
{
// add before routing
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
return services;
}
public static IServiceCollection AddRoutingServices(this IServiceCollection services, IConfiguration configuration)
{
// add after proxy
services.AddRouting(options =>
{
options.LowercaseUrls = true;
});
return services;
}
private static IServiceCollection AddIdentityServices2(this IServiceCollection services, IConfiguration configuration)
{
var identityOptions = new MongoDbIdentityConfiguration
{
MongoDbSettings = new MongoDbSettings
{
ConnectionString = configuration.GetSection("ConnectionStrings:Mongo").Value,
DatabaseName = "insight"
},
IdentityOptionsAction = options =>
{
options.User.RequireUniqueEmail = true;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-_";
options.Password.RequireDigit = false;
options.Password.RequiredLength = 8;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
options.SignIn.RequireConfirmedAccount = false;
options.SignIn.RequireConfirmedEmail = false;
options.SignIn.RequireConfirmedPhoneNumber = false;
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
}
};
services.ConfigureMongoDbIdentity<InsightUser, InsightRole, ObjectId>(identityOptions)
.AddDefaultTokenProviders()
.AddSignInManager<InsightUser>();
return services;
}
private static IServiceCollection AddIdentityAuthentication(this IServiceCollection services, IConfiguration configuration)
{
services.AddAuthentication(options =>
{
//options.DefaultAuthenticateScheme =
});
//cookieBuilder.ApplicationCookie = builder.AddApplicationCookie();
//cookieBuilder.ExternalCookie = builder.AddExternalCookie();
//cookieBuilder.TwoFactorRememberMeCookie = builder.AddTwoFactorRememberMeCookie();
//cookieBuilder.TwoFactorUserIdCookie = builder.AddTwoFactorUserIdCookie();
//.AddCookie(options =>
//{
// options.
//};
//.AddIdentityCookies();
//.AddCookie(options =>
//{
// // Specify where to redirect un-authenticated users
// options.LoginPath = "/account/login";
// // Specify the name of the auth cookie.
// // ASP.NET picks a dumb name by default.
// options.Cookie.Name = "insight";
//});
return services;
}
}

View file

@ -1,28 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Insight.Infrastructure</RootNamespace>
<Product>Insight</Product>
<AssemblyVersion>2023.12.14.0</AssemblyVersion>
<ImplicitUsings>true</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>none</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>
<AssemblyVersion>2023.12.15.0</AssemblyVersion>
<SatelliteResourceLanguages>none</SatelliteResourceLanguages>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.13" />
<PackageReference Include="MongoDB.Driver" Version="2.23.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="AspNetCore.Identity.MongoDbCore" Version="3.1.2" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
<PackageReference Include="Vaitr.Scheduler" Version="2023.12.6" />
<PackageReference Include="MongoDB.Driver" Version="2.23.1" />
<PackageReference Include="Vaitr.Scheduler" Version="2023.12.15.1" />
</ItemGroup>
<ItemGroup>

View file

@ -1,17 +0,0 @@
namespace Insight.Infrastructure.Models;
public class TokenOptions
{
public string Key { get; set; }
public int Expires { get; set; }
public Uri? Audience { get; set; }
public Uri? Issuer { get; set; }
public TokenOptions(string key, int expires, Uri? audience = null, Uri? issuer = null)
{
Key = key;
Expires = expires;
Audience = audience;
Issuer = issuer;
}
}

View file

@ -1,35 +0,0 @@
using Insight.Infrastructure.Entities;
using Insight.Infrastructure.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
namespace Insight.Infrastructure.Services;
public class AccountService
{
private readonly IMongoDatabase _database;
private readonly ILogger<AccountService> _logger;
public AccountService(IMongoDatabase database, ILogger<AccountService> logger)
{
_database = database;
_logger = logger;
}
public Task<PagedList<InsightUser>> GetAsync(
FilterDefinition<InsightUser>? filter = null,
SortDefinition<InsightUser>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default) => _database.User().GetPagedAsync(filter, sort, offset, limit, cancellationToken);
public Task<PagedList<InsightUser>> GetAsync(
HttpRequest request,
HttpResponse response,
FilterDefinition<InsightUser>? filter = null,
SortDefinition<InsightUser>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default) => _database.User().GetPagedAsync(request, response, filter, sort, offset, limit, cancellationToken);
}

View file

@ -1,35 +0,0 @@
using Insight.Infrastructure.Entities;
using Insight.Infrastructure.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
namespace Insight.Infrastructure.Services;
public class AgentService
{
private readonly IMongoDatabase _database;
private readonly ILogger<AgentService> _logger;
public AgentService(IMongoDatabase database, ILogger<AgentService> logger)
{
_database = database;
_logger = logger;
}
public Task<PagedList<AgentEntity>> GetAsync(
FilterDefinition<AgentEntity>? filter = null,
SortDefinition<AgentEntity>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default) => _database.Agent().GetPagedAsync(filter, sort, offset, limit, cancellationToken);
public Task<PagedList<AgentEntity>> GetAsync(
HttpRequest request,
HttpResponse response,
FilterDefinition<AgentEntity>? filter = null,
SortDefinition<AgentEntity>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default) => _database.Agent().GetPagedAsync(request, response, filter, sort, offset, limit, cancellationToken);
}

View file

@ -7,16 +7,9 @@ using System.Text.Encodings.Web;
namespace Insight.Infrastructure.Services;
public class AuthenticatorService
public class AuthenticatorService(UserManager<InsightUser> userManager)
{
private readonly IMongoDatabase _database;
private readonly UserManager<InsightUser> _userManager;
public AuthenticatorService(IMongoDatabase database, UserManager<InsightUser> userManager)
{
_database = database;
_userManager = userManager;
}
private readonly UserManager<InsightUser> _userManager = userManager;
public async Task<bool> GetStatusAsync(InsightUser user)
{
@ -79,7 +72,7 @@ public class AuthenticatorService
return await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, count).ConfigureAwait(false);
}
public string GenerateQrCode(string email, string unformattedKey)
public static string GenerateQrCode(string email, string unformattedKey)
{
var encoder = UrlEncoder.Default;

View file

@ -1,35 +0,0 @@
using Insight.Infrastructure.Entities;
using Insight.Infrastructure.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
namespace Insight.Infrastructure.Services;
public class CustomerService
{
private readonly IMongoDatabase _database;
private readonly ILogger<CustomerService> _logger;
public CustomerService(IMongoDatabase database, ILogger<CustomerService> logger)
{
_database = database;
_logger = logger;
}
public Task<PagedList<CustomerEntity>> GetAsync(
FilterDefinition<CustomerEntity>? filter = null,
SortDefinition<CustomerEntity>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default) => _database.Customer().GetPagedAsync(filter, sort, offset, limit, cancellationToken);
public Task<PagedList<CustomerEntity>> GetAsync(
HttpRequest request,
HttpResponse response,
FilterDefinition<CustomerEntity>? filter = null,
SortDefinition<CustomerEntity>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default) => _database.Customer().GetPagedAsync(request, response, filter, sort, offset, limit, cancellationToken);
}

View file

@ -1,35 +0,0 @@
using Insight.Infrastructure.Entities;
using Insight.Infrastructure.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
namespace Insight.Infrastructure.Services;
public class HostService
{
private readonly IMongoDatabase _database;
private readonly ILogger<HostService> _logger;
public HostService(IMongoDatabase database, ILogger<HostService> logger)
{
_database = database;
_logger = logger;
}
public Task<PagedList<HostEntity>> GetAsync(
FilterDefinition<HostEntity>? filter = null,
SortDefinition<HostEntity>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default) => _database.Host().GetPagedAsync(filter, sort, offset, limit, cancellationToken);
public Task<PagedList<HostEntity>> GetAsync(
HttpRequest request,
HttpResponse response,
FilterDefinition<HostEntity>? filter = null,
SortDefinition<HostEntity>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default) => _database.Host().GetPagedAsync(request, response, filter, sort, offset, limit, cancellationToken);
}

View file

@ -5,18 +5,10 @@ using System.Security.Claims;
namespace Insight.Infrastructure.Services;
public class IdentityService
public class IdentityService(UserManager<InsightUser> userManager, RoleManager<InsightRole> roleManager)
{
private readonly UserManager<InsightUser> _userManager;
private readonly RoleManager<InsightRole> _roleManager;
private readonly ILogger<IdentityService> _logger;
public IdentityService(UserManager<InsightUser> userManager, RoleManager<InsightRole> roleManager, ILogger<IdentityService> logger)
{
_userManager = userManager;
_roleManager = roleManager;
_logger = logger;
}
private readonly UserManager<InsightUser> _userManager = userManager;
private readonly RoleManager<InsightRole> _roleManager = roleManager;
public async Task SeedAsync()
{

View file

@ -1,35 +0,0 @@
using Insight.Infrastructure.Entities;
using Insight.Infrastructure.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
namespace Insight.Infrastructure.Services;
public class InventoryService
{
private readonly IMongoDatabase _database;
private readonly ILogger<InventoryService> _logger;
public InventoryService(IMongoDatabase database, ILogger<InventoryService> logger)
{
_database = database;
_logger = logger;
}
public Task<PagedList<HostApplicationEntity>> GetAsync(
FilterDefinition<HostApplicationEntity>? filter = null,
SortDefinition<HostApplicationEntity>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default) => _database.HostApplication().GetPagedAsync(filter, sort, offset, limit, cancellationToken);
public Task<PagedList<HostApplicationEntity>> GetAsync(
HttpRequest request,
HttpResponse response,
FilterDefinition<HostApplicationEntity>? filter = null,
SortDefinition<HostApplicationEntity>? sort = null,
int offset = 0,
int limit = 10,
CancellationToken cancellationToken = default) => _database.HostApplication().GetPagedAsync(request, response, filter, sort, offset, limit, cancellationToken);
}