Feature/Auth: implement user authentication #3

Merged
homeburger merged 48 commits from feature/auth into main 2026-03-22 20:52:22 -05:00
5 changed files with 58 additions and 14 deletions
Showing only changes of commit 96026d448f - Show all commits

View File

@@ -1,16 +1,41 @@
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using agologumApi.Services; using agologumApi.Services;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
var key = builder.Configuration["Jwt:Key"];
if(key == null) return;
builder.Services.AddDbContext<AppDbContext>(options => builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddControllers(); builder.Services.AddControllers();
// services
builder.Services.AddScoped<UserService>(); builder.Services.AddScoped<UserService>();
builder.Services.AddScoped<JwtService>();
// configuration for jwt authentication
builder.Services.AddAuthentication(options => {
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters {
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))
};
});
builder.Services.AddAuthorization();
// configuration for behind my nginx proxy // configuration for behind my nginx proxy
builder.Services.Configure<ForwardedHeadersOptions>(options => builder.Services.Configure<ForwardedHeadersOptions>(options =>
@@ -41,10 +66,17 @@ builder.Services.AddCors(options =>
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
// https://www.reddit.com/r/dotnet/comments/1h7vzbs/how_do_you_guys_handle_authorization_on_a_web_api/
// add authorization here
// controllers will have endpoints based on authorization
// frontend is a different story
var app = builder.Build(); var app = builder.Build();
app.UseForwardedHeaders(); app.UseForwardedHeaders();
app.UseCors("dev"); app.UseCors("dev");
app.UseAuthentication();
app.UseAuthorization();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsEnvironment("Development")) { if (app.Environment.IsEnvironment("Development")) {

View File

@@ -8,6 +8,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.5" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.3" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.5"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.5">

View File

@@ -1,28 +1,30 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using agologumApi.Models; using agologumApi.Models;
using agologumApi.Services; using agologumApi.Services;
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/[controller]")]
public class UsersController : ControllerBase public class UsersController : ControllerBase {
{
private readonly UserService service_; private readonly UserService service_;
public UsersController(UserService service) public UsersController(UserService service) {
{
service_ = service; service_ = service;
} }
[AllowAnonymous] // accessible if not authorized
[HttpGet] [HttpGet]
public async Task<ActionResult<List<User>>> getUsers() public async Task<ActionResult<List<User>>> getUsers() {
{
return Ok(await service_.GetAll()); return Ok(await service_.GetAll());
} }
[AllowAnonymous]
[HttpGet("{id:int}")] [HttpGet("{id:int}")]
public async Task<ActionResult<User>> getUser(int id) public async Task<ActionResult<User>> getUser(int id) {
{
var user = await service_.Get(id); var user = await service_.Get(id);
if (user == null) return NotFound(); if (user == null) return NotFound();
@@ -30,9 +32,10 @@ public class UsersController : ControllerBase
return Ok(user); return Ok(user);
} }
[Authorize] // testing the authorization
[HttpPost] [HttpPost]
public async Task<ActionResult<User>> createUser(User user) public async Task<ActionResult<User>> createUser(User user) {
{
var created = await service_.Create(user); var created = await service_.Create(user);
return CreatedAtAction( return CreatedAtAction(
@@ -42,9 +45,10 @@ public class UsersController : ControllerBase
); );
} }
[Authorize]
[HttpPut("{id}")] [HttpPut("{id}")]
public async Task<ActionResult<User>> updateUser(int id, User user) public async Task<ActionResult<User>> updateUser(int id, User user) {
{
var updated = await service_.Update(user); var updated = await service_.Update(user);
if (updated == null) return NotFound(); if (updated == null) return NotFound();
@@ -52,9 +56,10 @@ public class UsersController : ControllerBase
return Ok(updated); return Ok(updated);
} }
[Authorize]
[HttpDelete("{id}")] [HttpDelete("{id}")]
public async Task<ActionResult> deleteUser(int id) public async Task<ActionResult> deleteUser(int id) {
{
var success = await service_.Delete(id); var success = await service_.Delete(id);
if (!success) return NotFound(); if (!success) return NotFound();

View File

@@ -6,5 +6,6 @@ public class User {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } = ""; public string Name { get; set; } = "";
public string Email { get; set; } = ""; public string Email { get; set; } = "";
public string PasswordHash { get; set; } = "";
}; };

View File

@@ -21,6 +21,10 @@ public class UserService {
return await db_.Users.FindAsync(id); return await db_.Users.FindAsync(id);
} }
public async Task<User?> Get(string username) {
return await db_.Users.FirstOrDefaultAsync(u => u.Name == username);
}
public async Task<User> Create(User user) { public async Task<User> Create(User user) {
db_.Users.Add(user); db_.Users.Add(user);
await db_.SaveChangesAsync(); await db_.SaveChangesAsync();