How can you secure a JavaScript application's API calls?

前端 未结 1 1835
我寻月下人不归
我寻月下人不归 2021-02-13 12:43

I have a JavaScript application.

It\'s built with jQuery.

It uses $.get() to pull JSON data from a server, and uses the data to load a puzzle.

1条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 13:00

    JavaScript authentication has holes

    With JavaScript, just about any authentication system is going to have holes, simply because the code runs directly in the browser and can be seen by anyone (as can the network calls). So there are a couple of things you can try, depending on your situation.

    IP whitelisting

    If you are distributing this application to a small subset of clients, and you know exactly where they will be accessing it from, you could use IP whitelisting. This really is the only way to completely secure the API. However this method is very cumbersome, since with every new client you have to update the API whitelist, and considering what you're talking about here probably not what you're looking for (but I mention it just because it is a possibility).

    Access tokens

    Another method is access tokens. This is a common method used by sites such as Facebook. There are two methods to do this. One is to just give each client a secret key. You can have the same secret key for everyone, but this is not very secure. Having a different secret key for everyone allows you to not only track usage, but also revoke access privs if necessary.

    The first method for access tokens is to just give it inside the JS client. However this means that anyone who looks at the source will be able to access your key, and make requests using it.

    The second method is to have the secret key stored somewhere on the SERVER of the website where your client runs. This server can then make a server-to-server call using that key to obtain a temporary session token. People will still be able to access the temporary session token via the front-end, but they will have to access this site first in order to get it (this allows you to pass off responsibility for handling this to the website operator) and the token will eventually expire. However this means there needs to be some server-side code, and the app won't just be a drag and drop thing.

    For the method given above you can also look into things like OAuth, to avoid re-inventing the wheel.

    Whitelist hard-cap

    Another possible thing using IPs is to set a hard-cap on either how often or how much per day a specific IP can hit the whitelist. Though you may run into problems with users who REALLY like the puzzles, this will prevent some of the potential abuse.

    0 讨论(0)
提交回复
热议问题