Skip to content

Commit 2d81047

Browse files
committed
refactoring
1 parent a49f413 commit 2d81047

6 files changed

Lines changed: 30 additions & 23 deletions

File tree

durablefunctionsmonitor.dotnetbackend/Common/Auth.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static bool IsNonceSetAndValid(IHeaderDictionary headers)
9292
/// <returns><see cref="DfmMode"/> value for current request (so that it can be returned to the client) </returns>
9393
/// <exception cref="AccessViolationException"></exception>
9494
/// <exception cref="UnauthorizedAccessException"></exception>
95-
public static async Task<DfmMode> ValidateIdentityAsync(ClaimsPrincipal principal, IHeaderDictionary headers, IRequestCookieCollection cookies, string taskHubName, OperationKind operationKind)
95+
public static async Task<DfmMode> ValidateIdentityAsync(ClaimsPrincipal principal, IHeaderDictionary headers, IRequestCookieCollection cookies, OperationKind operationKind)
9696
{
9797
// Checking if the endpoint is in ReadOnly mode
9898
if (operationKind != OperationKind.Read && DfmEndpoint.Settings.Mode == DfmMode.ReadOnly)

durablefunctionsmonitor.dotnetbackend/Common/Globals.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ public static async Task<IActionResult> HandleAuthAndErrors(this HttpRequest req
118118
{
119119
return await HandleErrors(req, log, async () =>
120120
{
121-
var taskHubName = CombineConnNameAndHubName(connName, hubName);
122-
var mode = await Auth.ValidateIdentityAsync(req.HttpContext.User, req.Headers, req.Cookies, taskHubName, kind);
123-
await Auth.ThrowIfTaskHubNameIsInvalid(taskHubName);
121+
var mode = await Auth.ValidateIdentityAsync(req.HttpContext.User, req.Headers, req.Cookies, kind);
122+
123+
// Also validating task hub name (if it is a part of the request).
124+
// But only after validating user identity (because validating task hub name involves querying the Storage).
125+
await Auth.ThrowIfTaskHubNameIsInvalid(CombineConnNameAndHubName(connName, hubName));
124126

125127
return await todo(mode);
126128
});

durablefunctionsmonitor.dotnetbackend/Common/HttpHandlerBase.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ protected async Task<IActionResult> HandleAuthAndErrors(OperationKind kind, IDur
2929
{
3030
return await Globals.HandleErrors(req, log, async () => {
3131

32-
var taskHubName = Globals.CombineConnNameAndHubName(connName, hubName);
33-
await Auth.ValidateIdentityAsync(req.HttpContext.User, req.Headers, req.Cookies, taskHubName, kind);
34-
await Auth.ThrowIfTaskHubNameIsInvalid(taskHubName);
32+
await Auth.ValidateIdentityAsync(req.HttpContext.User, req.Headers, req.Cookies, kind);
33+
34+
// Also validating task hub name (if it is a part of the request).
35+
// But only after validating user identity (because validating task hub name involves querying the Storage).
36+
await Auth.ThrowIfTaskHubNameIsInvalid(Globals.CombineConnNameAndHubName(connName, hubName));
3537

3638
// For default storage connections using default durableClient, injected normally, as a parameter.
3739
// Only using IDurableClientFactory for custom connections, just in case.

durablefunctionsmonitor.dotnetisolated.core/Common/Auth.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static bool IsNonceSetAndValid(DfmSettings settings, HttpHeadersCollectio
9191
/// <returns><see cref="DfmMode"/> value for current request (so that it can be returned to the calling code) </returns>
9292
/// <exception cref="DfmAccessViolationException"></exception>
9393
/// <exception cref="DfmUnauthorizedException"></exception>
94-
public static async Task<DfmMode> ValidateIdentityAsync(HttpRequestData request, OperationKind operationKind, DfmSettings settings, DfmExtensionPoints extensionPoints)
94+
public static async Task<DfmMode> ValidateIdentityAsync(HttpRequestData request, OperationKind operationKind, DfmSettings settings)
9595
{
9696
// Checking if the endpoint is in ReadOnly mode
9797
if (operationKind != OperationKind.Read && settings.Mode == DfmMode.ReadOnly)

durablefunctionsmonitor.dotnetisolated.core/Common/ExtensionMethods.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ public static IFunctionsWorkerApplicationBuilder UseDurableFunctionsMonitor(
9898
if (operationKind.HasValue)
9999
{
100100
// If so, invoking DfMon's auth logic
101-
var dfmMode = await Auth.ValidateIdentityAsync(request, operationKind.Value, settings, extensionPoints);
101+
var dfmMode = await Auth.ValidateIdentityAsync(request, operationKind.Value, settings);
102+
103+
// Also validating task hub name (if it is a part of the request).
104+
// But only after validating user identity (because validating task hub name involves querying the Storage).
102105
await Auth.ThrowIfUriTaskHubNameIsInvalid(request.Url.AbsolutePath, extensionPoints);
103106

104107
// Propagating DfmMode to Functions

tests/durablefunctionsmonitor.dotnetisolated.core.tests/AuthTests.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public async Task RespectsTaskHubNameFromHostJson()
189189
await File.WriteAllTextAsync("../host.json", $"{{\"extensions\":{{\"durableTask\": {{\"hubName\": \"{hubName}\"}}}}}}");
190190

191191
// Act
192-
var task = Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings(), new DfmExtensionPoints());
192+
var task = Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings());
193193

194194
// Assert
195195

@@ -222,7 +222,7 @@ public async Task RespectsTaskHubNameEnvVariableFromHostJson()
222222

223223
// Act
224224

225-
var task = Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings(), new DfmExtensionPoints());
225+
var task = Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings());
226226

227227
// Assert
228228

@@ -256,7 +256,7 @@ public void ReturnsUnauthorizedResultIfUserNotWhitelisted()
256256
}, "tino-test-auth-type"));
257257

258258
// Act
259-
var task = Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings(), new DfmExtensionPoints());
259+
var task = Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings());
260260

261261
// Assert
262262
Assert.IsInstanceOfType(task.Exception.InnerException, typeof(DfmUnauthorizedException));
@@ -294,7 +294,7 @@ public void ReturnsUnauthorizedResultIfUserIsNotInAppRole(string appRoles, strin
294294

295295
// Act
296296

297-
var task = Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings(), new DfmExtensionPoints());
297+
var task = Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings());
298298

299299
// Assert
300300

@@ -336,7 +336,7 @@ public async Task ReturnsAuthorizedIfUserIsInAppRole(string appRoles, string ful
336336

337337
// Act
338338

339-
var result = await Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings(), new DfmExtensionPoints());
339+
var result = await Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings());
340340

