Skip to content

Commit 18f9fc0

Browse files
committed
chore: add cancellation tokens to pep
1 parent c893975 commit 18f9fc0

File tree

7 files changed

+32
-29
lines changed

7 files changed

+32
-29
lines changed

src/pkgs/Altinn.Authorization.PEP/src/Altinn.Authorization.PEP/Authorization/AppAccessHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext
4949

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

52-
XacmlJsonResponse response = await _pdp.GetDecisionForRequest(request);
52+
XacmlJsonResponse response = await _pdp.GetDecisionForRequest(request, httpContext.RequestAborted);
5353

5454
if (response?.Response == null)
5555
{

src/pkgs/Altinn.Authorization.PEP/src/Altinn.Authorization.PEP/Authorization/ResourceAccessHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext
5151

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

54-
XacmlJsonResponse response = await _pdp.GetDecisionForRequest(request);
54+
XacmlJsonResponse response = await _pdp.GetDecisionForRequest(request, httpContext.RequestAborted);
5555

5656
if (response?.Response == null)
5757
{

src/pkgs/Altinn.Authorization.PEP/src/Altinn.Authorization.PEP/Clients/AuthorizationApiClient.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ public AuthorizationApiClient(
5656
/// Method for performing authorization.
5757
/// </summary>
5858
/// <param name="xacmlJsonRequest">An authorization request.</param>
59+
/// <param name="cancellationToken">Cancellation token</param>
5960
/// <returns>The result of the authorization request.</returns>
60-
public async Task<XacmlJsonResponse> AuthorizeRequest(XacmlJsonRequestRoot xacmlJsonRequest)
61+
public async Task<XacmlJsonResponse> AuthorizeRequest(XacmlJsonRequestRoot xacmlJsonRequest, CancellationToken cancellationToken = default)
6162
{
6263
XacmlJsonResponse xacmlJsonResponse = null;
6364
string apiUrl = $"decision";
@@ -66,19 +67,19 @@ public async Task<XacmlJsonResponse> AuthorizeRequest(XacmlJsonRequestRoot xacml
6667

6768
Stopwatch stopWatch = new Stopwatch();
6869
stopWatch.Start();
69-
HttpResponseMessage response = await _httpClient.PostAsync(apiUrl, httpContent);
70+
HttpResponseMessage response = await _httpClient.PostAsync(apiUrl, httpContent, cancellationToken);
7071
stopWatch.Stop();
7172
TimeSpan ts = stopWatch.Elapsed;
7273
_logger.LogInformation("Authorization PDP time elapsed: " + ts.TotalMilliseconds);
7374

7475
if (response.StatusCode == HttpStatusCode.OK)
7576
{
76-
xacmlJsonResponse = await response.Content.ReadFromJsonAsync<XacmlJsonResponse>(jsonOptions);
77+
xacmlJsonResponse = await response.Content.ReadFromJsonAsync<XacmlJsonResponse>(jsonOptions, cancellationToken);
7778
}
7879
else
7980
{
8081
_logger.LogInformation($"// PDPAppSI // GetDecisionForRequest // Non-zero status code: {response.StatusCode}");
81-
_logger.LogInformation($"// PDPAppSI // GetDecisionForRequest // Response: {await response.Content.ReadAsStringAsync()}");
82+
_logger.LogInformation($"// PDPAppSI // GetDecisionForRequest // Response: {await response.Content.ReadAsStringAsync(cancellationToken)}");
8283
}
8384

8485
return xacmlJsonResponse;

src/pkgs/Altinn.Authorization.PEP/src/Altinn.Authorization.PEP/Implementation/PDPAppSI.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public PDPAppSI(ILogger<PDPAppSI> logger, AuthorizationApiClient authorizationAp
2828
}
2929

3030
/// <inheritdoc/>
31-
public async Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest)
31+
public async Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest, CancellationToken cancellationToken = default)
3232
{
3333
XacmlJsonResponse xacmlJsonResponse = null;
3434

3535
try
3636
{
37-
xacmlJsonResponse = await _authorizationApiClient.AuthorizeRequest(xacmlJsonRequest);
37+
xacmlJsonResponse = await _authorizationApiClient.AuthorizeRequest(xacmlJsonRequest, cancellationToken);
3838
}
3939
catch (Exception e)
4040
{
@@ -45,9 +45,9 @@ public async Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot
4545
}
4646

4747
/// <inheritdoc/>
48-
public async Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user)
48+
public async Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user, CancellationToken cancellationToken = default)
4949
{
50-
XacmlJsonResponse response = await GetDecisionForRequest(xacmlJsonRequest);
50+
XacmlJsonResponse response = await GetDecisionForRequest(xacmlJsonRequest, cancellationToken);
5151

5252
if (response?.Response == null)
5353
{

src/pkgs/Altinn.Authorization.PEP/src/Altinn.Authorization.PEP/Interfaces/IPDP.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ public interface IPDP
1414
/// Sends in a request and get response with result of the request
1515
/// </summary>
1616
/// <param name="xacmlJsonRequest">The Xacml Json Request</param>
17+
/// <param name="cancellationToken">Cancellation token</param>
1718
/// <returns>The Xacml Json response contains the result of the request</returns>
18-
Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest);
19+
Task<XacmlJsonResponse> GetDecisionForRequest(XacmlJsonRequestRoot xacmlJsonRequest, CancellationToken cancellationToken = default);
1920

2021
/// <summary>
2122
/// Change this to a better one???????
2223
/// </summary>
2324
/// <param name="xacmlJsonRequest">The Xacml Json Request</param>
2425
/// <param name="user">The claims principal</param>
26+
/// <param name="cancellationToken">Cancellation token</param>
2527
/// <returns>Returns true if request is permitted and false if not</returns>
26-
Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user);
28+
Task<bool> GetDecisionForUnvalidateRequest(XacmlJsonRequestRoot xacmlJsonRequest, ClaimsPrincipal user, CancellationToken cancellationToken = default);
2729
}
2830
}

