Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful API authorization on entities/resources?

I am working on an API in a system that has very complex access control rules. Often times there are complex SQL queries required to determine if a user has read or write access to a particular resource. This causes a lot of complexity and redundancy in our client applications as they have to know all these rules in order to determine whether to present the user with CRUD options for each object.

My goal is to reduce much of the complexity on the client side and house all the complex logic in the API. This way new client applications written against our API can avoid re-implementing the complex access rule logic on their side when ensuring that the UI only presents valid options to the user.

I am not sure what the best way is to handle this. I'm considering two different options but I don't know if there is a better or more standard way to expose generic access information to callers of an API.

Option 1

When a caller makes a GET request on a resource entity or collection of them, every returned entity will return an _allowed_actions field attached, which is an array of actions the caller is allowed to perform on that entity. For example, requesting a Listing object may result in the following response.

GET /listing/5

{
 "id": 5,
 "address": "123 Foo Street",
 "city": "New York",
 "state": "New York",
 "price": 457000,
 "status": "pending",
 "_allowed_actions": ["READ", "UPDATE", "DELETE"]
}

Still unsure how to relate to clients whether they have the authority to create instances of a resource entity using this method, but perhaps the client will simply need to maintain enough understanding of the permission structure to determine this on its own. The access rules around creating instances are typically less complex than the READ/UPDATE/DELETE access rules so that doesn't seem too bad.

Option 2

Create a meta-API, which clients can make requests to in order to determine what actions they can perform on each resource. For example, checking what the client can do with a listing:

GET /access-query/listing/5

{
 "allowed_actions": ["READ", "UPDATE","DELETE"]
}

And checking what options are allowed for listings in general, including CREATE:

GET /access-query/listing

{
 "allowed_actions": ["READ", "CREATE", "UPDATE", "DELETE"]
}

The benefit of this approach is that it allows callers to have a full understanding of what they can do on every resource in a generic way. This way clients wouldn't have to understand that the "create_listing" permission AND a non-probationary user status are required required in order to create listings. They can simply query for this information ahead of time.

The downside to this approach is that it increases the amount of requests. Rather than require clients to have an understanding of the permissions logic, they now have to query once to determine what they can do and a second time to do it.

I don't particularly care for either of these methods but they're all I can come up with at the moment. Is there a better way to go about this?

like image 480
william Avatar asked Jul 08 '14 20:07

william


People also ask

How do you pass credentials in REST API?

Application credential requirements The client must create a POST call and pass the user name, password, and authString in the Request headers using the /x-www-form-urlencoded content type. The AR System server then performs the normal authentication mechanisms to validate the credentials.

Which of these is the most secure way of authenticating an API?

OAuth 2.0 is the best choice for identifying personal user accounts and granting proper permissions. In this method, the user logs into a system. That system will then request authentication, usually in the form of a token.


Video Answer


2 Answers

What you are looking for is fine-grained, externalized authorization:

  • fine-grained: you want to create authorization policies that take into account multiple parameters or attributes and possibly relationships between the client (the requestor) and the targeted entity e.g. a listing in your case.
  • externalized: you want to decouple the business logic from the authorization logic. In your question you complain about how complex the code and the SQL statements are becoming. This is a direct consequence of not clearly separating business logic from authorization logic.

There is a model called attribute-based access control (ABAC) that defines an approach to fine-grained externalized authorization. NIST, the National Institute of Standards and Technology, has produced a report on ABAC which you can read online.

OASIS, the organization for the advancement of structured information standards, has defined a standard called XACML (eXtensible Access Control Markup Language) to implement ABAC.

XACML brings you:

  • an architecture as illustrated below
    • The policy enforcement point (PEP) intercepts your API calls. It protects your API, inspects the messages and sends an authorization request to the policy decision point (PDP).
    • The policy decision point (PDP) evaluates incoming authorization requests from the PEP against a set of authorization policies written in XACML. The PDP eventually reaches a Permit or Deny decision. To reach decisions it may need to look up additional attribute values from databases, web services, LDAP, or files. These are called policy information points in the architecture. XACML Architecture Flow
  • a policy language: the XACML policy language is attribute-based which means it uses attributes to define what can be allowed and what is not. For instance, you could define rules such as:
    • a real estate agent can see all the listings if and only if the listing location == the agent location
    • a real estate agent can edit a listing if and only if he/she owns the listing
    • a real estate agent can close a listing if and only if the listing's item is sold and if and only if the agent is the person that sold the item.
  • a request/response scheme: XACML also defines a way to query the PDP and to get responses back. A PDP can be queried either via single questions or via multiple questions in a single request e.g.:
    • Can Alice view listing 123? Yes, permit.
    • Can Alice view, edit, or delete listing 123? Permit; Deny; Deny.

With a XACML-based approach, you get to maintain your business logic and your API separate from the authorization logic. This has several benefits:

  1. you can always reimplement the API and keep the same authorization model
  2. you can easily expand your API without having to rewrite the authorization
  3. you can change your authorization logic independently of your code
  4. you can audit your authorization logic more easily
  5. your authorization logic is technology-neutral. It works for REST APIs, web services, databases, and more

I recommend you check out the following resources:

  1. the OASIS XACML website
  2. the ALFA plugin for Eclipse - a free tool to write XACML policies.
  3. The XACML developer community

There are both vendor and open-source implementations of XACML:

  • Axiomatics is a vendor solution that provides both .NET and Java XACML implementations
  • SunXACML is a long-standing open source Java XACML implementation

HTH, David.

like image 140
David Brossard Avatar answered Oct 11 '22 23:10

David Brossard


Not trying to resurrect an old question, but I came here searching for almost exactly the same thing and wanted to add a solution that I think is more RESTful.

I haven't actually implemented this but think it may help others who come here...

Your second option is very nearly what I think should be done, but instead of a get use the OPTIONS verb to your resource which will then return an "allow" header with a list of available verbs for that resource.

OPTIONS /listing/5

Assuming your resources are fine-grained enough for this to make sense, then you would know if you can make a POST/DELETE

like image 23
Azbola Avatar answered Oct 12 '22 00:10

Azbola