Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,40 @@ public void nested_type_policy_fail()
});
}

[Fact]
public void passes_with_claim_on_input_type()
{
Settings.AddPolicy("FieldPolicy", _ =>
{
_.RequireClaim("admin");
});

ShouldPassRule(_=>
{
_.Query = @"query { author(input: { name: ""Quinn"" }) }";
_.Schema = TypedSchema();
_.User = CreatePrincipal(claims: new Dictionary<string, string>
{
{"Admin", "true"}
});
});
}

[Fact]
public void fails_on_missing_claim_on_input_type()
{
Settings.AddPolicy("FieldPolicy", _ =>
{
_.RequireClaim("admin");
});

ShouldFailRule(_=>
{
_.Query = @"query { author(input: { name: ""Quinn"" }) }";
_.Schema = TypedSchema();
});
}

private ISchema BasicSchema()
{
var defs = @"
Expand Down Expand Up @@ -172,5 +206,24 @@ public class Author
{
public string Name { get; set;}
}

private ISchema TypedSchema()
{
var query = new ObjectGraphType();
query.Field<StringGraphType>(
"author",
arguments: new QueryArguments(new QueryArgument<AuthorInputType> { Name = "input" }),
resolve: context => "testing"
);
return new Schema { Query = query };
}

public class AuthorInputType : InputObjectGraphType<Author>
{
public AuthorInputType()
{
Field(x => x.Name).AuthorizeWith("FieldPolicy");
}
}
}
}
2 changes: 1 addition & 1 deletion src/GraphQL.Authorization/AuthorizationValidationRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public INodeVisitor Validate(ValidationContext context)
if (argumentType == null)
return;

var fieldType = argumentType.Fields.First(p => p.Name == objectFieldAst.Name);
var fieldType = argumentType.GetField(objectFieldAst.Name);
CheckAuth(objectFieldAst, fieldType, userContext, context, operationType);
});

Expand Down