Files
DiunaBI/WebAPI/Program.cs

98 lines
3.0 KiB
C#
Raw Normal View History

2025-03-03 13:06:53 +01:00
using FirebaseAdmin;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Firestore;
2023-02-22 12:12:38 +01:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
using WebAPI;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("SQLDatabase");
2025-03-03 13:06:53 +01:00
builder.Services.AddDbContext<AppDbContext>(x =>
{
2023-09-18 19:41:39 +02:00
x.UseSqlServer(connectionString);
x.EnableSensitiveDataLogging();
2025-03-03 13:06:53 +01:00
});
2023-02-22 12:12:38 +01:00
builder.Services.AddCors(options =>
{
2024-06-18 18:39:02 +02:00
options.AddPolicy("CORSPolicy", corsPolicyBuilder =>
2023-02-22 12:12:38 +01:00
{
2024-06-18 18:39:02 +02:00
corsPolicyBuilder.WithOrigins("http://localhost:4200")
2023-02-22 12:12:38 +01:00
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
2024-06-18 18:39:02 +02:00
corsPolicyBuilder.WithOrigins("https://diuna.bim-it.pl")
2023-02-22 12:12:38 +01:00
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
builder.Services.AddControllers();
2023-11-12 17:54:15 +01:00
2023-02-22 12:12:38 +01:00
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
2023-11-12 14:41:44 +01:00
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Secret"]!))
2023-02-22 12:12:38 +01:00
};
});
2023-11-12 14:41:44 +01:00
builder.Services.AddAuthentication();
2023-02-22 12:12:38 +01:00
builder.Services.AddSingleton(typeof(GoogleSheetsHelper));
builder.Services.AddSingleton(typeof(GoogleDriveHelper));
2025-03-03 13:06:53 +01:00
var fileName = "diunabi-admin-firebase.json";
#if DEBUG
fileName = "diunabi-admin-firebase-Development.json";
#endif
var credentialPath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credentialPath);
FirebaseAdmin.FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.GetApplicationDefault()
});
builder.Services.AddSingleton(FirestoreDb.Create("diunabi-admin"));
2023-02-22 12:12:38 +01:00
var app = builder.Build();
2023-11-12 17:54:15 +01:00
2023-02-22 12:12:38 +01:00
app.Use(async (context, next) =>
{
2024-06-18 22:24:04 +02:00
var token = context.Request.Headers.Authorization.ToString();
2025-03-03 13:06:53 +01:00
if (token.Length > 0
2024-07-15 19:46:02 +02:00
&& !context.Request.Path.ToString().Contains("getForPowerBI")
2025-03-03 13:06:53 +01:00
&& !context.Request.Path.ToString().Contains("DataInbox/Add"))
{
2023-02-22 12:12:38 +01:00
var handler = new JwtSecurityTokenHandler();
var data = handler.ReadJwtToken(token.Split(' ')[1]);
2024-06-18 22:24:04 +02:00
context.Request.Headers.Append("UserId", new Microsoft.Extensions.Primitives.StringValues(data.Subject));
2023-02-22 12:12:38 +01:00
}
await next(context);
});
app.UseCors("CORSPolicy");
2023-11-12 14:41:44 +01:00
app.UseAuthentication();
2023-02-22 12:12:38 +01:00
app.UseAuthorization();
app.MapControllers();
app.Run();