8000 clean up Authorization selection · EntityGraphQL/EntityGraphQL@ca82261 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca82261

Browse files
committed
clean up Authorization selection
1 parent 90189b0 commit ca82261

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

docs/docs/authorization.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,35 @@ sidebar_position: 5
66

77
You should secure the route where you app/client posts request to in any ASP.NET supports. Given GraphQL works with a schema you likely want to provide authorization within the schema. EntityGraphQL provides support for checking claims on a `ClaimsPrincipal` object.
88

9+
## Authorization Services
10+
11+
EntityGraphQL supports different authorization service implementations:
12+
13+
- **`RoleBasedAuthorization`** - The default. Checks roles on the `ClaimsPrincipal`. Use when you only need role-based authorization.
14+
- **`PolicyOrRoleBasedAuthorization`** - Supports both ASP.NET Core policies and roles. This is the default when calling `AddGraphQLSchema()` in `EntityGraphQL.AspNet` if `IAuthorizationService` is available.
15+
16+
### Configuring Authorization Service
17+
18+
When using `AddGraphQLSchema()` in ASP.NET, `PolicyOrRoleBasedAuthorization` is used by default. To use a different authorization service:
19+
20+
```cs
21+
services.AddGraphQLSchema<DemoContext>(options => {
22+
// Use role-based authorization only
23+
options.Schema.AuthorizationService = new RoleBasedAuthorization();
24+
25+
// Or use a custom authorization service
26+
options.Schema.AuthorizationService = new MyCustomAuthService();
27+
});
28+
```
29+
30+
When creating a schema manually outside of ASP.NET:
31+
32+
```cs
33+
var schema = new SchemaProvider<DemoContext>(
34+
authorizationService: new RoleBasedAuthorization()
35+
);
36+
```
37+
938
## Passing in the User
1039

1140
First pass in the `ClaimsPrincipal` to the query call

src/EntityGraphQL.AspNet/Extensions/AddGraphQLOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using EntityGraphQL.Schema;
3+
using Microsoft.AspNetCore.Authorization;
34

45
namespace EntityGraphQL.AspNet;
56

@@ -10,6 +11,13 @@ namespace EntityGraphQL.AspNet;
1011
/// <typeparam name="TSchemaContext">The type of the schema context</typeparam>
1112
public class AddGraphQLOptions<TSchemaContext>
1213
{
14+
public AddGraphQLOptions(IAuthorizationService? authService)
15+
{
16+
// Default for asp.net if IAuthorizationService available
17+
if (authService != null)
18+
Schema.AuthorizationService = new PolicyOrRoleBasedAuthorization(authService);
19+
}
20+
1321
/// <summary>
1422
/// Options that control how SchemaBuilder reflects the object graph to auto-create schema types and fields.
1523
/// </summary>

src/EntityGraphQL.AspNet/Extensions/EntityGraphQLAspNetServiceCollectionExtensions.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,12 @@ public static IServiceCollection AddGraphQLSchema<TSchemaContext>(this IServiceC
4545
var authService = serviceProvider.GetService<IAuthorizationService>();
4646
var webHostEnvironment = serviceProvider.GetService<IWebHostEnvironment>();
4747

48-
var options = new AddGraphQLOptions<TSchemaContext>();
48+
var options = new AddGraphQLOptions<TSchemaContext>(authService);
4949
configure(options);
5050

5151
// Apply environment-based defaults if not explicitly set
5252
var schemaOptions = options.Schema;
5353

54-
// If user hasn't configured authorization, use the ASP.NET policy-based authorization
55-
if (schemaOptions.AuthorizationService is RoleBasedAuthorization)
56-
{
57-
schemaOptions.AuthorizationService = new PolicyOrRoleBasedAuthorization(authService);
58-
}
59-
6054
// If user hasn't explicitly set IsDevelopment, detect from environment
6155
// We check if it's still the default value (true) and the environment is not Development
6256
if (webHostEnvironment != null && !webHostEnvironment.IsEnvironment("Development"))

0 commit comments

Comments
 (0)
0