Skip to content

Create project context from route and authorize based on user context#14

Closed
myieye wants to merge 1 commit intomainfrom
feat/create-and-authorize-project-context-from-route
Closed

Create project context from route and authorize based on user context#14
myieye wants to merge 1 commit intomainfrom
feat/create-and-authorize-project-context-from-route

Conversation

@myieye
Copy link
Copy Markdown
Contributor

@myieye myieye commented Jan 27, 2023

Fixes #15

  1. Create a Project-Context that can be injected globally to determine the current project.
  2. Add a project-access policy that verifies the current user has access to the project

string.IsNullOrEmpty(roleClaim) || string.IsNullOrEmpty(projectRolesClaim))
{
throw new ArgumentException($"User is missing required claims. Claims: {user.Claims}.");
return null;
Copy link
Copy Markdown
Contributor Author

@myieye myieye Jan 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hahn-kev I decided this makes more sense in the end, because when we get requests from unauthenticated users there can still be a WebContext, it just doesn't include a user.

Ultimately I made this change, because (to my surprise) my ProjectAuthorizationHandler gets instantiated for requests that don't need it (i.e. requests where AllowAnonymous is present). So, I either had to call ExtractLfUser from the authorization handler or make this change. I don't have a super strong opinion either way.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My first thought was to just lazily get the user - and leave this code as is.

But that means we could miss bugs where we access the user from some code outside of an authorized context, in this case that code has to decide what to do if the user is null now.

@myieye myieye self-assigned this Jan 27, 2023
Copy link
Copy Markdown
Contributor

@hahn-kev hahn-kev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good. I'm still not a fan of putting the project code in the url though. See my comment in #2

//default policy is when there's no parameters specified on the auth attribute
//this will make sure that all endpoints require auth unless they have the AllowAnonymous attribute
options.FallbackPolicy = options.DefaultPolicy;
options.FallbackPolicy = AuthorizationPolicy.Combine(options.DefaultPolicy, options.GetPolicy(nameof(ProjectAuthorizationRequirement)));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this modifies the fallback policy. But not the default, so if we were to put the authorize attribute somewhere then we would no longer be applying the project auth requirement without any trigger that this was happening.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. Then, I'll change the Default-Policy.

@myieye
Copy link
Copy Markdown
Contributor Author

myieye commented Feb 10, 2023

Implemented in #18

@myieye myieye closed this Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create project context from current route and use for authorize check

2 participants