Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

How to implement a secure REST API with node.js

I start planning a REST API with node.js ,express and mongodb. The API provides data for a website (public and private area) and maybe later a mobile app. The frontend will be developed with AngularJS.

For some days I read a lot about securing REST APIs, but I don’t get to a final solution. As far as I understand is to use HTTPS to provide a basic security. But how I can protect the API in that use cases:

  • Only visitors/users of the website/app are allowed to get data for the public area of the website/app

  • Only authenticated and authorized users are allowed to get data for private area (and only data, where the user granted permissions)

At the moment I think about to only allow users with a active session to use the API. To authorize the users I will use passport and for permission I need to implement something for myself. All on the top of HTTPS.

Can somebody provide some best practice or experiences? Is there a lack in my "architecture"?

Answer*

Draft saved
Draft discarded
Cancel
13
  • 1
    I also use mongodb but it's pretty easy to manage if you save the session (accesstoken) using redis (use atomic operations). The apitoken is generated in the server when the user creates an account and sent it back to the user. Then, when the user wants to authenticate it must send username+password+apitoken (put them in the http body). Keep in mind that HTTP doesn't encrypt the body so the password and apitoken can be sniffed. Use HTTPS if this is a concern for you. Commented Mar 20, 2013 at 10:03
  • 1
    what's the point on using an apitoken? is it a "secondary" password? Commented Feb 14, 2014 at 12:13
  • 2
    @TheBronx The apitoken has 2 use cases: 1) with an apitoken you can control the access of the users to your system and you can monitor and build statistics of each user. 2) It's an additional security measure, a "secondary" password. Commented Feb 14, 2014 at 18:01
  • 1
    Why should you send the user id again and again after successfull authentication. The token should be the only secret you need to perform API calls. Commented Jun 28, 2014 at 7:43
  • 1
    Idea of the token - beside abusing it for tracking user activity - is, that a user ideally does not need any username and password for using an application: The token is the unique access key. This allows users to drop any key at any time affecting only the app but not the user account. For a webservice a token is quite unhandy - that's why an initial login for a session is the place where the user gets that token - for a "regular" client ab, an token is no problem: Enter it once and you'r almost done ;) Commented Jun 28, 2014 at 7:44

lang-js

AltStyle によって変換されたページ (->オリジナル) /