113 lines
3.1 KiB
C#
113 lines
3.1 KiB
C#
using GameAuthApi.Services;
|
|
using GameAuthApi.Middleware;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
builder.WebHost.ConfigureKestrel(serverOptions => {});
|
|
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
c.SwaggerDoc("v1", new() { Title = "GameAuthApi", Version = "v1" });
|
|
|
|
c.AddSecurityDefinition("Bearer", new()
|
|
{
|
|
Name = "Authorization",
|
|
Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey,
|
|
Scheme = "Bearer",
|
|
BearerFormat = "JWT",
|
|
In = Microsoft.OpenApi.Models.ParameterLocation.Header,
|
|
Description = "Enter: `Bearer <your JWT token>`"
|
|
});
|
|
|
|
c.AddSecurityRequirement(new()
|
|
{
|
|
{
|
|
new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
|
{
|
|
Reference = new Microsoft.OpenApi.Models.OpenApiReference
|
|
{
|
|
Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme,
|
|
Id = "Bearer"
|
|
}
|
|
},
|
|
new string[] {}
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
builder.Services.AddSingleton<UserService>();
|
|
builder.Services.AddSingleton<LocationService>();
|
|
builder.Services.AddSingleton<PlayerService>();
|
|
builder.Services.AddSingleton<SpawnableService>();
|
|
builder.Services.AddSingleton<ItemService>();
|
|
builder.Services.AddSingleton<BlacklistService>();
|
|
builder.Services.AddSingleton<AuctionService>();
|
|
builder.Services.AddSingleton<ChatService>();
|
|
builder.Services.AddSingleton<AdminService>();
|
|
|
|
var jwtKey = builder.Configuration["Jwt:Key"];
|
|
var keyBytes = Encoding.UTF8.GetBytes(jwtKey);
|
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
|
ValidAudience = builder.Configuration["Jwt:Issuer"],
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(keyBytes)
|
|
};
|
|
});
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("AllowAll", policy =>
|
|
{
|
|
policy.AllowAnyOrigin()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod();
|
|
});
|
|
});
|
|
|
|
var app = builder.Build();
|
|
app.UseCors("AllowAll");
|
|
|
|
// Enable OPTIONS requests to reach controllers
|
|
app.Use(async (context, next) =>
|
|
{
|
|
if (context.Request.Method == HttpMethods.Options)
|
|
{
|
|
context.Response.StatusCode = 204;
|
|
return;
|
|
}
|
|
|
|
await next();
|
|
});
|
|
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
|
|
app.UseMiddleware<GameAuthApi.Middleware.TokenBlacklistMiddleware>();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|