aerogear / keycloak-connect-graphql

Add Keyloak Authentication and Authorization to your GraphQL server.
Apache License 2.0
157 stars 23 forks source link

Forward all directiveResolver args #108

Closed augustusnaz closed 4 years ago

augustusnaz commented 4 years ago

Currently the directive resolvers auth, hasRole etc only forwards graphql params root, args, context, info.

This is generally sufficient but is an issue when using with prisma/nexus and other plugins that many need to add more arguments.

In my case, graphql nexus adds a fourth argument that allows custom resolvers to intercept requests.

An example for a directiveResolver to solve this problem could look like this

exports.auth = (next) => () => {
    if (!context[KeycloakContext_1.CONTEXT_KEY] || !context[KeycloakContext_1.CONTEXT_KEY].isAuthenticated()) {
        const error = new Error(`User not Authenticated`);
        error.code = "UNAUTHENTICATED";
        throw error;
    }
    return next.apply( this, arguments ); 
};
wtrocki commented 4 years ago

Valid point and really easy fix. since you have proposed this change do you want to contribute it? I have no problem to do it next week but I think your code change will be enough.

Thank you so much for logging this issue. I did not seen or had situation like it so it is nice to get this feedback

augustusnaz commented 4 years ago

Sweet! On it