341341
// Assert
342342

@@ -400,7 +400,7 @@ public async Task ValidatesTokenWithoutEasyAuthsHelp()
400400
request.Headers.Add("Authorization", "Bearer " + token);
401401

402402
// Act
403-
var result = await Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings(), new DfmExtensionPoints());
403+
var result = await Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings());
404404

405405
// Assert
406406
Assert.AreEqual(DfmMode.Normal, result);
@@ -431,13 +431,13 @@ public async Task LoadsListOfTablesFromTableStorage()
431431

432432
// Act
433433

434-
// Now initializing TableClient
435-
TableClient.MockedTableClient = tableClientMoq.Object;
436-
437-
var task = Auth.ThrowIfUriTaskHubNameIsInvalid(request.Url.ToString(), new DfmExtensionPoints());
438-
Thread.Sleep(100);
439-
task = Auth.ThrowIfUriTaskHubNameIsInvalid(request.Url.ToString(), new DfmExtensionPoints());
440-
434+
// Now initializing TableClient
435+
TableClient.MockedTableClient = tableClientMoq.Object;
436+
437+
var task = Auth.ThrowIfUriTaskHubNameIsInvalid(request.Url.ToString(), new DfmExtensionPoints());
438+
Thread.Sleep(100);
439+
task = Auth.ThrowIfUriTaskHubNameIsInvalid(request.Url.ToString(), new DfmExtensionPoints());
440+
441441
TableClient.MockedTableClient = null;
442442

443443
Assert.IsInstanceOfType(task.Exception.InnerException, typeof(DfmUnauthorizedException));
@@ -480,7 +480,7 @@ public void LoadsListOfTablesFromAlternativeStorage()
480480

481481
// Act
482482

483-
var task = Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings(), new DfmExtensionPoints());
483+
var task = Auth.ValidateIdentityAsync(request, OperationKind.Read, new DfmSettings());
484484

485485
// Assert
486486

0 commit comments

Comments
 (0)