Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arguments Empty When Trying Authorize #43

Open
andrejohansson opened this issue Apr 10, 2019 · 6 comments
Open

Arguments Empty When Trying Authorize #43

andrejohansson opened this issue Apr 10, 2019 · 6 comments
Labels
question Further information is requested

Comments

@andrejohansson
Copy link

I'm trying to authorize a field by validating it´s input arguments in a custom policy. My policy gets executed correctly but the Arguments property of the AuthorizationContext variable is always empty. What do I need to do in order to get this populated?

Field Definition

            Field<DeviceContextType>(
                name: "deviceContext",
                description: "Get device context data by id.",
                arguments: new QueryArguments(
                    new QueryArgument<IdGraphType> {Name = "id"},
                    new QueryArgument<IdGraphType> {Name = "organizationId"}
                ),
                resolve: context => new DeviceContext
                {
                   // my dummy object here
                }
            ).AuthorizeWith<DeviceQueryPolicy>();

DeviceQueryPolicy

    public class DeviceQueryPolicy : IAuthorizationPolicy
    {
        public DeviceQueryPolicy(IDependencyResolver resolver)
        {
            Requirements = new[]
            {
                new DeviceIdMatchOrganizationIdRequirement(resolver),
            };
        }

        public IEnumerable<IAuthorizationRequirement> Requirements { get; }
    }

DeviceIdMatchOrganizationIdRequirement

        public async Task Authorize(AuthorizationContext context)
        {
            // Arguments is always empty here! Why?
            if (!context.Arguments.ContainsKey("id") 
                || !context.Arguments.ContainsKey("organizationId")) {
                context.ReportError("Organization have no access rights to this device");
                return;
            }

Query

# Passing along two arguments
{
  devices {
    deviceContext(
      id: "26748243-9D9D-4E9A-A890-718A46D2C0D5"
      organizationId: "26748243-9D9D-4E9A-A890-718A46D2C0D5"
    ) {      
      active
    }
  }
}
@joemcbride
Copy link
Member

AuthorizationContext.Arguments is probably a poorly named property. Those are actually the Input Variables and not the Arguments to the field.

@andrejohansson
Copy link
Author

I think I'm mixing things up here, which Input variables do you mean?

@joemcbride
Copy link
Member

@andrejohansson The variables that you pass in to a GraphQL Query.

https://graphql-dotnet.github.io/docs/getting-started/variables

@joacoleza
Copy link

joacoleza commented Jun 6, 2019

Is there any way to access to the field's arguments from the AuthorizationContext in order to use them in the auth logic?

@joemcbride
Copy link
Member

@joacoleza only if you use variables really. These rules run before execution so don’t have direct access to the arguments.

@joacoleza
Copy link

Ok. I guess that I will have to add that kind of authorization on the resolver itself, right?

@sungam3r sungam3r added the question Further information is requested label May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants