Skip to content

Commit f536888

Browse files
Middleware Logging (#129)
* Fixed an issue where some security middleware components would log erroneous messages when they were skipped and performed no action.
1 parent 6dab282 commit f536888

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/graphql-aspnet/Middleware/SchemaItemSecurity/Components/SchemaItemAuthenticationMiddleware.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// --
77
// License: MIT
88
// *************************************************************
9+
910
namespace GraphQL.AspNet.Middleware.SchemaItemSecurity.Components
1011
{
1112
using System;
@@ -47,22 +48,25 @@ public SchemaItemAuthenticationMiddleware(IAuthenticationSchemeProvider schemePr
4748
/// <inheritdoc />
4849
public async Task InvokeAsync(SchemaItemSecurityChallengeContext context, GraphMiddlewareInvocationDelegate<SchemaItemSecurityChallengeContext> next, CancellationToken cancelToken = default)
4950
{
50-
context.Logger?.SchemaItemAuthenticationChallenge(context);
51-
5251
// only attempt an authentication
5352
// if no result is already deteremined and if no user has already been authenticated
54-
IAuthenticationResult authenticationResult = null;
53+
//
54+
// if a piece of middleware has already set an authenticated user
55+
// just skip this component.
5556
if (context.Result == null && context.AuthenticatedUser == null)
5657
{
58+
context.Logger?.SchemaItemAuthenticationChallenge(context);
59+
IAuthenticationResult authenticationResult = null;
60+
5761
ClaimsPrincipal user;
5862
SchemaItemSecurityChallengeResult challengeResult;
5963

6064
(user, authenticationResult, challengeResult) = await this.AuthenticateUser(context, cancelToken);
6165
context.AuthenticatedUser = user;
6266
context.Result = challengeResult;
63-
}
6467

65-
context.Logger?.SchemaItemAuthenticationChallengeResult(context, authenticationResult);
68+
context.Logger?.SchemaItemAuthenticationChallengeResult(context, authenticationResult);
69+
}
6670

6771
await next.Invoke(context, cancelToken).ConfigureAwait(false);
6872
}

src/graphql-aspnet/Middleware/SchemaItemSecurity/Components/SchemaItemAuthorizationMiddleware.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ public SchemaItemAuthorizationMiddleware(IAuthorizationService authService = nul
4545
/// <returns>Task.</returns>
4646
public async Task InvokeAsync(SchemaItemSecurityChallengeContext context, GraphMiddlewareInvocationDelegate<SchemaItemSecurityChallengeContext> next, CancellationToken cancelToken = default)
4747
{
48-
context.Logger?.SchemaItemAuthorizationChallenge(context);
49-
5048
// a result may have been set by other middleware
5149
// in this auth pipeline, if a result is already determined just skip this step
5250
if (context.Result == null)
5351
{
52+
context.Logger?.SchemaItemAuthorizationChallenge(context);
53+
5454
var result = await this.AuthorizeRequestAsync(context).ConfigureAwait(false);
5555
context.Result = result ?? SchemaItemSecurityChallengeResult.Default();
56-
}
5756

58-
context.Logger?.SchemaItemAuthorizationChallengeResult(context);
57+
context.Logger?.SchemaItemAuthorizationChallengeResult(context);
58+
}
5959

6060
await next(context, cancelToken).ConfigureAwait(false);
6161
}

0 commit comments

Comments
 (0)