Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Altinn.AccessManagement.Tests.Mocks
internal class PdpDenyMock : IPDP
{
/// <inheritdoc/>
public Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest)
public Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest, CancellationToken cancellationToken = default)
{
var response = new XacmlJsonResponse
{
Expand All @@ -21,7 +21,7 @@ public Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJ
}

/// <inheritdoc/>
public Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user)
public Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user, CancellationToken cancellationToken = default)
{
return Task.FromResult(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Altinn.AccessManagement.Tests.Mocks;
public class PdpPermitMock: IPDP
{
/// <inheritdoc/>
public Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest)
public Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest, CancellationToken cancellationToken = default)
{
var response = new XacmlJsonResponse
{
Expand All @@ -23,7 +23,7 @@ public Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJ
}

/// <inheritdoc/>
public Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user)
public Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user, CancellationToken cancellationToken = default)
{
return Task.FromResult(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class PepWithPDPAuthorizationMock : IPDP
private const string PartyAttributeId = "urn:altinn:partyid";

/// <inheritdoc />
public async Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest)
public async Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest, CancellationToken cancellationToken = default)
{
return await Authorize(xacmlJsonRequest.Request);
}
Expand Down Expand Up @@ -125,9 +125,9 @@ private async Task<XacmlContextResponse> Authorize(XacmlContextRequest decisionR
}

/// <inheritdoc/>
public async Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user)
public async Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user, CancellationToken cancellationToken = default)
{
XacmlJsonResponse response = await GetDecisionForRequest(xacmlJsonRequest);
XacmlJsonResponse response = await GetDecisionForRequest(xacmlJsonRequest, cancellationToken);
return DecisionHelper.ValidatePdpDecision(response.Response, user);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext

XacmlJsonRequestRoot request = DecisionHelper.CreateDecisionRequest(context, requirement, _httpContextAccessor.HttpContext.GetRouteData());

XacmlJsonResponse response = await _pdp.GetDecisionForRequest(request);
XacmlJsonResponse response = await _pdp.GetDecisionForRequest(request, httpContext.RequestAborted);

if (response?.Response == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext

XacmlJsonRequestRoot request = DecisionHelper.CreateDecisionRequest(context, requirement, _httpContextAccessor.HttpContext.GetRouteData(), _httpContextAccessor.HttpContext.Request.Headers);

XacmlJsonResponse response = await _pdp.GetDecisionForRequest(request);
XacmlJsonResponse response = await _pdp.GetDecisionForRequest(request, httpContext.RequestAborted);

if (response?.Response == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public AuthorizationApiClient(
/// Method for performing authorization.
/// </summary>
/// <param name="xacmlJsonRequest">An authorization request.</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>The result of the authorization request.</returns>
public async Task<XacmlJsonResponse> AuthorizeRequest(XacmlJsonRequestRoot xacmlJsonRequest)
public async Task<XacmlJsonResponse> AuthorizeRequest(XacmlJsonRequestRoot xacmlJsonRequest, CancellationToken cancellationToken = default)
{
XacmlJsonResponse xacmlJsonResponse = null;
string apiUrl = $"decision";
Expand All @@ -66,19 +67,19 @@ public async Task<XacmlJsonResponse> AuthorizeRequest(XacmlJsonRequestRoot xacml

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
HttpResponseMessage response = await _httpClient.PostAsync(apiUrl, httpContent);
HttpResponseMessage response = await _httpClient.PostAsync(apiUrl, httpContent, cancellationToken);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
_logger.LogInformation("Authorization PDP time elapsed: " + ts.TotalMilliseconds);

if (response.StatusCode == HttpStatusCode.OK)
{
xacmlJsonResponse = await response.Content.ReadFromJsonAsync<XacmlJsonResponse>(jsonOptions);
xacmlJsonResponse = await response.Content.ReadFromJsonAsync<XacmlJsonResponse>(jsonOptions, cancellationToken);
}
else
{
_logger.LogInformation($"// PDPAppSI // GetDecisionForRequest // Non-zero status code: {response.StatusCode}");
_logger.LogInformation($"// PDPAppSI // GetDecisionForRequest // Response: {await response.Content.ReadAsStringAsync()}");
_logger.LogInformation($"// PDPAppSI // GetDecisionForRequest // Response: {await response.Content.ReadAsStringAsync(cancellationToken)}");
}

return xacmlJsonResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public PDPAppSI(ILogger<PDPAppSI> logger, AuthorizationApiClient authorizationAp
}

/// <inheritdoc/>
public async Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest)
public async Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest, CancellationToken cancellationToken = default)
{
XacmlJsonResponse xacmlJsonResponse = null;

try
{
xacmlJsonResponse = await _authorizationApiClient.AuthorizeRequest(xacmlJsonRequest);
xacmlJsonResponse = await _authorizationApiClient.AuthorizeRequest(xacmlJsonRequest, cancellationToken);
}
catch (Exception e)
{
Expand All @@ -45,9 +45,9 @@ public async Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot
}

/// <inheritdoc/>
public async Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user)
public async Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user, CancellationToken cancellationToken = default)
{
XacmlJsonResponse response = await GetDecisionForRequest(xacmlJsonRequest);
XacmlJsonResponse response = await GetDecisionForRequest(xacmlJsonRequest, cancellationToken);

if (response?.Response == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ public interface IPDP
/// Sends in a request and get response with result of the request
/// </summary>
/// <param name="xacmlJsonRequest">The Xacml Json Request</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>The Xacml Json response contains the result of the request</returns>
Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest);
Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest, CancellationToken cancellationToken = default);

/// <summary>
/// Change this to a better one???????
/// </summary>
/// <param name="xacmlJsonRequest">The Xacml Json Request</param>
/// <param name="user">The claims principal</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Returns true if request is permitted and false if not</returns>
Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user);
Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
AuthorizationHandlerContext context = CreateAuthorizationHandlerContext();
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));

