Code Monkey home page Code Monkey logo

Comments (3)

okarlsson avatar okarlsson commented on July 18, 2024

So I've researched this some more and it seems like I can get away without using the [Authorize] attribute on my endpoint. The UserContext gets populated from the JWT token without the attribute.

This means that I could solve this by making sure that my users always have a role in the JWT token.

So I can register some policies like this:

services.AddGraphQLAuth({
    _.AddPolicy("UserPolicy", p => p.RequireClaim("roles", "user", "admin"));
    _.AddPolicy("AdminPolicy", p => p.RequireClaim("roles", "admin"));
});

And then use them on my fields like this:

// Mutation open for anyone
graphQLMutation.FieldAsync<ResponseGraphType<StringGraphType>>(
	"doSomething",
	arguments: new QueryArguments(
		new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "username" }
	),
	resolve: async context =>
	{
		// Do something..
	}
);

// Mutation open for users and admins
graphQLMutation.FieldAsync<ResponseGraphType<StringGraphType>>(
	"doSomethingAsUser",
	arguments: new QueryArguments(
		new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "username" }
	),
	resolve: async context =>
	{
		// Do something as user..
	}
).AuthorizeWith("UserPolicy");

// Mutation open for admins
graphQLMutation.FieldAsync<ResponseGraphType<StringGraphType>>(
	"doSomethingAsAdmin",
	arguments: new QueryArguments(
		new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "username" }
	),
	resolve: async context =>
	{
		// Do something as admin..
	}
).AuthorizeWith("AdminPolicy");

Could there be any potential security risks here?

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024

As long as the ClaimsPrincipal is getting populated, should be fine.

from authorization.

okarlsson avatar okarlsson commented on July 18, 2024

Awesome, thanks @joemcbride!

Closing this issue.

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.