Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasisnes committed Aug 12, 2024
1 parent cf0d941 commit d1ba126
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface ISingleRightsService
/// <param name="authenticatedUserAuthlevel">The authentication level of the authenticated user performing the delegation</param>
/// <param name="delegation">The delegation</param>
/// <returns>The result of the delegation</returns>
public Task<DelegationActionResult> DelegateRights(int authenticatedUserId, int authenticatedUserAuthlevel, DelegationLookup delegation);
public Task<DelegationActionResult> DelegateRights(int authenticatedUserId, int authenticatedUserAuthlevel, DelegationLookup delegation, CancellationToken cancellationToken = default);

/// <summary>
/// Gets all offered single rights delegations for a reportee
Expand Down
12 changes: 6 additions & 6 deletions src/Altinn.AccessManagement.Core/Services/SingleRightsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public async Task<DelegationCheckResponse> RightsDelegationCheck(int authenticat
}

/// <inheritdoc/>
public async Task<DelegationActionResult> DelegateRights(int authenticatedUserId, int authenticatedUserAuthlevel, DelegationLookup delegation)
public async Task<DelegationActionResult> DelegateRights(int authenticatedUserId, int authenticatedUserAuthlevel, DelegationLookup delegation, CancellationToken cancellationToken = default)
{
(DelegationActionResult result, ServiceResource resource, Party fromParty, List<AttributeMatch> to) = await ValidateDelegationLookupModel(DelegationActionType.Delegation, delegation, authenticatedUserId);
if (!result.IsValid)
Expand All @@ -125,7 +125,7 @@ public async Task<DelegationActionResult> DelegateRights(int authenticatedUserId

// Verify authenticated users delegable rights
RightsQuery rightsQuery = RightsHelper.GetRightsQuery(authenticatedUserId, fromParty.PartyId, resourceRegistryId, org, app);
List<Right> usersDelegableRights = await _pip.GetRights(rightsQuery, getDelegableRights: true);
List<Right> usersDelegableRights = await _pip.GetRights(rightsQuery, getDelegableRights: true, cancellationToken: cancellationToken);
if (usersDelegableRights == null || usersDelegableRights.Count == 0)
{
result.Errors.Add("right[0].Resource", $"Authenticated user does not have any delegable rights for the resource: {resourceRegistryId}");
Expand Down Expand Up @@ -187,7 +187,7 @@ public async Task<ValidationProblemDetails> RevokeRightsDelegation(int authentic
{
return assertion;
}

var fromAttribute = await _resolver.Resolve(delegation.From, [AltinnXacmlConstants.MatchAttributeIdentifiers.PartyAttribute], cancellationToken);
var toAttribute = await _resolver.Resolve(delegation.To, Urn.Altinn2InternalIds, cancellationToken);

Expand Down Expand Up @@ -395,7 +395,7 @@ private ValidationProblemDetails AssertRevokeDelegationInput(DelegationLookup de
else if (DelegationHelper.TryGetSingleAttributeMatchValue(delegation.To, AltinnXacmlConstants.MatchAttributeIdentifiers.SystemUserUuid, out string toSystemUserUuidAttrValue))
{
toSystemUser = await _contextRetrievalService.GetSystemUserById(fromParty.PartyId, toSystemUserUuidAttrValue);

if (toSystemUser == null)
{
result.Errors.Add("To", $"The provided To attribute value could not be found as a valid systemuser.");
Expand All @@ -421,14 +421,14 @@ private ValidationProblemDetails AssertRevokeDelegationInput(DelegationLookup de
result.Errors.Add("To", $"A distinct recipient party for the delegation, could not be identified by the supplied attributes. A recipient can be identified by either a single {AltinnXacmlConstants.MatchAttributeIdentifiers.OrganizationNumberAttribute} or {AltinnXacmlConstants.MatchAttributeIdentifiers.EnterpriseUserName} attribute, or a combination of {AltinnXacmlConstants.MatchAttributeIdentifiers.PersonId} and {AltinnXacmlConstants.MatchAttributeIdentifiers.PersonLastName} attributes, {AltinnXacmlConstants.MatchAttributeIdentifiers.PersonUserName} and {AltinnXacmlConstants.MatchAttributeIdentifiers.PersonLastName} attributes or {AltinnXacmlConstants.MatchAttributeIdentifiers.SystemUserUuid} attribute.");
return (result, resource, null, null);
}

// Verify delegation From and To is not the same party (with exception for Altinn 2 Enterprise users)
if (fromParty.PartyId == toParty?.PartyId || (toUser != null && fromParty.PartyId == toUser.PartyId && toUser.Party.PartyTypeName != PartyType.Organisation))
{
result.Errors.Add("To", $"The From party and the To recipient are the same. Self-delegation is not supported as it serves no purpose.");
return (result, resource, null, null);
}

// Build To AttributeMatch to be used for the delegation rules
List<AttributeMatch> to = new List<AttributeMatch>();
if (toParty != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public async Task<ActionResult<List<RightDelegationCheckResultExternal>>> Delega
/// </summary>
/// <param name="party">The reportee party</param>
/// <param name="rightsDelegationRequest">Request model for rights delegation</param>
/// <param name="cancellationToken">CancellationToken</param>
/// <response code="200" cref="List{RightDelegationStatusExternal}">Ok</response>
/// <response code="400">Bad Request</response>
/// <response code="401">Unauthorized</response>
Expand All @@ -194,7 +195,7 @@ public async Task<ActionResult<List<RightDelegationCheckResultExternal>>> Delega
[ProducesResponseType(403)]
[ProducesResponseType(500)]
[FeatureGate(FeatureFlags.RightsDelegationApi)]
public async Task<ActionResult<RightsDelegationResponseExternal>> Delegation([FromRoute] string party, [FromBody] RightsDelegationRequestExternal rightsDelegationRequest)
public async Task<ActionResult<RightsDelegationResponseExternal>> Delegation([FromRoute] string party, [FromBody] RightsDelegationRequestExternal rightsDelegationRequest, CancellationToken cancellationToken)
{
int authenticatedUserId = AuthenticationHelper.GetUserId(HttpContext);
int authenticationLevel = AuthenticationHelper.GetUserAuthenticationLevel(HttpContext);
Expand All @@ -206,7 +207,7 @@ public async Task<ActionResult<RightsDelegationResponseExternal>> Delegation([Fr
DelegationLookup rightsDelegationRequestInternal = _mapper.Map<DelegationLookup>(rightsDelegationRequest);
rightsDelegationRequestInternal.From = reportee.SingleToList();

DelegationActionResult delegationResultInternal = await _rights.DelegateRights(authenticatedUserId, authenticationLevel, rightsDelegationRequestInternal);
DelegationActionResult delegationResultInternal = await _rights.DelegateRights(authenticatedUserId, authenticationLevel, rightsDelegationRequestInternal, cancellationToken);
if (!delegationResultInternal.IsValid)
{
foreach (var error in delegationResultInternal.Errors)
Expand Down Expand Up @@ -418,4 +419,4 @@ public async Task<IActionResult> ClearAccessCache([FromRoute] int party, [FromBo
return Ok();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1632,4 +1632,4 @@ private static StreamContent GetRightsDelegationContent(string resourceId, strin
return content;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
builder.ConfigureLogging((ctx, logging) =>
{
logging.ClearProviders();
logging.AddConsole();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public AltinnRolesClientMock()
}

/// <inheritdoc/>
public async Task<List<Role>> GetDecisionPointRolesForUser(int coveredByUserId, int offeredByPartyId, CancellationToken cancellationToken)
public async Task<List<Role>> GetDecisionPointRolesForUser(int coveredByUserId, int offeredByPartyId, CancellationToken cancellationToken = default)
{
List<Role> roles = new List<Role>();
string rolesPath = GetRolesPath(coveredByUserId, offeredByPartyId);
Expand All @@ -40,7 +40,7 @@ public async Task<List<Role>> GetDecisionPointRolesForUser(int coveredByUserId,
}

/// <inheritdoc/>
public async Task<List<Role>> GetRolesForDelegation(int coveredByUserId, int offeredByPartyId, CancellationToken cancellationToken)
public async Task<List<Role>> GetRolesForDelegation(int coveredByUserId, int offeredByPartyId, CancellationToken cancellationToken = default)
{
List<Role> roles = new List<Role>();
string rolesPath = GetRolesForDelegationPath(coveredByUserId, offeredByPartyId);
Expand All @@ -54,12 +54,12 @@ public async Task<List<Role>> GetRolesForDelegation(int coveredByUserId, int off
}

/// <inheritdoc/>
public async Task<List<AuthorizedParty>> GetAuthorizedPartiesWithRoles(int userId, CancellationToken cancellationToken)
public async Task<List<AuthorizedParty>> GetAuthorizedPartiesWithRoles(int userId, CancellationToken cancellationToken = default)
{
string authorizedPartiesPath = GetAltinn2AuthorizedPartiesWithRolesPath(userId);
if (File.Exists(authorizedPartiesPath))
{
string content = await File.ReadAllTextAsync(authorizedPartiesPath, cancellationToken);
string content = await File.ReadAllTextAsync(authorizedPartiesPath);
List<SblAuthorizedParty> bridgeAuthParties = (List<SblAuthorizedParty>)JsonSerializer.Deserialize(content, typeof(List<SblAuthorizedParty>), jsonOptions);
return bridgeAuthParties.Select(sblAuthorizedParty => new AuthorizedParty(sblAuthorizedParty)).ToList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,4 @@ private Instance GetTestInstance(string instanceId)
}

#pragma warning restore SA1600 // ElementsMustBeDocumented
}
}
6 changes: 4 additions & 2 deletions test/Altinn.AccessManagement.Tests/Mocks/PolicyFactoryMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ namespace Altinn.AccessManagement.Tests.Mocks;
/// <inheritdoc/>
public class PolicyFactoryMock(ILogger<PolicyRepositoryMock> logger) : IPolicyFactory
{
private ILogger<PolicyRepositoryMock> Logger { get; } = logger;

/// <inheritdoc/>
public IPolicyRepository Create(PolicyAccountType account, string filepath)
{
return new PolicyRepositoryMock(filepath, logger);
return new PolicyRepositoryMock(filepath, Logger);
}

/// <inheritdoc/>
public IPolicyRepository Create(string filepath)
{
return new PolicyRepositoryMock(filepath, logger);
return new PolicyRepositoryMock(filepath, Logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public Task<bool> PolicyExistsAsync(CancellationToken cancellationToken = defaul
private static string GetDataOutputBlobPath()
{
return Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Data", "blobs", "output");

}

private static string GetDataInputBlobPath()
Expand Down Expand Up @@ -140,4 +139,4 @@ private static async Task<Response<BlobContentInfo>> WriteStreamToTestDataFolder
return mockResponse.Object;
}
}
}
}
8 changes: 1 addition & 7 deletions test/Altinn.AccessManagement.Tests/Utils/TestDataUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,5 @@ private static string GetPartiesPath()
string unitTestFolder = Path.GetDirectoryName(new Uri(typeof(PartiesClientMock).Assembly.Location).LocalPath);
return Path.Combine(unitTestFolder, "Data", "Parties");
}

private static string GetResourcesPath()
{
string unitTestFolder = Path.GetDirectoryName(new Uri(typeof(DelegationsControllerTest).Assembly.Location).LocalPath);
return Path.Combine(unitTestFolder, "..", "..", "..", "Data", "Resources");
}
}
}
}

0 comments on commit d1ba126

Please sign in to comment.