yavsc/src/Api/Program.cs

115 lines
3.9 KiB
C#
Raw Normal View History

2025-02-11 04:45:05 +00:00
/*
Copyright (c) 2024 HigginsSoft, Alexander Higgins - https://github.com/alexhiggins732/
Copyright (c) 2018, Brock Allen & Dominick Baier. All rights reserved.
Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
Source code and license this software can be found
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
2025-02-15 19:57:08 +00:00
using IdentityModel;
2025-02-14 00:20:35 +00:00
using Microsoft.AspNetCore.Identity;
2025-02-15 12:45:27 +00:00
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2025-02-11 04:45:05 +00:00
using Microsoft.AspNetCore.Mvc;
2025-02-14 00:20:35 +00:00
using Microsoft.EntityFrameworkCore;
2025-02-26 18:59:08 +00:00
using Yavsc.Helpers;
2025-02-15 12:45:27 +00:00
using Yavsc.Interface;
2025-02-14 00:20:35 +00:00
using Yavsc.Models;
2025-02-15 12:45:27 +00:00
using Yavsc.Services;
2025-02-11 04:45:05 +00:00
internal class Program
{
private static async Task Main(string[] args)
{
Console.Title = "API";
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
2025-02-16 22:40:51 +00:00
// builder.Services.AddDistributedMemoryCache();
2025-02-11 04:45:05 +00:00
// accepts any access token issued by identity server
2025-02-15 19:57:08 +00:00
// adds an authorization policy for scope 'scope1'
2025-02-17 23:56:28 +00:00
2025-02-11 04:45:05 +00:00
services
.AddAuthorization(options =>
{
options.AddPolicy("ApiScope", policy =>
{
policy
.RequireAuthenticatedUser()
2025-02-17 23:56:28 +00:00
.RequireClaim(JwtClaimTypes.Scope, new string[] { "scope2" });
2025-02-11 04:45:05 +00:00
});
})
.AddCors(options =>
{
// this defines a CORS policy called "default"
options.AddPolicy("default", policy =>
{
policy.WithOrigins("https://localhost:5003")
.AllowAnyHeader()
.AllowAnyMethod();
});
})
2025-02-15 19:57:08 +00:00
.AddControllers();
2025-02-11 04:45:05 +00:00
// accepts any access token issued by identity server
2025-02-15 19:57:08 +00:00
var authenticationBuilder = services.AddAuthentication("Bearer")
2025-02-11 04:45:05 +00:00
.AddJwtBearer("Bearer", options =>
{
options.IncludeErrorDetails = true;
options.Authority = "https://localhost:5001";
options.TokenValidationParameters =
new() { ValidateAudience = false };
2025-02-17 23:56:28 +00:00
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<ITrueEmailSender, MailSender>()
.AddTransient<IBillingService, BillingService>()
.AddTransient<ICalendarManager, CalendarManager>();
2025-02-26 18:59:08 +00:00
services.AddTransient<IFileSystemAuthManager, FileSystemAuthManager>();
2025-02-17 23:56:28 +00:00
/*
services.AddSingleton<IConnexionManager, HubConnectionManager>();
services.AddSingleton<ILiveProcessor, LiveProcessor>();
services.AddIdentityApiEndpoints<ApplicationUser>();
services.AddSession();
2025-02-26 18:59:08 +00:00
*/
WorkflowHelpers.ConfigureBillingService();
2025-02-11 04:45:05 +00:00
using (var app = builder.Build())
{
if (app.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();
app
.UseRouting()
.UseAuthentication()
2025-02-16 17:28:38 +00:00
.UseAuthorization()
.UseCors("default")
2025-02-17 23:56:28 +00:00
/* .UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute()
.RequireAuthorization();
})*/
2025-02-16 22:40:51 +00:00
;
2025-02-17 23:56:28 +00:00
// app.MapIdentityApi<ApplicationUser>().RequireAuthorization("ApiScope");
2025-02-16 22:40:51 +00:00
app.MapDefaultControllerRoute();
2025-02-16 17:28:38 +00:00
app.MapGet("/identity", (HttpContext context) =>
new JsonResult(context?.User?.Claims.Select(c => new { c.Type, c.Value }))
);
2025-02-17 23:56:28 +00:00
// app.UseSession();
2025-02-11 04:45:05 +00:00
await app.RunAsync();
2025-02-17 23:56:28 +00:00
}
;
2025-02-14 00:20:35 +00:00
2025-02-12 20:41:14 +00:00
2025-02-11 04:45:05 +00:00
}
}