-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VCST-1985: Use only token authentication for graphql (#20)
- Loading branch information
1 parent
d35d32d
commit 82be438
Showing
7 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace VirtoCommerce.Xapi.Core.Models; | ||
|
||
public class GraphQLOptions | ||
{ | ||
public IList<string> ForbiddenAuthenticationTypes { get; set; } = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/VirtoCommerce.Xapi.Data/Extensions/ApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using VirtoCommerce.Xapi.Core; | ||
using VirtoCommerce.Xapi.Data.Security; | ||
|
||
namespace VirtoCommerce.Xapi.Data.Extensions; | ||
public static class ApplicationBuilderExtensions | ||
{ | ||
public static IApplicationBuilder UseAuthenticationFilter(this IApplicationBuilder applicationBuilder) | ||
{ | ||
return applicationBuilder.UseWhen(httpContext => httpContext.Request.Path.StartsWithSegments(ModuleConstants.GraphQlPath), builder => | ||
{ | ||
builder.UseMiddleware<AuthenticationFilterMiddleware>(); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/VirtoCommerce.Xapi.Data/Security/AuthenticationFilterMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using VirtoCommerce.Xapi.Core.Models; | ||
|
||
namespace VirtoCommerce.Xapi.Data.Security; | ||
|
||
public class AuthenticationFilterMiddleware : IMiddleware | ||
{ | ||
private readonly GraphQLOptions _options; | ||
private readonly ILogger<AuthenticationFilterMiddleware> _logger; | ||
|
||
public AuthenticationFilterMiddleware(IOptions<GraphQLOptions> optionsAccessor, ILogger<AuthenticationFilterMiddleware> logger) | ||
{ | ||
_logger = logger; | ||
_options = optionsAccessor.Value; | ||
} | ||
|
||
public Task InvokeAsync(HttpContext context, RequestDelegate next) | ||
{ | ||
_logger.LogDebug("{Method} {Path} {AuthenticationType}", | ||
context.Request.Method, context.Request.Path, string.Join(", ", context.User.Identities.Select(x => x.AuthenticationType + " " + x.Name))); | ||
|
||
// Remove identities with forbidden authenticated types | ||
var identities = context.User.Identities | ||
.Where(x => !_options.ForbiddenAuthenticationTypes.Contains(x.AuthenticationType)) | ||
.ToList(); | ||
|
||
if (identities.Count == 0) | ||
{ | ||
identities.Add(new ClaimsIdentity()); | ||
} | ||
|
||
context.User = new ClaimsPrincipal(identities); | ||
|
||
return next.Invoke(context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters