Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/adr/0003-authentication.md
Original file line number Diff line number Diff line change
@@ -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 <token>` on every request.
2. **Keep our own `User` entity** and use only `PasswordHasher<T>` 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.
6 changes: 0 additions & 6 deletions src/BuildingBlocks/Class1.cs

This file was deleted.

22 changes: 0 additions & 22 deletions src/Host/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
6 changes: 0 additions & 6 deletions src/Modules/Identity/Api/Class1.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/Modules/Identity/Application/Class1.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/Modules/Identity/Contracts/Class1.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/Modules/Identity/Domain/Class1.cs

This file was deleted.

14 changes: 11 additions & 3 deletions src/Modules/Identity/Domain/Organizations/Organization.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Basira.BuildingBlocks.Domain;
using System.Xml.Linq;

using Basira.BuildingBlocks.Domain;

namespace Basira.Modules.Identity.Domain.Organizations;

Expand All @@ -18,14 +20,20 @@ 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.");

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;
}
}
6 changes: 0 additions & 6 deletions src/Modules/Identity/Infrastructure/Class1.cs

This file was deleted.

33 changes: 33 additions & 0 deletions tests/Identity.UnitTests/OrganizationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -32,4 +34,35 @@ public void Create_with_too_long_name_throws()

Assert.Throws<DomainException>(() => 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<DomainException>(() => 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<DomainException>(() => org.Rename(name));
}


}