diff --git a/Marechai.Server/Helpers/CharRouteConstraint.cs b/Marechai.Server/Helpers/CharRouteConstraint.cs new file mode 100644 index 00000000..31296f2d --- /dev/null +++ b/Marechai.Server/Helpers/CharRouteConstraint.cs @@ -0,0 +1,43 @@ +/****************************************************************************** +// MARECHAI: Master repository of computing history artifacts information +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// --[ License ] -------------------------------------------------------------- +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2021 Natalia Portillo +*******************************************************************************/ + +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; + +namespace Marechai.Server.Helpers; + +/// Custom route constraint for char parameters +public class CharRouteConstraint : IRouteConstraint +{ + public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, + RouteDirection routeDirection) + { + if(!values.TryGetValue(routeKey, out object value)) return false; + + string stringValue = value?.ToString() ?? string.Empty; + + return stringValue.Length == 1 && char.TryParse(stringValue, out _); + } +} \ No newline at end of file diff --git a/Marechai.Server/Helpers/UlongRouteConstraint.cs b/Marechai.Server/Helpers/UlongRouteConstraint.cs new file mode 100644 index 00000000..5cf3518c --- /dev/null +++ b/Marechai.Server/Helpers/UlongRouteConstraint.cs @@ -0,0 +1,41 @@ +/****************************************************************************** +// MARECHAI: Master repository of computing history artifacts information +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// --[ License ] -------------------------------------------------------------- +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2021 Natalia Portillo +*******************************************************************************/ + +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; + +namespace Marechai.Server.Helpers; + +/// Custom route constraint for ulong parameters +public class UlongRouteConstraint : IRouteConstraint +{ + public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, + RouteDirection routeDirection) + { + if(!values.TryGetValue(routeKey, out object value)) return false; + + return ulong.TryParse(value?.ToString() ?? string.Empty, out _); + } +} \ No newline at end of file diff --git a/Marechai.Server/Program.cs b/Marechai.Server/Program.cs index 786bd31e..5a330b2b 100644 --- a/Marechai.Server/Program.cs +++ b/Marechai.Server/Program.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Text.Json; +using System.Text.Json.Serialization; using Aaru.CommonTypes.Interop; using Marechai.Database; using Marechai.Database.Models; @@ -10,6 +11,7 @@ using Marechai.Server.Services; using Markdig; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -21,7 +23,7 @@ namespace Marechai.Server; file class Program { - static IDbCore _database; + private static IDbCore _database; public static void Main(string[] args) { @@ -154,11 +156,19 @@ file class Program options.JsonSerializerOptions.Converters.Add(new IsoDateTimeConverter()); options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; options.JsonSerializerOptions.WriteIndented = true; + options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; }); // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi(); + // Add custom route constraints + builder.Services.Configure(options => + { + options.ConstraintMap["ulong"] = typeof(UlongRouteConstraint); + options.ConstraintMap["char"] = typeof(CharRouteConstraint); + }); + builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;