diff --git a/Controllers/DiscordController.cs b/Controllers/DiscordController.cs index cd9aed2..2b59d4b 100644 --- a/Controllers/DiscordController.cs +++ b/Controllers/DiscordController.cs @@ -1,4 +1,3 @@ -using app.Apis; using app.Models; using app.Services; using Microsoft.AspNetCore.Authorization; diff --git a/Program.cs b/Program.cs index 6307b11..7058c0d 100644 --- a/Program.cs +++ b/Program.cs @@ -1,12 +1,7 @@ using app.Services; using app.Settings; using app; -using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.IdentityModel.Tokens; -using System.Text; -using Microsoft.OpenApi.Models; using app.Models; -using Microsoft.Extensions.Configuration; using app.Middlewares; using Microsoft.AspNetCore.Authentication; @@ -22,7 +17,6 @@ builder.Services.AddRefitServices(serverApiSettings); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); @@ -31,29 +25,7 @@ builder.Services.AddAuthentication("BasicAuthentication") .AddScheme("BasicAuthentication", null); -builder.Services.AddSwaggerGen(c => -{ - c.SwaggerDoc("v1", new OpenApiInfo { Title = "Your API", Version = "v1" }); - c.AddSecurityDefinition("Basic", new OpenApiSecurityScheme - { - Name = "Authorization", - Type = SecuritySchemeType.Http, - Scheme = "basic", - In = ParameterLocation.Header, - Description = "Basic Authorization header." - }); - c.AddSecurityRequirement(new OpenApiSecurityRequirement - { - { - new OpenApiSecurityScheme - { - Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Basic" } - }, - new string[] { } - } - }); -}); - +builder.Services.AddSwaggerServices(); var app = builder.Build(); @@ -70,5 +42,15 @@ app.UseAuthentication(); app.UseAuthorization(); +app.Use(async (context, next) => +{ + if (!context.Connection.RemoteIpAddress.Equals(context.Connection.LocalIpAddress)) + { + context.Response.StatusCode = 403; + return; + } + await next.Invoke(); +}); + app.MapControllers(); app.Run(); diff --git a/ServiceExtensions.cs b/ServiceExtensions.cs index eb7b440..48c7b3a 100644 --- a/ServiceExtensions.cs +++ b/ServiceExtensions.cs @@ -1,5 +1,6 @@ using app.Apis; using app.Settings; +using Microsoft.OpenApi.Models; using Refit; using System.Net.Http.Headers; using System.Text; @@ -10,7 +11,7 @@ public static class ServiceExtensions { public static IServiceCollection AddRefitServices(this IServiceCollection collection, ServerApiSettings serverApiSettings) { - collection.AddRefitClient().ConfigureHttpClient( c => + collection.AddRefitClient().ConfigureHttpClient(c => { c.BaseAddress = new Uri(serverApiSettings.Url); c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{serverApiSettings.Username}:{serverApiSettings.Password}"))); @@ -18,5 +19,32 @@ public static IServiceCollection AddRefitServices(this IServiceCollection collec return collection; } + + public static IServiceCollection AddSwaggerServices(this IServiceCollection collection) + { + collection.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo { Title = "Your API", Version = "v1" }); + c.AddSecurityDefinition("Basic", new OpenApiSecurityScheme + { + Name = "Authorization", + Type = SecuritySchemeType.Http, + Scheme = "basic", + In = ParameterLocation.Header, + Description = "Basic Authorization header." + }); + c.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Basic" } + }, + Array.Empty() + } + }); + }); + return collection; + } } }