feat(aspnetcore): add group-role mapping extension for authorization policies#143
feat(aspnetcore): add group-role mapping extension for authorization policies#143
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a new authorization extension method AddGroupRoleMappings that enables mapping group IDs from JWT claims to authorization policies, allowing role-based authorization when only group claims are available in tokens.
Changes:
- Added
AddGroupRoleMappingsextension method to configure authorization policies based on group-to-role mappings - Implemented comprehensive test coverage for various authorization scenarios
- Updated package version to 2.3.0-alpha.1 and documented the new feature in CHANGELOG
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| AuthorizationExtensions.cs | Implements the core extension method for group-role mapping with case-insensitive group ID comparison |
| AuthorizationExtensionsTests.cs | Provides comprehensive test coverage for authentication, group membership, and custom claim type scenarios |
| CHANGELOG.md | Documents the new HasAnyAllowedGroup extension method in version 2.3.0 |
| AT.Common.AspNetCore.Publish.csproj | Updates package version to 2.3.0-alpha.1 for the new feature release |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| foreach (var mapping in roleGroupMappings) | ||
| { | ||
| var allowed = (mapping.Value ?? Array.Empty<string>()).ToHashSet( | ||
| StringComparer.OrdinalIgnoreCase |
There was a problem hiding this comment.
Jeg tror denne kan skjule/skape usynlige feil.
Hvis det er to groups "reader" og "Reader", så blir bare den første brukt (siden de er equal under string compare med StringComparer.OrdinalIgnoreCase)
jeg ville heller latt det være case sensitive.
Hva enn vi velger så må vi sørge for at det passer i HasAnyAllowedGroup()
There was a problem hiding this comment.
Det er fordi gruppene her ikke er navn som "Reader", men objekt-ids i Entra. De er GUID's som ikke er case sensitive.
F.eks. 52d10c33-1ee7-4215-b433-f719b42d00f4 eller 52D10C33-1EE7-4215-b433-f719b42d00f4 fungerer likt ved kall til Entra API (testet med client credential request).
There was a problem hiding this comment.
Vi kan fjerne det uansett for å forenkle koden da. HashSet er neppe nødvendig også siden det er snakk om korte lister med grupper, men Copilot maste om det.
There was a problem hiding this comment.
Aha, da foreslår jeg at vi fjerner HasSet og legger på StringComparer.OrdinalIgnoreCase på det .Contains-kallet i HasAnyAllowedGroup()
|




Example usage:
NB: Client credential tokens (m2m) are not handled yet. We can add a path for checking for role
access_as_application, but what should the API be?