0

I will design a REST backend that will work with my database. I want users to access this data by viewing my webpage (I will work with an MVC JS framework) without logging in.

At the same time, I want to prevent users making direct API calls to my backend so they can't take advantage of my data. (I am gathering some kind of statistics aggregating from many sites).

Is that somehow possible? I was thinking of generating a session based on a captcha but maybe there is a better solution I haven't thought about.

tldr: I want only my code running in the browser to access my backend.

P.S I know people can use selenium to scrap my site, I want to make it difficult / not a good solution for them.

2

Then just don't make a public API. Make your web server build the HTML in the back-end and simply return it as a whole. KISS.

If you want to build a HTML/JS front-end that calls public JSON/whatever REST services, there will be hundreds of ways to call the services directly¹, even it means to simulate a browser (which is really not as hard as it sounds). The Captcha-based session seems like the only secure choice, but then why go as far as to force the user to do something so annoying, such as filling a Captcha along with every request², if you can simply use server-side HTML builders and get rid of the public API?


¹: I can't think of any reason to be afraid of that, but I digress.

²: If it isn't with every request, it must be a session with an expiration date. Until that date, a user can fill the Captcha, get the session ID and just throw it into a program that calls your public APIs.

  • you are right about the captcha. So you propose to make a normal REST service, and have a normal application that logs in the service with a password (that will be my password for example). that is ok, but then I will not be able to use something like backbone directly, I need to do it the old fashioned way of generating the html and hand it in the browser with vanilla jquery etc right? – arisalexis Dec 8 '14 at 9:14
  • It all comes down to using the right tool for the right job. Backbone.js is intended to be used with public REST APIs. Since you can't have that, it clearly isn't the the right tool for the job. So yes, in this case I believe the "old fashioned way" is the right one. Sometimes we have to leave our personal preferences behind. ;) – MichelHenrich Dec 8 '14 at 14:51
0

If you don't want your users to view the data in the format that is returned by the API to the client side JS, then you can do the client side processing/aggregation on the server itself and return the aggregated data via your API. This way the API will only return the data that the users are allowed to see and your JS framework works just as a presentation layer (you might do away with MVC on the client side).

There is no comprehensive way to prevent your users from accessing data that is accessed by your client side JS code. If it were a compiled app (java applet, etc) you could make it more difficult for the user to view the data from the API but even then the data can still be retrieved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.