-
Notifications
You must be signed in to change notification settings - Fork 1
JASPER-792 CSO service account OAUTH. #775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d1a26f3
JASPER-792 CSO service account OAUTH.
devinleighsmith 5d85390
role mapping simplification.
devinleighsmith 1dc3e5a
Add AWS secrets for CSO functionality.
devinleighsmith 6360d82
Pr comments.
devinleighsmith 34f16c3
correct service account check to account for environment naming.
devinleighsmith 346eb5f
add new secret to sync-secrets.sh
devinleighsmith 55e99dc
correct secret name for consistency.
devinleighsmith 6622f7d
Naming corrections.
devinleighsmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
api/Infrastructure/Authorization/CsoRoleAuthorizationHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
| using Scv.Api.Helpers.Extensions; | ||
| using Scv.Api.Infrastructure.Options; | ||
| using System; | ||
| using System.Linq; | ||
| using System.Security.Claims; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Scv.Api.Infrastructure.Authorization | ||
| { | ||
| /// <summary> | ||
| /// Authorization handler for CSO API roles. | ||
| /// Validates client roles from Keycloak JWT tokens. | ||
| /// </summary> | ||
| public class CSoRoleAuthorizationHandler : AuthorizationHandler<CsoRoleRequirement> | ||
| { | ||
| private readonly ILogger<CSoRoleAuthorizationHandler> _logger; | ||
| private readonly string _writeRoleName; | ||
| private readonly string _audience; | ||
|
|
||
| public CSoRoleAuthorizationHandler( | ||
| ILogger<CSoRoleAuthorizationHandler> logger, | ||
| IOptions<KeycloakOptions> keycloakOptions) | ||
| { | ||
| _logger = logger; | ||
| var options = keycloakOptions?.Value ?? throw new ArgumentNullException(nameof(keycloakOptions)); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(options.Audience)) | ||
| { | ||
| throw new ArgumentException("CsoKeycloak:Audience must be configured.", nameof(keycloakOptions)); | ||
| } | ||
|
|
||
| if (string.IsNullOrWhiteSpace(options.WriteRole)) | ||
| { | ||
| throw new ArgumentException("CsoKeycloak:WriteRole must be configured.", nameof(keycloakOptions)); | ||
| } | ||
|
|
||
| _audience = options.Audience; | ||
| _writeRoleName = options.WriteRole; | ||
| } | ||
|
|
||
| protected override Task HandleRequirementAsync( | ||
| AuthorizationHandlerContext context, | ||
| CsoRoleRequirement requirement) | ||
| { | ||
| if (context.User?.Identity?.IsAuthenticated != true || !context.User.IsCsoServiceAccountUser()) | ||
| { | ||
| _logger.LogWarning("User is not authenticated"); | ||
| context.Fail(); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| var clientRoles = context.User.ClientRoles(_audience); | ||
|
|
||
| if (clientRoles == null || !clientRoles.Any()) | ||
| { | ||
| _logger.LogWarning( | ||
| "No client roles found for audience: {ClientId}", | ||
| _audience); | ||
| context.Fail(); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| var hasRequiredRole = | ||
| string.Equals(requirement.RequiredRole, _writeRoleName, StringComparison.OrdinalIgnoreCase) && | ||
| clientRoles.Any(role => role.Equals(_writeRoleName, StringComparison.OrdinalIgnoreCase)); | ||
|
|
||
| if (hasRequiredRole) | ||
| { | ||
| var username = context.User.PreferredUsername(); | ||
|
|
||
| _logger.LogInformation( | ||
| "Authorization succeeded for user: {Username}, required role: {RequiredRole}", | ||
| username, | ||
| requirement.RequiredRole); | ||
|
|
||
| context.Succeed(requirement); | ||
| } | ||
| else | ||
| { | ||
| var username = context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value | ||
| ?? context.User.FindFirst("preferred_username")?.Value | ||
| ?? "unknown"; | ||
|
|
||
| _logger.LogWarning( | ||
| "Authorization failed for user: {Username}, required role: {RequiredRole}, user roles: {UserRoles}", | ||
| username, | ||
| requirement.RequiredRole, | ||
| string.Join(", ", clientRoles)); | ||
|
|
||
| context.Fail(); | ||
| } | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Authorization requirement for role-based access | ||
| /// </summary> | ||
| public class CsoRoleRequirement(string requiredRole) : IAuthorizationRequirement | ||
| { | ||
| public string RequiredRole { get; } = requiredRole; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| namespace Scv.Api.Infrastructure.Authorization | ||
| { | ||
| /// <summary> | ||
| /// Defines the role names for the JASPER CSO integration. | ||
| /// These are client roles from Keycloak. | ||
| /// </summary> | ||
| public static class CsoRoles | ||
| { | ||
| /// <summary> | ||
| /// Allows write order operations from CSO. maps to the CsoAuthorization:WriteRoleName configuration option. | ||
| /// </summary> | ||
| public const string Write = "cso-order-write"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Policy names for authorization. These policies determine which roles are required to access certain resources. | ||
| /// </summary> | ||
| public static class CsoPolicies | ||
| { | ||
| public const string RequireWriteRole = nameof(CSoRoleAuthorizationHandler); | ||
| public const string AuthenticationScheme = "CsoKeycloak"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using System.ComponentModel.DataAnnotations; | ||
|
|
||
| namespace Scv.Api.Infrastructure.Options | ||
| { | ||
| /// <summary> | ||
| /// Configuration options for Keycloak authentication and authorization. | ||
| /// </summary> | ||
| public sealed class KeycloakOptions | ||
| { | ||
| /// <summary> | ||
| /// Keycloak authority URL (e.g., https://keycloak.example.com/realms/your-realm) | ||
| /// </summary> | ||
| [Required] | ||
| public string Authority { get; set; } = default!; | ||
|
|
||
| /// <summary> | ||
| /// Expected audience in the JWT token (default: td-dev) | ||
| /// </summary> | ||
| [Required] | ||
| public string Audience { get; set; } = "jasper"; | ||
|
|
||
| /// <summary> | ||
| /// Client ID for the service account (default: jasper-td-dev) | ||
| /// </summary> | ||
| [Required] | ||
| public string ClientId { get; set; } = "cso-jasper-dev"; | ||
|
|
||
| /// <summary> | ||
| /// Client role name that allows query/search operations | ||
| /// </summary> | ||
| [Required] | ||
| public string WriteRole { get; set; } = "cso-order-write"; | ||
|
|
||
| /// <summary> | ||
| /// Validate the token issuer (default: true) | ||
| /// </summary> | ||
| public bool ValidateIssuer { get; set; } = true; | ||
|
|
||
| /// <summary> | ||
| /// Require HTTPS metadata (default: true, set to false only for local dev) | ||
| /// </summary> | ||
| public bool RequireHttpsMetadata { get; set; } = true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be reflected in our [docker-compose.yaml] (https://github.com/bcgov/jasper/blob/master/docker/docker-compose.yaml) file and in Terraform too?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missed pushing a commit, and updated.