135 lines
3.8 KiB
C#
135 lines
3.8 KiB
C#
using System.Text;
|
|
using HelpDesk.API.Middleware;
|
|
using HelpDesk.API.Services;
|
|
using HelpDesk.Data;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Controllers
|
|
builder.Services.AddControllers();
|
|
|
|
// Database
|
|
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
|
|
?? Environment.GetEnvironmentVariable("DB_CONNECTION")
|
|
?? "Host=localhost;Port=5432;Database=helpdesk;Username=postgres;Password=postgres";
|
|
|
|
builder.Services.AddDbContext<HelpDeskDbContext>(options =>
|
|
options.UseNpgsql(connectionString));
|
|
|
|
// Repositories
|
|
builder.Services.AddRepositories();
|
|
|
|
// Services
|
|
builder.Services.AddScoped<IJwtService, JwtService>();
|
|
builder.Services.AddScoped<IPasswordService, PasswordService>();
|
|
|
|
// Authentication
|
|
var jwtSecretKey = builder.Configuration["JwtSettings:SecretKey"] ?? "YourSuperSecretKeyHereThatShouldBeAtLeast32CharactersLong!!";
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = builder.Configuration["JwtSettings:Issuer"] ?? "HelpDesk",
|
|
ValidAudience = builder.Configuration["JwtSettings:Audience"] ?? "HelpDeskAPI",
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecretKey)),
|
|
ClockSkew = TimeSpan.Zero
|
|
};
|
|
});
|
|
|
|
// Authorization Policies
|
|
builder.Services.AddAuthorization(options =>
|
|
{
|
|
options.AddPolicy("SuperAdmin", policy => policy.RequireRole("SuperAdmin"));
|
|
options.AddPolicy("Admin", policy => policy.RequireRole("SuperAdmin", "Admin"));
|
|
options.AddPolicy("Agent", policy => policy.RequireRole("SuperAdmin", "Admin", "Agent"));
|
|
options.AddPolicy("Customer", policy => policy.RequireRole("SuperAdmin", "Admin", "Agent", "Customer"));
|
|
});
|
|
|
|
// CORS
|
|
var frontendUrl = builder.Configuration["FrontendUrl"] ?? "http://localhost:3000";
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("Frontend", policy =>
|
|
{
|
|
policy.WithOrigins(frontendUrl)
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
|
|
// Swagger
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(options =>
|
|
{
|
|
options.SwaggerDoc("v1", new OpenApiInfo
|
|
{
|
|
Title = "HelpDesk API",
|
|
Version = "v1",
|
|
Description = "HelpDesk API for ticket management"
|
|
});
|
|
|
|
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
Name = "Authorization",
|
|
Type = SecuritySchemeType.Http,
|
|
Scheme = "Bearer",
|
|
BearerFormat = "JWT",
|
|
In = ParameterLocation.Header,
|
|
Description = "Enter your JWT token"
|
|
});
|
|
|
|
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = "Bearer"
|
|
}
|
|
},
|
|
Array.Empty<string>()
|
|
}
|
|
});
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Exception Middleware
|
|
app.UseMiddleware<ExceptionMiddleware>();
|
|
|
|
// Swagger
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
// CORS
|
|
app.UseCors("Frontend");
|
|
|
|
// Auth
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
// Map Controllers
|
|
app.MapControllers();
|
|
|
|
app.Run();
|