Skip to content

Conversation

@marcominerva
Copy link
Owner

This pull request introduces support for assigning roles to users authenticated via API Key and Basic Authentication, enhancing authorization capabilities across the library and its sample projects. The changes include updates to configuration files, controller logic, and data models to enable role-based claims and authorization checks, along with documentation improvements to guide usage.

Role-based authentication and authorization:

  • Added support for specifying roles in API Key and Basic Authentication credentials via the Roles property in configuration files (appsettings.json). These roles are now added as claims to the user's identity upon successful authentication. [1] [2]
  • Updated controller and minimal API endpoints in sample projects to extract user roles from claims and demonstrate role-based authorization using [Authorize(Roles = ...)] and policy requirements. The User model now includes a list of roles. [1] [2] [3] [4]

Configuration and extensibility:

  • Added comments and examples for custom claim types (NameClaimType, RoleClaimType) in API Key and Basic Authentication configuration, allowing further customization of identity claims. [1] [2]
  • Registered custom validator services (IApiKeyValidator, IBasicAuthenticationValidator) in sample projects to support dynamic authentication scenarios when credentials are not fixed in configuration. [1] [2] [3] [4]

Documentation improvements:

  • Updated the README.md to document the new role assignment feature for API Key and Basic Authentication, including configuration examples and usage of ASP.NET Core's role-based authorization. Also clarified JWT Bearer registration behavior. [1] [2]

Data model updates:

  • Extended the ApiKey record to include a Roles property and implemented custom equality logic to ensure role lists do not affect key comparison.

Minor adjustments:

  • Removed the obsolete EnableJwtBearerService setting from JWT Bearer configuration in sample projects for clarity. [1] [2]

Closes #166
Closes #168

Copilot AI and others added 24 commits October 8, 2025 15:47
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
…rences

Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
… Administrator, use configured RoleClaimType

Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
… classes

Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
…ervice

Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Updated `SimpleAuthentication.Swashbuckle.csproj` to use `SimpleAuthenticationTools.Abstractions` version 3.0.13. Added `SimpleAuthenticationTools.Abstractions` version 3.0.13 to `SimpleAuthentication.csproj`. Removed direct project reference to `SimpleAuthentication.Abstractions` in favor of package dependency.
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
…etting

Remove EnableJwtBearerService setting and always register IJwtBearerService
# Conflicts:
#	src/SimpleAuthentication.Abstractions/ApiKey/ApiKey.cs
#	src/SimpleAuthentication.Abstractions/ApiKey/ApiKeySettings.cs
#	src/SimpleAuthentication.Abstractions/BasicAuthentication/BasicAuthenticationSettings.cs
#	src/SimpleAuthentication.Abstractions/BasicAuthentication/Credential.cs
#	src/SimpleAuthentication.Swashbuckle/SimpleAuthentication.Swashbuckle.csproj
#	src/SimpleAuthentication/SimpleAuthentication.csproj
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
…-authentication

Add Roles support to ApiKey and Basic Authentication
- Ensure roles in `ApiKey` and `Credential` classes are non-optional and add equality/hash code methods.
- Initialize roles and claims with empty lists in settings and validation result classes to prevent null references.
- Simplify role claim addition in authentication handlers by removing null checks.
- Maintain consistency in `SimpleAuthenticationExtensions` by initializing roles with empty lists if null.
Updated MeController to include role-based authorization by modifying the Get method to accept role settings and return user roles. Added AdministratorOnly and UserOnly endpoints with role-based access. Updated User record to include roles. Extended appsettings.json for role configuration. Adjusted Program.cs for new authorization requirements.
Replaced `ArgumentNullException.ThrowIfNull` with `ArgumentException.ThrowIfNullOrWhiteSpace` across multiple files to ensure parameters are not null, empty, or whitespace. Updated `SwaggerExtensions.cs` and `OpenApiExtensions.cs` to use this validation for `sectionName`. In `SimpleAuthenticationExtensions.cs`, adjusted `defaultAuthenticationScheme` handling and improved validation for `JwtBearerSettings`, `ApiKeySettings`, and `BasicAuthenticationSettings` properties. These changes prevent runtime errors by ensuring meaningful content in string parameters.
@marcominerva marcominerva merged commit f2b89d1 into master Oct 16, 2025
6 checks passed
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.

Remove EnableJwtBearerService from settings and always register the Service Allow specifying user roles for ApiKey and Basic Authentication

2 participants