Skip to content

Commit

Permalink
Update test matrix + support for >= 4.2 (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
joemcbride authored Apr 27, 2021
1 parent 1c817bf commit 68afa77
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ insert_final_newline = false
[*.cs]
indent_size = 4

[*.txt]
insert_final_newline = false

# Code files
[*.{cs,vb}]

Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ jobs:
- ubuntu-latest
- windows-latest
graphqlversion:
- 4.0.2
- 4.2.0
- 4.4.0
steps:
- name: Checkout source
uses: actions/checkout@v2
- name: Setup .NET Core 3.1 SDK
uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.x'
dotnet-version: "3.1.x"
source-url: https://nuget.pkg.github.com/graphql-dotnet/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Setup .NET Core 5.0 SDK
uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.x'
dotnet-version: "5.0.x"
source-url: https://nuget.pkg.github.com/graphql-dotnet/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ A toolset for authorizing access to graph types for [GraphQL.NET](https://github

Provides the following packages:

| Package | Downloads | NuGet Latest |
|-----------------------|-------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------|
| GraphQL.Authorization | [![Nuget](https://img.shields.io/nuget/dt/GraphQL.Authorization)](https://www.nuget.org/packages/GraphQL.Authorization) | [![Nuget](https://img.shields.io/nuget/v/GraphQL.Authorization)](https://www.nuget.org/packages/GraphQL.Authorization) |
| Package | Downloads | NuGet Latest |
| --------------------- | ----------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| GraphQL.Authorization | [![Nuget](https://img.shields.io/nuget/dt/GraphQL.Authorization)](https://www.nuget.org/packages/GraphQL.Authorization) | [![Nuget](https://img.shields.io/nuget/v/GraphQL.Authorization)](https://www.nuget.org/packages/GraphQL.Authorization) |

You can get all preview versions from [GitHub Packages](https://github.com/orgs/graphql-dotnet/packages?repo_name=authorization).
Note that GitHub requires authentication to consume the feed. See [here](https://docs.github.com/en/free-pro-team@latest/packages/publishing-and-managing-packages/about-github-packages#authenticating-to-github-packages).

# Usage

* Register the authorization classes in your DI container - `IAuthorizationEvaluator`, `AuthorizationSettings`, and the `AuthorizationValidationRule`.
* Provide a custom `UserContext` class that implements `IProvideClaimsPrincipal`.
* Add policies to the `AuthorizationSettings`.
* Apply a policy to a GraphType or Field (both implement `IProvideMetadata`):
- Register the authorization classes in your DI container - `IAuthorizationEvaluator`, `AuthorizationSettings`, and the `AuthorizationValidationRule`.
- Provide a custom `UserContext` class that implements `IProvideClaimsPrincipal`.
- Add policies to the `AuthorizationSettings`.
- Apply a policy to a GraphType or Field (both implement `IProvideMetadata`):
- using `AuthorizeWith(string policy)` extension method
- or with `GraphQLAuthorize` attribute if using Schema + Handler syntax.
* The `AuthorizationValidationRule` will run and verify the policies based on the registered policies.
* You can write your own `IAuthorizationRequirement`.
- The `AuthorizationValidationRule` will run and verify the policies based on the registered policies.
- You can write your own `IAuthorizationRequirement`.

# Examples

Expand All @@ -61,20 +61,20 @@ public class MyType : ObjectGraphType
4. Schema first syntax - use `GraphQLAuthorize` attribute on type, method or property.

```csharp
[GraphQLAuthorize(Policy = "MyPolicy")]
[GraphQLAuthorize("MyPolicy")]
public class MutationType
{
[GraphQLAuthorize(Policy = "AnotherPolicy")]
[GraphQLAuthorize("AnotherPolicy")]
public async Task<string> CreateSomething(MyInput input)
{
return await SomeMethodAsync(input);
}

[GraphQLAuthorize(Policy = "SuperPolicy")]
[GraphQLAuthorize("SuperPolicy")]
public string SomeProperty => Guid.NewGuid().ToString();
}
```

# Known Issues

* It is currently not possible to add a policy to Input objects using Schema first approach.
- It is currently not possible to add a policy to Input objects using Schema first approach.
2 changes: 1 addition & 1 deletion src/BasicSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class Query
/// <summary>
/// Resolver for 'Query.viewer' field.
/// </summary>
[GraphQLAuthorize(Policy = "AdminPolicy")]
[GraphQLAuthorize("AdminPolicy")]
public User Viewer() => new User { Id = Guid.NewGuid().ToString(), Name = "Quinn" };

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ namespace GraphQL.Authorization
public ClaimAuthorizationRequirement(string claimType, System.Collections.Generic.IEnumerable<string> allowedValues, System.Collections.Generic.IEnumerable<string> displayValues) { }
public System.Threading.Tasks.Task Authorize(GraphQL.Authorization.AuthorizationContext context) { }
}
public class GraphQLAuthorizeAttribute : GraphQL.GraphQLAttribute
{
public GraphQLAuthorizeAttribute() { }
public string Policy { get; set; }
public override void Modify(GraphQL.Utilities.FieldConfig field) { }
public override void Modify(GraphQL.Utilities.TypeConfig type) { }
}
public interface IAuthorizationEvaluator
{
System.Threading.Tasks.Task<GraphQL.Authorization.AuthorizationResult> Evaluate(System.Security.Claims.ClaimsPrincipal principal, System.Collections.Generic.IDictionary<string, object> userContext, System.Collections.Generic.IReadOnlyDictionary<string, object> inputs, System.Collections.Generic.IEnumerable<string> requiredPolicies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ type Query {
}

[GraphQLMetadata("Query")]
[GraphQLAuthorize(Policy = "ClassPolicy")]
[GraphQLAuthorize("ClassPolicy")]
public class QueryWithAttributes
{
[GraphQLAuthorize(Policy = "FieldPolicy")]
[GraphQLAuthorize("FieldPolicy")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "test")]
public string Post(string id) => "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ type Query {
}

[GraphQLMetadata("Query")]
[GraphQLAuthorize(Policy = "ClassPolicy")]
[GraphQLAuthorize("ClassPolicy")]
public class BasicQueryWithAttributes
{
[GraphQLAuthorize(Policy = "FieldPolicy")]
[GraphQLAuthorize("FieldPolicy")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "test")]
public string Post(string id) => "";
}
Expand Down Expand Up @@ -258,7 +258,7 @@ public class NestedQueryWithAttributes
public IEnumerable<Post> PostsNonNull() => null;
}

[GraphQLAuthorize(Policy = "PostPolicy")]
[GraphQLAuthorize("PostPolicy")]
public class Post
{
public string Id { get; set; }
Expand Down
21 changes: 0 additions & 21 deletions src/GraphQL.Authorization/GraphQLAuthorizeAttribute.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/Harness/GraphQL.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using GraphQL;
using GraphQL.Authorization;

namespace Harness
Expand All @@ -22,7 +23,7 @@ public class Query
/// <summary>
/// Resolver for 'Query.viewer' field.
/// </summary>
[GraphQLAuthorize(Policy = "AdminPolicy")]
[GraphQLAuthorize("AdminPolicy")]
public User Viewer() => new User { Id = Guid.NewGuid().ToString(), Name = "Quinn" };

/// <summary>
Expand Down

0 comments on commit 68afa77

Please sign in to comment.