Search code examples
laraveleloquentoauth-2.0

Is possible to use Oauth2 scopes to scope model data?


Every place I read about Oauth2 scopes it uses examples of read, write, delete, post:read, post:delete, etc... This always representing "actions", like It was a permission...

I am in a situation that I must implement an API that must authenticate the user but limit user's access to data that only belongs to the same corporation he belongs, this user may belong to "N" corporations.

I came with the idea to use the Oauth2 scopes for that purpose then use Laravel's eloquent global scopes in model to filter the data.

I am stuck and dont know How to proceed. Could anyone give some advice?


Solution

  • There are 2 concepts in the requirements you mention:

    • Scopes are high level privileges that represent an area of data and operations allowed on that data - they are also static values defined as part of the system design. Avoid attempting to use them for dynamic logic.

    • Claims are where the real authorization happens, and what most domain specific authorization uses. Claims are just extra fields included in JWTs. In your case an array claim of Corporation IDs could be issued and included in JWTs received by APIs.

    These two Curity articles explain this in more detail, along with some real world examples. When done well, the result should be simple code in your APIs: