g.carey
g.carey

Reputation: 710

JSON API REST endpoint with permissions-restricted fields

JSON API REST endpoint with permissions-restricted fields

I am working on a JSON API-compliant REST api. Some endpoints contain fields that should be restricted (read-only or not available) for certain users.

What is the best way to architect the api to allow that certain users have access to certain fields, while others do not? By "best", I mean:

I am considering the following options, each with their set of concerns/ questions. I would be more than grateful for any other solutions!

Option 1: Return null on restricted fields for users without permissions

Option 2: Exclude restricted fields entirely for users without permissions

Option 3: Move restricted field(s) onto another endpoint, available as an ?include='field_name' relation for those with permission

Note: I have already built a separate permissions schema so that the client can determine which CRUD privileges the user has, per top-level endpoint, before making any requests. Permission sets are indexed by resource type.

Any help on the matter would be very much appreciated! And if anything needs to be clarified, feel free to ask.

Upvotes: 9

Views: 3128

Answers (1)

arjabbar
arjabbar

Reputation: 6404

I would definitely not use undefined or null to indicate fields that the user is not allowed to see. To me, that feels like a lie and represents that the data is really not there. They would have to really know your API in order to get a grasp of what is really going on.

I would recommend something more like your 3rd option, except I would make it a different endpoint altogether. So in your example, the endpoints would be:

/api/entity/1/cost

and for admins

/api/admin/entity/1/cost

or something like that.

This way your server code for the admin endpoint could just be focused on authenticating this admin user and getting them back all the fields that they have visibility on. If a non admin user tries to hit that route, reject them with an unauthorized status code.

I'm not saying that you should not implement the GET param to be able to specify fields as well. You can if you want to, but I don't think it just won't be necessary in this case.

Upvotes: 1

Related Questions