0

Context: I have an API (using DDD) with an entity lets call it "Content" that only can be update by certain users. For example Content with Id = 1, can only be modified by User Id = 1, Content with Id = 2, can only be modified by User Id = 2, etc...

I'm using an external Identity provider (Auth0) and of course I'm using a claim on a user to give him the role "manager", so he can access the endpoints.

The question:
I have 2 approaches in mind.

  1. Should I store the ContentId inside Identity user as a claim, so it can be validated by a policy or similar?
  2. Should I store this relationship in my app database and validate this at application layer level, cause it's business logic and my own app logic it's easier to customize than Identity?

I don't know if option 2 is spliting the single responsibility that Identity provider owns. Furthermore, is more complex and generates more code. On the other hand is easier to customize.

Option 1 is simple and "clean" cause you only need the claims and a policy that checks if the Id provided to the endpoint is the same the one stored in the claims. But I ask myself, "Is Presentation layer the responsible of that business logic?"

Notes:
The idea is that this service will become an entreprize size microservice project. So I want the "scalable option"

asked Feb 27, 2024 at 9:46

1 Answer 1

2

So i'm assuming here that each user basically gets a different view of your app and loads of things have this UserId field attached. Effectively each user is a tenant of the app. eg Facebook.

I would advise using option 1. The best course with "Business rules which are auth rules" is to treat them as security rules.

Adding the id claim to the auth token is scalable and works if you are passing it around microservices.

However! You do have to consider that its normal for objects to have relationships and replacing them all with ids passed in token claims "I relate to these object ids" wouldn't be sensible.

If you find yourself adding random ids to your token something has gone wrong. Try and replace your authorisation related business rules with a simple Role Based Security model.

ie

  • users born on tuesdays can select a darkmode

becomes

  • darkmode can be used by powerusers

And you then assign the "poweruser" role to whomever you want.

answered Feb 27, 2024 at 10:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.