generated from fossapps/Micro.Starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from fossapps/directive
feat(directive): add require permission directive
- Loading branch information
Showing
6 changed files
with
94 additions
and
2 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
11 changes: 11 additions & 0 deletions
11
Micro.Auth.Api/GraphQL/Directives/Exceptions/NotAuthenticatedException.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,11 @@ | ||
using System; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Directives.Exceptions | ||
{ | ||
public class NotAuthenticatedException : Exception | ||
{ | ||
public NotAuthenticatedException() : base("This operation requires logging in") | ||
{ | ||
} | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
Micro.Auth.Api/GraphQL/Directives/RequirePermissionDirective.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,52 @@ | ||
using GraphQL.Resolvers; | ||
using GraphQL.Types; | ||
using GraphQL.Utilities; | ||
using Micro.Auth.Api.GraphQL.Directives.Exceptions; | ||
using Micro.Auth.Api.GraphQL.Directives.Extensions; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Directives | ||
{ | ||
public class RequirePermissionDirective : DirectiveGraphType | ||
{ | ||
public const string DirectiveName = "requirePermission"; | ||
|
||
public RequirePermissionDirective() : base( | ||
DirectiveName, | ||
DirectiveLocation.Field, | ||
DirectiveLocation.Mutation, | ||
DirectiveLocation.Query, | ||
DirectiveLocation.FieldDefinition) | ||
{ | ||
} | ||
} | ||
public class RequirePermissionDirectiveVisitor : BaseSchemaNodeVisitor | ||
{ | ||
private readonly IHttpContextAccessor _contextAccessor; | ||
|
||
public RequirePermissionDirectiveVisitor(IHttpContextAccessor contextAccessor) | ||
{ | ||
_contextAccessor = contextAccessor; | ||
} | ||
public override void VisitObjectFieldDefinition(FieldType field, IObjectGraphType type, ISchema schema) | ||
{ | ||
var permission = field.GetAppliedPermission(); | ||
if (permission == null) | ||
{ | ||
return; | ||
} | ||
|
||
var isAuthorized = _contextAccessor | ||
.HttpContext | ||
?.User | ||
.HasClaim(x => x.Type == "Permission" && x.Value == permission); | ||
|
||
if (isAuthorized == true) | ||
{ | ||
return; | ||
} | ||
|
||
field.Resolver = new AsyncFieldResolver<object>(async context => throw new NotAuthorizedException()); | ||
} | ||
} | ||
} |