Files
PetriePanel/WebAPI/Startup.cs
littlefoot 320d939c76 Authentication finally moved to the dotnet way in webapi, ready to be added to to deal with users and such
Introspection access point properly uses basic auth of client id and secret to access
2021-10-14 20:54:58 -04:00

106 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using WebAPI.Auth;
using WebAPI.Data;
namespace WebAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
AppSettings.Init(configuration);
}
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.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo {Title = "Petrie Panel Web API", Version = "v1"});
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Description = "JWT Token",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}
});
});
services.AddDbContext<AppDbContext>(options => options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<PterodactylService>();
services.AddScoped<OIDCService>();
services.AddScoped<CustomAuthorizationFilter>();
services.AddAuthentication(opt =>
{
opt.DefaultScheme = OIDCTokenAuthenticationDefaults.DefaultScheme;
})
.AddScheme<OIDCTokenAuthenticationOptions, OIDCTokenAuthenticationHandler>(
OIDCTokenAuthenticationDefaults.DefaultScheme,
opt =>
{
opt.OIDCClientId = AppSettings.OIDCClientId;
opt.OIDCClientSecret = AppSettings.OIDCClientSecret;
opt.OIDCIntrospectionEndpoint = AppSettings.OIDCIntrospectionEndpoint;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}
}