The nightly build of OpenIG contains a new filter - the ThrottlingFilter - which allows a simple way to limit and then time-out, a user who hits an endpoint x-number of times.
To test, I created a simple node.js API that allows the read (GET) and write (POST) of widgets. Each widget has a numerical id that is used as the identifier. I also created a basic queryAll route within my node.js API to do a wildcard-esque search to return all ids.
So now, I wanted to add OpenIG into the mix and do a little reverse-proxying. I wanted to basically expose only the GET queries against my API, preventing POST requests entirely and only allowing GETs to specific endpoints. To those endpoints I also wanted to limit the number of requests per user to 3 - if that threshold was hit I would redirect the user to a warning page and time them out for 10 seconds.
To set things up, I added a few things to my config.json main heap. Firstly, I used the defaultHandler attribute within my main router, to act as a catch all and handle all the requests that came in for which a specific route file was not defined. I also added in the new ThrottlingFilter so I could use this from within any of my routes - as objects in the config.json main heap are visible to allow my lower level route handlers. The ThrottlingFilter just looks like this:
I then setup a couple of static HTML files that I housed in a config/html folder in my OpenIG base directory. I had a noRouteResponse.html that my defaultHandler delivered via a StaticResponseHandler (note here, I also wanted to include an image in my HTML, so I included the image as a base64 encoded object, so I didn't have to worry about access to the image URL). I also created a thresholdBreachedResponse.html, that I would redirect to, again via a StaticResponseHandler, when a user racked up 3 hits to my API.
In my config/routes directory I had two routes - one for a GET on an :id and another for a GET on the queryAll endpoint. I added no explicit routes for POST requests, so they would be caught by my defaultHandler and redirected and thus preventing access.
The route for my throttling does a few things. Firstly, I added a conditional on what would trigger it - using the out of the box matches function, I added a basic regex for capturing requests that matched '/widget/[0-9][0-9]' - that is, only requests with digits as the :id - /widget/AA would fail for example. The route passed all traffic that matched into a chain - which is just an ordered set of filters. Here I could call my throttle filter and also a SwitchFilter. The switch allowed me to check if a user had the threshold imposed by my throttle. If the throttle was triggered, a 429 response code was hit back - if so, OpenIG would catch this and I would redirect to my thresholdBreachedResponse.html page.
At a high level, the set up looks like the following:
The redirect for the threshold breach in reality, may redirect to an identity provider like Facebook or OpenAM to log the user in, before allowing unlimited access that avoided the throttle filter entirely.
Code for the node.js API example is available here.
Artifacts for the OpenIG config are available here.