-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Clean up code for getting organization id and remove prefix where necessary * We do not need to check IsRecipient/IsSender explicitly because the PDP verifies whether the caller is allowed to receive/send on behalf of recipient/sender * Re-write all authorization code to use pre-defined "simple" methods to hide authorization logic/complexity. Make the "raw" function private. * Remove now unused dependencies * Add hotfix to main as a fix is not in priority * Bug: Purge needs CORS support to be used from Arbeidsflate * As we no longer use OnBehalfOf directly in the logic, we no longer need it anywhere aside from GetCorrespondences * Had to change logic in GetCorrespondenceOverview/Details because someone may have access both as a sender and recipient, but calling the API as a recipient, hence fetched should still be set. * Suggestions from CodeRabbit * Fix tests with authorization override * Also need override for legacy authentication * Smart coderabbit * Return org number without prefix
- Loading branch information
Showing
62 changed files
with
487 additions
and
405 deletions.
There are no files selected for viewing
This file contains 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
90 changes: 90 additions & 0 deletions
90
Test/Altinn.Correspondence.Tests/Helpers/AuthorizationOverride.cs
This file contains 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,90 @@ | ||
using Altinn.Correspondence.Core.Models.Entities; | ||
using Altinn.Correspondence.Core.Models.Enums; | ||
using Altinn.Correspondence.Core.Repositories; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using System.Security.Claims; | ||
|
||
namespace Altinn.Correspondence.Tests.Helpers; | ||
|
||
public static class AuthorizationOverride | ||
{ | ||
public static IServiceCollection OverrideAuthorization(this IServiceCollection services) | ||
{ | ||
var altinnAuthorizationService = new Mock<IAltinnAuthorizationService>(); | ||
altinnAuthorizationService | ||
.Setup(x => x.CheckAccessAsRecipient( | ||
It.IsAny<ClaimsPrincipal>(), | ||
It.IsAny<CorrespondenceEntity>(), | ||
It.IsAny<CancellationToken>())) | ||
.Returns((ClaimsPrincipal? user, CorrespondenceEntity corr, CancellationToken token) => { | ||
return Task.FromResult(NotRecipient(user)); | ||
}); | ||
|
||
altinnAuthorizationService | ||
.Setup(x => x.CheckAccessAsSender( | ||
It.IsAny<ClaimsPrincipal>(), | ||
It.IsAny<CorrespondenceEntity>(), | ||
It.IsAny<CancellationToken>())) | ||
.Returns((ClaimsPrincipal? user, CorrespondenceEntity corr, CancellationToken token) => { | ||
return Task.FromResult(NotSender(user)); | ||
}); | ||
|
||
altinnAuthorizationService | ||
.Setup(x => x.CheckAccessAsSender( | ||
It.IsAny<ClaimsPrincipal>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<CancellationToken>())) | ||
.Returns((ClaimsPrincipal? user, string resourceId, string sender, string? instance, CancellationToken token) => { | ||
return Task.FromResult(NotSender(user)); | ||
}); | ||
|
||
altinnAuthorizationService | ||
.Setup(x => x.CheckAccessAsAny( | ||
It.IsAny<ClaimsPrincipal>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<CancellationToken>())) | ||
.Returns((ClaimsPrincipal? user, string resource, string party, CancellationToken token) => { | ||
return Task.FromResult(!NotRecipient(user) || !NotSender(user)); | ||
}); | ||
|
||
altinnAuthorizationService | ||
.Setup(x => x.CheckMigrationAccess( | ||
It.IsAny<string>(), | ||
It.IsAny<List<ResourceAccessLevel>>(), | ||
It.IsAny<CancellationToken>())) | ||
.Returns((string resourceId, IEnumerable<ResourceAccessLevel> levels, CancellationToken token) => | ||
{ | ||
return Task.FromResult(true); | ||
}); | ||
|
||
altinnAuthorizationService | ||
.Setup(x => x.CheckUserAccessAndGetMinimumAuthLevel( | ||
It.IsAny<ClaimsPrincipal>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<List<ResourceAccessLevel>>(), | ||
It.IsAny<string>(), | ||
It.IsAny<CancellationToken>())) | ||
.Returns((ClaimsPrincipal? user, string ssn, string resourceId, List<ResourceAccessLevel> rights, string recipientOrgNo, CancellationToken token) => | ||
{ | ||
return Task.FromResult<int?>(3); | ||
}); | ||
|
||
return services.AddScoped(_ => altinnAuthorizationService.Object); | ||
} | ||
|
||
private static bool NotSender(ClaimsPrincipal? user) | ||
{ | ||
return !user?.Claims.Any(c => | ||
c.Type == "notSender") ?? true; | ||
} | ||
private static bool NotRecipient(ClaimsPrincipal? user) | ||
{ | ||
return !user?.Claims.Any(c => | ||
c.Type == "notRecipient") ?? true; | ||
} | ||
} |
This file contains 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
2 changes: 1 addition & 1 deletion
2
Test/Altinn.Correspondence.Tests/Helpers/MockPolicyEvaluator.cs
This file contains 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 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 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 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 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 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 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 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 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
2 changes: 1 addition & 1 deletion
2
....Correspondence.Tests/TestingController/Correspondence/CorrespondenceNotificationTests.cs
This file contains 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.