Skip to content

Add password authentication and user accounts - #29

Open
Dedac wants to merge 1 commit into
mainfrom
add-password-auth
Open

Dedac wants to merge 1 commit into
mainfrom
add-password-auth

Conversation

@Dedac

@Dedac Dedac commented Sep 16, 2026

Copy link
Copy Markdown
Owner

Summary

  • add username/password registration, login, logout, and password changes
  • store passwords as ASP.NET Core PBKDF2 hashes in a new database column
  • use HttpOnly cookie authentication and Blazor authentication state
  • require authentication for creating frequencies, rays, and prisms while keeping reads public
  • prevent request-body user ID spoofing by deriving the acting user from the authenticated cookie
  • add migration handling for existing users without passwords

Validation

  • dotnet build Beam.Server/Beam.Server.csproj --no-incremental
  • verified registration, duplicate-user rejection, password validation, login/logout, password changes, protected writes, public reads, and legacy passwordless account behavior against SQL Server

Add registration, cookie-backed login, password changes, and sign-out. Protect user actions with the authenticated identity, store password hashes, and add the database migration and client auth state.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 279b2234-e59e-4a47-8f00-af458d7e565b

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The migration can fail on legacy username collisions, and authentication flows contain unresolved security and state-handling issues.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 6 High severity · 3 Medium severity · 2 Low severity

Open (11)
What changed in this PR

Adds cookie-based user accounts and protects write operations while preserving public reads.

Changes:

  • Adds registration, login, logout, password changes, and Blazor authentication state.
  • Associates ray/prism writes with the authenticated user.
  • Adds password storage and legacy-user migration support.
File Description
README.md Documents accounts and .NET 10.
Beam.Shared/​RegisterRequest.cs Defines registration validation.
Beam.Shared/​LoginRequest.cs Defines login input.
Beam.Shared/​ChangePasswordRequest.cs Defines password-change validation.
Beam.Shared/​AuthResult.cs Defines authentication responses.
Beam.Server/​Program.cs Configures cookie authentication.
Beam.Server/​Controllers/​UserController.cs Makes user lookup read-only.
Beam.Server/​Controllers/​RayController.cs Protects ray creation and derives ownership.
Beam.Server/​Controllers/​PrismController.cs Protects prism mutations and derives ownership.
Beam.Server/​Controllers/​FrequencyController.cs Protects frequency creation.
Beam.Server/​Controllers/​AuthController.cs Implements account endpoints.
Beam.Server/​ClaimsPrincipalExtensions.cs Extracts authenticated user IDs.
Beam.Data/​Migrations/​BeamContextModelSnapshot.cs Updates the EF model snapshot.
Beam.Data/​Migrations/​20260916152802_AddUserPasswords.Designer.cs Describes the migration model.
Beam.Data/​Migrations/​20260916152802_AddUserPasswords.cs Migrates legacy users and passwords.
Beam.Data/​BeamContext.cs Configures username and password fields.
Beam.Client/​Shared/​UserLayout.razor Shows authenticated profile navigation.
Beam.Client/​Shared/​RedirectToLogin.razor Redirects protected routes.
Beam.Client/​Shared/​NavPanel.razor Adds account navigation and logout.
Beam.Client/​Services/​DataService.cs Tracks the authenticated user.
Beam.Client/​Services/​BeamAuthenticationStateProvider.cs Provides Blazor authentication state.
Beam.Client/​Services/​BeamApiService.cs Adds authentication API calls.
Beam.Client/​Program.cs Registers authorization services.
Beam.Client/​Pages/​Settings.razor Adds password settings and logout.
Beam.Client/​Pages/​Register.razor Adds account registration UI.
Beam.Client/​Pages/​RayList.razor Uses authenticated usernames.
Beam.Client/​Pages/​RayItem.razor Gates prism controls by authentication.
Beam.Client/​Pages/​RayInput.razor Gates ray creation by authentication.
Beam.Client/​Pages/​Login.razor Adds sign-in UI.
Beam.Client/​Pages/​FrequencyList.razor Gates frequency creation.
Beam.Client/​Beam.Client.csproj Adds authorization dependencies.
Beam.Client/​App.razor Enables authorized route handling.
Beam.Client/​_Imports.razor Imports authentication types.
Files not reviewed (1)
  • Beam.Data/Migrations/20260916152802_AddUserPasswords.Designer.cs: Generated file

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

return;
}

navigation.NavigateTo(string.IsNullOrWhiteSpace(ReturnUrl) ? "" : ReturnUrl);
return;
}

navigation.NavigateTo(string.IsNullOrWhiteSpace(ReturnUrl) ? "" : ReturnUrl);
Comment on lines +31 to +34
migrationBuilder.Sql(@"
UPDATE [Users]
SET [Username] = LEFT([Username], 32)
WHERE LEN([Username]) > 32;");
Comment on lines +132 to +135
user.PasswordHash = _passwordHasher.HashPassword(user, request.NewPassword);
await _context.SaveChangesAsync();

await SignInAsync(user);
Comment on lines +33 to +37
var alreadyPrismed = _context.Prisms.Any(p => p.RayId == prism.RayId && p.UserId == userId.Value);

if (!alreadyPrismed)
{
_context.Add(new Data.Prism { RayId = prism.RayId, UserId = userId.Value });
Comment on lines 42 to 46
var resp = await http.PostAsJsonAsync("api/Ray/Add", ray);

if (!resp.IsSuccessStatusCode) return new List<Ray>();

return (await resp.Content.ReadFromJsonAsync<List<Ray>>()) ?? new List<Ray>();
internal async Task Logout()
{
return (await http.GetFromJsonAsync<User>($"api/User/Get/{name}")) ?? new User();
await http.PostAsync("api/Auth/Logout", null);
Comment on lines +56 to +57
[HttpPost("[action]")]
public async Task<ActionResult<AuthResult>> Login([FromBody] LoginRequest request)
public string CurrentPassword { get; set; }

[Required(ErrorMessage = "A new password is required.")]
[StringLength(128, MinimumLength = 8, ErrorMessage = "Passwords must be at least 8 characters.")]
public string Username { get; set; }

[Required(ErrorMessage = "A password is required.")]
[StringLength(128, MinimumLength = 8, ErrorMessage = "Passwords must be at least 8 characters.")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants