Skip to content

Commit

Permalink
Merge pull request #6 from eclipserporg/feature/restrict_to_local
Browse files Browse the repository at this point in the history
Restrict access to localhost
  • Loading branch information
pauliusdotpro authored Oct 25, 2023
2 parents deedef7 + beaa9a6 commit c629475
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
1 change: 0 additions & 1 deletion Controllers/DiscordController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using app.Apis;
using app.Models;
using app.Services;
using Microsoft.AspNetCore.Authorization;
Expand Down
40 changes: 11 additions & 29 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,7 +17,6 @@
builder.Services.AddRefitServices(serverApiSettings);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<DiscordService>();
builder.Services.AddSingleton<LoginService>();
builder.Services.AddSingleton<RunnerService>();
Expand All @@ -31,29 +25,7 @@
builder.Services.AddAuthentication("BasicAuthentication")
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("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();

Expand All @@ -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();
30 changes: 29 additions & 1 deletion ServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using app.Apis;
using app.Settings;
using Microsoft.OpenApi.Models;
using Refit;
using System.Net.Http.Headers;
using System.Text;
Expand All @@ -10,13 +11,40 @@ public static class ServiceExtensions
{
public static IServiceCollection AddRefitServices(this IServiceCollection collection, ServerApiSettings serverApiSettings)
{
collection.AddRefitClient<IServerDiscordApi>().ConfigureHttpClient( c =>
collection.AddRefitClient<IServerDiscordApi>().ConfigureHttpClient(c =>
{
c.BaseAddress = new Uri(serverApiSettings.Url);
c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{serverApiSettings.Username}:{serverApiSettings.Password}")));
});

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<string>()
}
});
});
return collection;
}
}
}

0 comments on commit c629475

Please sign in to comment.