src/pkgs/Altinn.Authorization.PEP/test/Altinn.Authorization.PEP.Tests/AppAccessHandlerTest.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public async Task HandleRequirementAsync_TC01Async()
4141
AuthorizationHandlerContext context = CreateAuthorizationHandlerContext();
4242
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
4343
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
44-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
44+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
4545

4646
// Act
4747
await _aah.HandleAsync(context);
@@ -62,7 +62,7 @@ public async Task HandleRequirementAsync_TC02Async()
6262
AuthorizationHandlerContext context = CreateAuthorizationHandlerContext();
6363
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
6464
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Deny.ToString());
65-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
65+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
6666

6767
// Act
6868
await _aah.HandleAsync(context);
@@ -86,7 +86,7 @@ public async Task HandleRequirementAsync_TC03Async()
8686

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

9191
// Act
9292
await _aah.HandleAsync(context);
@@ -108,7 +108,7 @@ public async Task HandleRequirementAsync_TC04Async()
108108
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
109109
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
110110
AddObligationWithMinAuthLv(response, "2");
111-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
111+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
112112

113113
// Act
114114
await _aah.HandleAsync(context);
@@ -130,7 +130,7 @@ public async Task HandleRequirementAsync_TC05Async()
130130
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
131131
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
132132
AddObligationWithMinAuthLv(response, "3");
133-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
133+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
134134

135135
// Act
136136
await _aah.HandleAsync(context);
@@ -151,7 +151,7 @@ public async Task HandleRequirementAsync_TC06Async()
151151
AuthorizationHandlerContext context = CreateAuthorizationHandlerContext();
152152
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
153153
XacmlJsonResponse response = null;
154-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
154+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
155155

156156
// Act & Assert
157157
await Assert.ThrowsAsync<ArgumentNullException>(() => _aah.HandleAsync(context));
@@ -171,7 +171,7 @@ public async Task HandleRequirementAsync_TC07Async()
171171
// Create response with a result list that is null
172172
XacmlJsonResponse response = new XacmlJsonResponse();
173173
response.Response = null;
174-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
174+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
175175

