Save before testing router
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using System.Text;
|
||||
using System.Text.Json.Nodes;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using USEntryCoach.Server.Data;
|
||||
@ -21,28 +23,28 @@ builder.Services.AddOptionsWithValidateOnStart<AuthenticationSettings>().Bind(au
|
||||
|
||||
ApiSettings? apiSettings = apiSettingsSection.Get<ApiSettings>();
|
||||
AuthenticationSettings? authSettings = authSettingsSection.Get<AuthenticationSettings>();
|
||||
|
||||
builder.Services.AddSingleton<TokenService>();
|
||||
|
||||
// Configure JWT token generation.
|
||||
builder.Services.AddAuthentication(config =>
|
||||
{
|
||||
config.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
config.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
}).AddJwtBearer(config =>
|
||||
{
|
||||
// TODO: Only for debug!
|
||||
config.RequireHttpsMetadata = false;
|
||||
|
||||
config.SaveToken = true;
|
||||
config.TokenValidationParameters = new TokenValidationParameters()
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(authSettings!.JwtGenerationSecretBytes),
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false
|
||||
};
|
||||
});
|
||||
//
|
||||
// builder.Services.AddSingleton<TokenService>();
|
||||
//
|
||||
// // Configure JWT token generation.
|
||||
// builder.Services.AddAuthentication(config =>
|
||||
// {
|
||||
// config.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
// config.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
// }).AddJwtBearer(config =>
|
||||
// {
|
||||
// // TODO: Only for debug!
|
||||
// config.RequireHttpsMetadata = false;
|
||||
//
|
||||
// config.SaveToken = true;
|
||||
// config.TokenValidationParameters = new TokenValidationParameters()
|
||||
// {
|
||||
// ValidateIssuerSigningKey = true,
|
||||
// IssuerSigningKey = new SymmetricSecurityKey(authSettings!.JwtGenerationSecretBytes),
|
||||
// ValidateIssuer = false,
|
||||
// ValidateAudience = false
|
||||
// };
|
||||
// });
|
||||
|
||||
builder.Services.AddAuthorization(options =>
|
||||
{
|
||||
@ -54,17 +56,32 @@ builder.Services.AddAuthorization(options =>
|
||||
});
|
||||
});
|
||||
|
||||
builder.Services.AddDbContext<BloggingContext>(options =>
|
||||
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
builder.Services.AddIdentityApiEndpoints<IdentityUser>()
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>();
|
||||
|
||||
//
|
||||
// builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
|
||||
// .AddEntityFrameworkStores<ApplicationDbContext>();
|
||||
//
|
||||
// builder.Services.AddDbContext<BloggingContext>(options =>
|
||||
// options.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.MapIdentityApi<IdentityUser>();
|
||||
//
|
||||
// app.UseAuthentication();
|
||||
// app.UseAuthorization();
|
||||
|
||||
app.UseDefaultFiles();
|
||||
app.MapStaticAssets();
|
||||
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
@ -78,45 +95,45 @@ HttpClient client = new();
|
||||
client.DefaultRequestHeaders.Clear();
|
||||
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiSettings.OpenAiToken}");
|
||||
|
||||
app.MapPost("/login", Login);
|
||||
//app.MapPost("/login", Login);
|
||||
|
||||
app.MapGet("/hello", () => "Hello World!").RequireAuthorization("admin_greetings");
|
||||
|
||||
async Task<IResult> Login(LoginCredentials credentials, TokenService tokenService, HttpContext context)
|
||||
{
|
||||
// TODO: Check with database
|
||||
User? user = null;
|
||||
|
||||
if (credentials is {Username:"developer", Password:"dev"})
|
||||
{
|
||||
user = new User()
|
||||
{
|
||||
Username = credentials.Username,
|
||||
Password = credentials.Password,
|
||||
Id = Guid.CreateVersion7(),
|
||||
Role = UserRole.Developer
|
||||
};
|
||||
}
|
||||
else if (credentials is {Username:"user", Password:"us"})
|
||||
{
|
||||
user = new User()
|
||||
{
|
||||
Username = credentials.Username,
|
||||
Password = credentials.Password,
|
||||
Id = Guid.CreateVersion7(),
|
||||
Role = UserRole.User
|
||||
};
|
||||
}
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
var token = tokenService.GenerateToken(user);
|
||||
|
||||
return Results.Ok(new { token });
|
||||
}
|
||||
//
|
||||
// async Task<IResult> Login(LoginCredentials credentials, TokenService tokenService, HttpContext context)
|
||||
// {
|
||||
// // TODO: Check with database
|
||||
// User? user = null;
|
||||
//
|
||||
// if (credentials is {Username:"developer", Password:"dev"})
|
||||
// {
|
||||
// user = new User()
|
||||
// {
|
||||
// Username = credentials.Username,
|
||||
// Password = credentials.Password,
|
||||
// Id = Guid.CreateVersion7(),
|
||||
// Role = UserRole.Developer
|
||||
// };
|
||||
// }
|
||||
// else if (credentials is {Username:"user", Password:"us"})
|
||||
// {
|
||||
// user = new User()
|
||||
// {
|
||||
// Username = credentials.Username,
|
||||
// Password = credentials.Password,
|
||||
// Id = Guid.CreateVersion7(),
|
||||
// Role = UserRole.User
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// if (user == null)
|
||||
// {
|
||||
// return Results.Unauthorized();
|
||||
// }
|
||||
//
|
||||
// var token = tokenService.GenerateToken(user);
|
||||
//
|
||||
// return Results.Ok(new { token });
|
||||
// }
|
||||
|
||||
app.MapGet("/developer", (ClaimsPrincipal user) =>
|
||||
{
|
||||
@ -180,29 +197,4 @@ app.MapGet("/ephemeral_token", async () =>
|
||||
|
||||
app.MapFallbackToFile("/index.html");
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
|
||||
var context = services.GetRequiredService<BloggingContext>();
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
Blog b = new()
|
||||
{
|
||||
Url = "BLa"
|
||||
};
|
||||
|
||||
Post p = new()
|
||||
{
|
||||
Blog = b,
|
||||
Content = "sd zgsödiog söffdgkjasödfl kjasdfökljasdfölasddj föadj kfö",
|
||||
Title = "Hallo Welt"
|
||||
};
|
||||
|
||||
b.Posts.Add(p);
|
||||
|
||||
context.Blogs.Add(b);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
app.Run();
|
||||
|
||||
Reference in New Issue
Block a user