Code Monkey home page Code Monkey logo

Comments (4)

sungam3r avatar sungam3r commented on July 18, 2024 1

I'll send a PR to add this into readme.

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024

AuthorizationValidationRule is the "framework" code and does not need to be modified. That rule checks all of the Graph Types in the query to see if they have authorization policies applied to them, which is why CheckAuth is called so many times.

You can instead add your own authorization requirements to the framework to extend it. Create your own IAuthorizationRequirement class and add that requirement to your policy.

public class UserExistsRequirement : IAuthorizationRequirement
{
    public Task Authorize(AuthorizationContext context)
    {
        // this is the authenticated user
        var user = context.User;

        // check DB here
        return Task.CompletedTask;
    }
}

public static class AuthorizationPolicyBuilderExtensions
{
    public static AuthorizationPolicyBuilder UserExists(this AuthorizationPolicyBuilder builder)
    {
        builder.AddRequirement(new UserExistsRequirement());
        return builder;
    }
}

// build your policy
var authSettings = new AuthorizationSettings();
authSettings.AddPolicy("UserExists", p => p.RequireAuthenticatedUser().UserExists());


// apply the policy to the graph type

/// <summary>
/// CLR type to map to the 'Query' graph type.
/// </summary>
public class Query
{
    /// <summary>
    /// Resolver for 'Query.viewer' field.
    /// </summary>
    [GraphQLAuthorize(Policy = "UserExists")]
    public User Viewer() => new User { Id = Guid.NewGuid().ToString(), Name = "Quinn" };
}

from authorization.

ninomllr avatar ninomllr commented on July 18, 2024

Ok thanks for adding this to the readme!

Reason why we copied AuthorizationValidationRule intially was because we needed to change the error message. We thought it could be a good place to change some things about validation as well. Thanks for clearing this up.

from authorization.

sungam3r avatar sungam3r commented on July 18, 2024

we needed to change the error message

I already do this, will be in v4.

from authorization.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.