Securing my Node.js app's REST API?

前端 未结 4 1455
孤独总比滥情好
孤独总比滥情好 2021-01-29 17:53

I could do with some help on my REST API. I\'m writing a Node.js app which is using Express, MongoDB and has Backbone.js on the client side. I\'ve spent the last two days trying

4条回答
  •  走了就别回头了
    2021-01-29 18:57

    Here's a different way of thinking about it:

    Let's suppose for a moment that you're not using an API. Your user logs into the app, providing some credentials, and you give a cookie or similar token of some sort to the user, which you use to identify that user has logged in. The user then requests a page containing restricted information (or creating/modifying/deleting it), so you check that this token to ensure that the user is allowed to view that information.

    Now, it sounds to me that the only thing you're changing here is the way that information is delivered. Instead of delivering the information as rendered HTML, you're returning the information as JSON and rendering it on the client side. Your AJAX requests to the server will carry that same logged-in token as before, so I suggest just checking that token, and restricting the information down to 'just what the user is allowed to know' in the same way.

    Your API is now as secure as your login is - if anyone was to know the token necessary for accessing the api, they would also be logged into the site and have access to all the information anyway. Best bit is, if you've already implemented login, you've not really had to do any more work.

    The point of systems such as OAuth is to provide this 'logging in' method, usually from a third party application and as a developer. This would potentially be a good solution for an iPhone app or similar, but that's in the future. Nothing wrong with the API accepting more than one authentication method!

提交回复
热议问题