Code Monkey home page Code Monkey logo

Comments (6)

ntaranov avatar ntaranov commented on July 19, 2024

@john-roland, you might be interested in reading this for extra background

graphql-dotnet/server#159

from authorization.

Shane32 avatar Shane32 commented on July 19, 2024

I wrote my own ASP.NET middleware to process GraphQL requests, and one of the things it does is validate the JWT token passed in and return a 401 response code if it is invalid - prior to calling the document executer (which does validation). Sorry, not sure how that answer helps if you're using this authorization library.

from authorization.

ntaranov avatar ntaranov commented on July 19, 2024

@Shane32 , I wrote a custom middleware piggybacking everything, the magic sauce is here

        private Task WriteResponseAsync(HttpContext context, IDocumentWriter writer, ExecutionResult result, int statusCode = 200)
        {
            var json = writer.Write(result);

            statusCode = result.Errors?.Any() != true
                ? (int)HttpStatusCode.OK
                : result.Errors?.Any(er => (er as ValidationError)?.Code == "authorization") == true
                    ? (int)HttpStatusCode.Unauthorized
                    : (int)HttpStatusCode.BadRequest;

            result.Errors?.ToList().ForEach(e => _logger.LogError($"GraphQL execution error: {e}"));

            context.Response.ContentType = "application/json";
            context.Response.StatusCode = statusCode;

            return context.Response.WriteAsync(json);
        }

UPD: that was long ago, I'm not sure if APIs are now compatible, but you get the idea

from authorization.

sungam3r avatar sungam3r commented on July 19, 2024

Is there's a way to get a 401 status code instead of a 200 + the graphql message (when the authentication is not ok) ?

Of course. Authorize user before handling graphql request. 401/200/xxx - these all are HTTP transport codes. GraphQL as it is has nothing to do with HTTP transport layer.

from authorization.

sungam3r avatar sungam3r commented on July 19, 2024

Closed due lack of feedback.

from authorization.

john-roland avatar john-roland commented on July 19, 2024

sorry for being silent, thank you all for your answers, I've done a bit like you've said, interrupting the request before the graphql processing

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.