From cd926525de45a2852ee5bbdd0dbd6ac0bb601bc9 Mon Sep 17 00:00:00 2001 From: Youssef11-dev Date: Thu, 3 Dec 2026 16:08:30 +0200 Subject: [PATCH 1/2] Cleanup template --- src/BuildingBlocks/Class1.cs | 6 ----- src/Host/Program.cs | 22 ------------------- src/Modules/Identity/Api/Class1.cs | 6 ----- src/Modules/Identity/Application/Class1.cs | 6 ----- src/Modules/Identity/Contracts/Class1.cs | 6 ----- src/Modules/Identity/Domain/Class1.cs | 6 ----- src/Modules/Identity/Infrastructure/Class1.cs | 6 ----- 7 files changed, 58 deletions(-) delete mode 100644 src/BuildingBlocks/Class1.cs delete mode 100644 src/Modules/Identity/Api/Class1.cs delete mode 100644 src/Modules/Identity/Application/Class1.cs delete mode 100644 src/Modules/Identity/Contracts/Class1.cs delete mode 100644 src/Modules/Identity/Domain/Class1.cs delete mode 100644 src/Modules/Identity/Infrastructure/Class1.cs diff --git a/src/BuildingBlocks/Class1.cs b/src/BuildingBlocks/Class1.cs deleted file mode 100644 index ec22074..0000000 --- a/src/BuildingBlocks/Class1.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Basira.BuildingBlocks; - -public class Class1 -{ - -} diff --git a/src/Host/Program.cs b/src/Host/Program.cs index 9f9d43f..8310654 100644 --- a/src/Host/Program.cs +++ b/src/Host/Program.cs @@ -23,28 +23,6 @@ app.UseHttpsRedirection(); -var summaries = new[] -{ - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" -}; - -app.MapGet("/weatherforecast", () => -{ - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; -}) -.WithName("GetWeatherForecast"); app.Run(); -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} diff --git a/src/Modules/Identity/Api/Class1.cs b/src/Modules/Identity/Api/Class1.cs deleted file mode 100644 index e109023..0000000 --- a/src/Modules/Identity/Api/Class1.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Basira.Modules.Identity.Api; - -public class Class1 -{ - -} diff --git a/src/Modules/Identity/Application/Class1.cs b/src/Modules/Identity/Application/Class1.cs deleted file mode 100644 index f63e2d7..0000000 --- a/src/Modules/Identity/Application/Class1.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Basira.Modules.Identity.Application; - -public class Class1 -{ - -} diff --git a/src/Modules/Identity/Contracts/Class1.cs b/src/Modules/Identity/Contracts/Class1.cs deleted file mode 100644 index 9289f89..0000000 --- a/src/Modules/Identity/Contracts/Class1.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Basira.Modules.Identity.Contracts; - -public class Class1 -{ - -} diff --git a/src/Modules/Identity/Domain/Class1.cs b/src/Modules/Identity/Domain/Class1.cs deleted file mode 100644 index ec8caed..0000000 --- a/src/Modules/Identity/Domain/Class1.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Basira.Modules.Identity.Domain; - -public class Class1 -{ - -} diff --git a/src/Modules/Identity/Infrastructure/Class1.cs b/src/Modules/Identity/Infrastructure/Class1.cs deleted file mode 100644 index fb09559..0000000 --- a/src/Modules/Identity/Infrastructure/Class1.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Basira.Modules.Identity.Infrastructure; - -public class Class1 -{ - -} From 2235429c15737056e1de0927f05535dc4b8a0ca5 Mon Sep 17 00:00:00 2001 From: Youssef11-dev Date: Thu, 3 Dec 2026 17:09:32 +0200 Subject: [PATCH 2/2] test(identity): add Organization.Rename tests feat(identity): add Organization.Rename refactor(identity): extract organization name validation docs: add ADR-0003 for authentication --- docs/adr/0003-authentication.md | 46 +++++++++++++++++++ .../Domain/Organizations/Organization.cs | 14 ++++-- tests/Identity.UnitTests/OrganizationTests.cs | 33 +++++++++++++ 3 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 docs/adr/0003-authentication.md diff --git a/docs/adr/0003-authentication.md b/docs/adr/0003-authentication.md new file mode 100644 index 0000000..578b3bd --- /dev/null +++ b/docs/adr/0003-authentication.md @@ -0,0 +1,46 @@ +# ADR-0003: JWT authentication with a custom User and PasswordHasher + +- **Status:** Accepted +- **Date:** 2026-12-03 + +## Context + +Basira is an API with no frontend of its own. It can be called by any client +(SPA, mobile, Postman). It is multi-tenant: every request must be tied to one +organization, and a user must never reach another organization's data. +The Identity module already has its own `User` entity and its own schema +(see ADR-0001). + +## Decision + +1. **JWT bearer tokens** for authentication. The client sends + `Authorization: Bearer ` on every request. +2. **Keep our own `User` entity** and use only `PasswordHasher` from + ASP.NET Core Identity for hashing passwords. We do not write our own + hashing algorithm. +3. **The organization comes from the token.** The token carries an `org_id` + claim, and the API reads the organization from it, never from the request + body or URL, because the client cannot change a signed token. + +## Alternatives considered + +- **Cookie authentication:** fits best when a frontend lives on the same + domain. Basira is a standalone API, so bearer tokens fit better. +- **Full ASP.NET Core Identity:** brings its own tables and user model, which + would conflict with our domain `User` and the module's schema. It is worth + learning separately, but not a fit here. +- **External provider (Auth0, Keycloak):** production-grade, but adds an + external dependency and hides the concepts this project is meant to teach. + +## Consequences + +**Positive** +- Stateless: the server does not store sessions. +- Works with any client. +- Tenant isolation comes from a signed value that the client cannot change. + +**Negative** +- A token stays valid until it expires. If a user is deactivated, their + existing token keeps working until expiry. To be addressed (short token + lifetime, refresh tokens, or checking `IsActive`) when auth is implemented. +- Implementing token issuing and validation correctly is our responsibility. \ No newline at end of file diff --git a/src/Modules/Identity/Domain/Organizations/Organization.cs b/src/Modules/Identity/Domain/Organizations/Organization.cs index 1541744..0f6230b 100644 --- a/src/Modules/Identity/Domain/Organizations/Organization.cs +++ b/src/Modules/Identity/Domain/Organizations/Organization.cs @@ -1,4 +1,6 @@ -using Basira.BuildingBlocks.Domain; +using System.Xml.Linq; + +using Basira.BuildingBlocks.Domain; namespace Basira.Modules.Identity.Domain.Organizations; @@ -18,6 +20,13 @@ private Organization(Guid id, string name, DateTime createdAtUtc) : base(id) } public static Organization Create(string name, DateTime utcNow) + { + string trimmed = ValidateName(name); + + return new Organization(Guid.NewGuid(), trimmed, utcNow); + } + public void Rename(string newName) => Name = ValidateName(newName); + private static string ValidateName(string name) { if (string.IsNullOrWhiteSpace(name)) throw new DomainException("Organization name is required."); @@ -25,7 +34,6 @@ public static Organization Create(string name, DateTime utcNow) var trimmed = name.Trim(); if (trimmed.Length > NameMaxLength) throw new DomainException($"Organization name cannot exceed {NameMaxLength} characters."); - - return new Organization(Guid.NewGuid(), trimmed, utcNow); + return trimmed; } } \ No newline at end of file diff --git a/tests/Identity.UnitTests/OrganizationTests.cs b/tests/Identity.UnitTests/OrganizationTests.cs index 0ad23d0..1dad46c 100644 --- a/tests/Identity.UnitTests/OrganizationTests.cs +++ b/tests/Identity.UnitTests/OrganizationTests.cs @@ -13,7 +13,9 @@ public void Create_with_valid_name_trims_it_and_sets_fields() var org = Organization.Create(" Acme ", Now); Assert.Equal("Acme", org.Name); + Assert.NotEqual(Guid.Empty, org.Id); + Assert.Equal(Now, org.CreatedAtUtc); } @@ -32,4 +34,35 @@ public void Create_with_too_long_name_throws() Assert.Throws(() => Organization.Create(name, Now)); } + [Fact] + public void Rename_with_valid_name_trims_and_updates_name() + { + var org = Organization.Create(" Acme ", Now); + + org.Rename(" Acme2 "); + + Assert.Equal("Acme2", org.Name); + + } + [Theory] + [InlineData("")] + [InlineData(" ")] + public void Rename_with_empty_name_throws(string name) + { + var org = Organization.Create(" Acme ", Now); + + Assert.Throws(() => org.Rename(name)); + + } + [Fact] + public void Rename_with_too_long_name_throws() + { + var org = Organization.Create(" Acme ", Now); + + var name = new string('a', Organization.NameMaxLength + 1); + + Assert.Throws(() => org.Rename(name)); + } + + } \ No newline at end of file