Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(JWT): Generate tenant claims and validate them. #914

Merged
merged 4 commits into from
Feb 18, 2025

Conversation

jimsynz
Copy link
Collaborator

@jimsynz jimsynz commented Feb 18, 2025

I don't like that it always places a "tenant" => nil claim in the token.

@jimsynz jimsynz requested a review from zachdaniel February 18, 2025 02:24
@jimsynz jimsynz force-pushed the fix/validate-tenant-claim branch from 95aa5e3 to 686f599 Compare February 18, 2025 02:39
@zachdaniel
Copy link
Collaborator

Yeah we need to find way to not have tenant set to nil. We also need to be sure that this method isn't requiring that all non multi tenant tokens explicitly have that key and is set to nil because that will log out all users for all non multi tenant usages of AA on upgrade.

end
end

defp multitenancy_required?(user_resource) do
Copy link
Collaborator

@zachdaniel zachdaniel Feb 18, 2025

Choose a reason for hiding this comment

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

It's not about if the token is multi tenant, but if the user is multi tenant. When we generate the token we need to set the tenant into it, but only if the tenant option is set when generating the token. You can have resources that support being accessed both with a tenant and without.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually I think we shouldn't worry about any of it. If the tenant option is set (to anything other than nil) in this code, then we write the tenant into the claims. If there is a tenant claim when validating, or if the tenant option is set, then we validate that the two match.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nope, I'm wrong. We do want to check if the user (but not the token) supports multitenancy, and if so then we add and validate the tenant. That might make it easier because in those cases it actually makes sense to add a nil tenant claim if it is nil.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually I think I just realised that we can just make it optional whether we add it to the config if required.

@jimsynz jimsynz force-pushed the fix/validate-tenant-claim branch from 686f599 to 94f25f5 Compare February 18, 2025 05:00
@jimsynz jimsynz requested a review from zachdaniel February 18, 2025 05:24
defp maybe_add_tenant_claim(cfg, nil), do: cfg

defp maybe_add_tenant_claim(cfg, tenant) do
Config.add_claim(
Copy link
Collaborator

Choose a reason for hiding this comment

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

So the main thing missing here is that if there is a tenant claim, and no opts[:tenant] then it is invalid. How to we add this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Alright, I remember the simpler answer. its "does the user resource support multi tenancy", and if so there is a tenant claim that must match, nil or otherwise.

@zachdaniel
Copy link
Collaborator

Ah, my bad, I didn't see that this PR also added a user with multi tenancy, and merged a different PR that did. I will alter this PR to use the new one from the other PR

@zachdaniel zachdaniel force-pushed the fix/validate-tenant-claim branch from 94f25f5 to 771c4e6 Compare February 18, 2025 21:42
@zachdaniel zachdaniel merged commit 62e3ee5 into main Feb 18, 2025
1 check passed
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.

2 participants