176176
// Act & Assert
177177
await Assert.ThrowsAsync<ArgumentNullException>(() => _aah.HandleAsync(context));
@@ -192,7 +192,7 @@ public async Task HandleRequirementAsync_TC08Async()
192192
AddObligationWithMinAuthLv(response, "2");
193193

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

197197
// Act
198198
await _aah.HandleAsync(context);
@@ -209,7 +209,7 @@ public async Task HandleRequirementAsync_TC09Async()
209209
AuthorizationHandlerContext context = CreateAuthorizationHandlerContextSystemUser();
210210
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
211211
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
212-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
212+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
213213

214214
// Act
215215
await _aah.HandleAsync(context);
@@ -230,7 +230,7 @@ public async Task HandleRequirementAsync_TC10Async()
230230
AuthorizationHandlerContext context = CreateAuthorizationHandlerContextAppUser("app_skd_flyttemelding");
231231
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext());
232232
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
233-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
233+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
234234

235235
// Act
236236
await _aah.HandleAsync(context);

src/pkgs/Altinn.Authorization.PEP/test/Altinn.Authorization.PEP.Tests/ResourceAccessHandlerTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task HandleRequirementAsync_TC01Async()
4444
AuthorizationHandlerContext context = CreateAuthorizationHandlerContext();
4545
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext("23453546", null, null));
4646
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
47-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
47+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
4848

4949
// Act
5050
await _rah.HandleAsync(context);
@@ -65,7 +65,7 @@ public async Task HandleRequirementAsync_TC02Async()
6565
AuthorizationHandlerContext context = CreateAuthorizationHandlerContext();
6666
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext("organization", "991825827", null));
6767
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
68-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
68+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
6969

7070
// Act
7171
await _rah.HandleAsync(context);
@@ -91,7 +91,7 @@ public async Task HandleRequirementAsync_TC03Async()
9191
AuthorizationHandlerContext context = CreateAuthorizationHandlerContext();
9292
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext("organization", "991825827M", null));
9393
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
94-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
94+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
9595

9696
// Act
9797
Task Act() => _rah.HandleAsync(context);
@@ -113,7 +113,7 @@ public async Task HandleRequirementAsync_TC04Async()
113113
AuthorizationHandlerContext context = CreateAuthorizationHandlerContext();
114114
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext("person", null, "01014922047"));
115115
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
116-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
116+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
117117

118118
// Act
119119
await _rah.HandleAsync(context);
@@ -139,7 +139,7 @@ public async Task HandleRequirementAsync_TC05Async()
139139
AuthorizationHandlerContext context = CreateAuthorizationHandlerContext();
140140
_httpContextAccessorMock.Setup(h => h.HttpContext).Returns(CreateHttpContext("person", null, "a01014922047"));
141141
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
142-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>())).Returns(Task.FromResult(response));
142+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.IsAny<XacmlJsonRequestRoot>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
143143

144144
// Act
145145
Task Act() => _rah.HandleAsync(context);
@@ -162,7 +162,7 @@ public async Task HandleRequirementAsync_TC06Async()
162162
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
163163

164164
// Verify
165-
_pdpMock.Setup(a => a.GetDecisionForRequest(It.Is<XacmlJsonRequestRoot>(xr => xr.Request.XForwardedForHeader == null))).Returns(Task.FromResult(response));
165+
_pdpMock.Setup(a => a.GetDecisionForRequest(It.Is<XacmlJsonRequestRoot>(xr => xr.Request.XForwardedForHeader == null), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
166166

167167
// Act
168168
await _rah.HandleAsync(context);
@@ -182,7 +182,7 @@ public async Task HandleRequirementAsync_TC07Async()
182182
XacmlJsonResponse response = CreateResponse(XacmlContextDecision.Permit.ToString());
183183

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

187187
// Act
188188
await _rah.HandleAsync(context);

0 commit comments

Comments
 (0)