The past 2 months I have been focused almost exclusively on building a new, exciting product for GetHuman. I am using the MEEAN stack (i.e. MongoDB Express.js ElasticSearch AngularJS Node.js), which basically means doing a boat load of JavaScript development. As someone who has done a lot of JavasScript development can tell you, there are a ton of open source libraries out there that can help speed your development and improve the quality of your code.
In fact, there are so many great libraries out there that I am always surprised when I can't find a good one for something that I think is a common problem. One such problem came up a couple weeks ago and I was inspired to create a solution which I hope to turn into a new open source library.
When an API is exposed with multiple roles and perhaps even multiple permission levels within each role, it takes a decent amount of code to properly control security access. For example, here are some examples of role based security access code that is implemented within an API:
- Allow admins to select all fields in a USERS collection, but block other roles from selecting sensitive data like password fields, user address, etc.
- Only allow a user to query data that they created (ex. their own posts)
- User with role X can read all data from a collection, but is only allowed to update a subset of those fields.
These are all pretty standard examples of API logic and I think many developers assume this is the stuff that is manually written within API code. The issues with doing that, however, include:
- Code Proliferation - As you start to add roles and permissions become more complex, this type of code proliferates and bloats the API
- More Bugs - It is easy to make a mistake because you are manually writing out what often ends up being long, complex logic
- Difficult to Refactor - It is tough to refactor code that is so specific and dense. Since the code is security related you can end up being afraid to make changes.
The thing is that while the rules are specific (i.e. role X can only access fields a, b and c in collection Y), they almost always follow a general pattern. My solution is to abstract out the basic patterns for CRUD operations on NoSql document store databases and create a relatively simple configuration file that can be used to enforce all the specific rules. Here is an example of a config file for one collection:
This sounds like a great idea to me. With a well-written library, I'd probably start including it in most of my projects. I'm dealing with user levels on a project right now and they're such a pain to code out.