// Act
await _aah.HandleAsync(context);
Expand All @@ -62,7 +62,7 @@
AuthorizationHandlerContext context = CreateAuthorizationHandlerContext();
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Deny.ToString());
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));

// Act
await _aah.HandleAsync(context);
Expand All @@ -86,7 +86,7 @@

// Add extra result
response.Response.Add(new XacmlJsonResult());
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));

// Act
await _aah.HandleAsync(context);
Expand All @@ -108,7 +108,7 @@
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
AddObligationWithMinAuthLv(response, "2");
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));

// Act
await _aah.HandleAsync(context);
Expand All @@ -130,7 +130,7 @@
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
AddObligationWithMinAuthLv(response, "3");
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));

// Act
await _aah.HandleAsync(context);
Expand All @@ -151,7 +151,7 @@
AuthorizationHandlerContext context = CreateAuthorizationHandlerContext();
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
XacmlJsonResponse response = null;
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));

// Act & Assert
await Assert.ThrowsAsync<ArgumentNullException>(() => _aah.HandleAsync(context));
Expand All @@ -171,7 +171,7 @@
// Create response with a result list that is null
XacmlJsonResponse response = new XacmlJsonResponse();
response.Response = null;
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));

// Act & Assert
await Assert.ThrowsAsync<ArgumentNullException>(() => _aah.HandleAsync(context));
Expand All @@ -192,7 +192,7 @@
AddObligationWithMinAuthLv(response, "2");

// verify
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));

// Act
await _aah.HandleAsync(context);
Expand All @@ -209,7 +209,7 @@
AuthorizationHandlerContext context = CreateAuthorizationHandlerContextSystemUser();
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));

// Act
await _aah.HandleAsync(context);
Expand All @@ -230,7 +230,7 @@
AuthorizationHandlerContext context = CreateAuthorizationHandlerContextAppUser("app_skd_flyttemelding");
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));

// Act
await _aah.HandleAsync(context);
Expand Down Expand Up @@ -299,7 +299,7 @@

if (!string.IsNullOrEmpty(xForwardedForHeader))
{
httpContext.Request.Headers.Add("x-forwarded-for", xForwardedForHeader);

Check warning on line 302 in src/pkgs/Altinn.Authorization.PEP/test/Altinn.Authorization.PEP.Tests/AppAccessHandlerTest.cs

View workflow job for this annotation

GitHub Actions / ci (pkg: PEP) / Build and Test

Use IHeaderDictionary.Append or the indexer to append or set headers. IDictionary.Add will throw an ArgumentException when attempting to add a duplicate key. (https://aka.ms/aspnet/analyzers)

Check warning on line 302 in src/pkgs/Altinn.Authorization.PEP/test/Altinn.Authorization.PEP.Tests/AppAccessHandlerTest.cs

View workflow job for this annotation

GitHub Actions / ci (pkg: PEP) / Build and Test

Use IHeaderDictionary.Append or the indexer to append or set headers. IDictionary.Add will throw an ArgumentException when attempting to add a duplicate key. (https://aka.ms/aspnet/analyzers)
}

return httpContext;
Expand Down
Loading
Loading