generated from fossapps/Micro.Starter
-
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.
feat(security): add config to disallow parts of schema (#48)
- Loading branch information
Showing
10 changed files
with
99 additions
and
6 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,64 @@ | ||
using System.Reflection; | ||
using API.Configs; | ||
using Business.Exceptions; | ||
using HotChocolate.Resolvers; | ||
using HotChocolate.Types.Descriptors; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace API.Attributes; | ||
|
||
public class ProtectedAttribute : ObjectFieldDescriptorAttribute | ||
{ | ||
private readonly string _operation; | ||
|
||
public ProtectedAttribute(string operation) | ||
{ | ||
_operation = operation; | ||
} | ||
|
||
public override void OnConfigure(IDescriptorContext context, IObjectFieldDescriptor descriptor, MemberInfo member) | ||
{ | ||
descriptor.Use(next => ctx => | ||
{ | ||
var operationRule = GetSecurityRequirements(ctx); | ||
if (operationRule == null) | ||
{ | ||
return next.Invoke(ctx); | ||
} | ||
var headerValue = GetHeaderValue(ctx, operationRule); | ||
if (headerValue == null || headerValue != operationRule.Value) | ||
{ | ||
throw new OperationNotPermitted("you do not have access to perform this operation"); | ||
} | ||
return next.Invoke(ctx); | ||
}); | ||
} | ||
|
||
private string? GetHeaderValue(IMiddlewareContext ctx, SecurityRequirements rule) | ||
{ | ||
ctx.ContextData.TryGetValue("HttpContext", out var ctxData); | ||
if (ctxData == null || ctxData.GetType() != typeof(DefaultHttpContext)) | ||
{ | ||
throw new ApplicationException("http context not configured correctly"); | ||
} | ||
|
||
var httpContext = (DefaultHttpContext) ctxData; | ||
if (!httpContext.Request.Headers.TryGetValue(rule.Header, out var headerValue)) | ||
{ | ||
return null; | ||
} | ||
|
||
return headerValue; | ||
} | ||
private SecurityRequirements? GetSecurityRequirements(IMiddlewareContext ctx) | ||
{ | ||
var securityOptions = ctx.Service<IOptions<Security>>().Value; | ||
if (securityOptions == null || securityOptions.Rules == null) | ||
{ | ||
return null; | ||
} | ||
securityOptions.Rules.TryGetValue(_operation, out var securityRequirements); | ||
return securityRequirements; | ||
} | ||
} |
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,12 @@ | ||
namespace API.Configs; | ||
|
||
public class Security | ||
{ | ||
public Dictionary<string, SecurityRequirements>? Rules { set; get; } | ||
} | ||
|
||
public class SecurityRequirements | ||
{ | ||
public string Header { set; get; } | ||
public string Value { set; get; } | ||
} |
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
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 @@ | ||
namespace Business.Exceptions; | ||
|
||
public class OperationNotPermitted : Exception | ||
{ | ||
public OperationNotPermitted(string? message) : base(message) | ||
{ | ||
} | ||
} |
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
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