I have a JSON web service that I only want to provide for certain sites. It's a service that would be called through JavaScript using JSONP. How would I go about preventing (or at best making it more difficult for) unauthorized sites from accessing it? Requiring a user/password won't work because that would be plainly visible in JavaScript.
Example: My web service at domain.com provides weather information, and I only want website.com and webpage.com to be able to access it. But since the web service is accessed through JavaScript, lazywebsite.com could just view website/webpage's source and copy/paste their JavaScript code.
My thoughts so far:
- Use an API key and log the
HTTP_REFERER
of where the service is accessed from. This isn't ideal sinceHTTP_REFERER
is unreliable. - Have website.com/webpage.com generate a unique key server-side using an algorithm that I provide, save it in a session, and use that as a key to access the web service. This way, the token is only registered for that specific visitor and the JS can't be copy/pasted. The problem then shifts to website.com/webpage.com protecting their page that generates the unique key.
Are there any better solutions?