angularjsrestauthenticationcookiesangularjs-authentication

Authentication with AngularJS, session management and security issues with REST Api WS


I started developing a web-app with angularJS and I'm not sure that everything is right secured (client and server side). Security is based on a single login page, if credentials are checked ok, my server sends back an unique token with custom time-validity. All other REST api are accessible through this token. The application (client) browse to my entry-point ex: https://www.example.com/home.html user insert credentials and receive back a unique token. This unique token is stored in the server database with AES or other secure techniques, it is not stored in clear format.

From now on, my AngluarJS app will use this token to authenticate to all REST Api exposed.

I'm thinking on temporary store the token in a custom http cookie; basically, when the server verifies the credentials, it sends back a new cookie Ex.

app-token : AIXOLQRYIlWTXOLQRYI3XOLQXOLQRYIRYIFD0T

The cookie has the secure and HTTP Only flags set on. Http protocol directly manage the new cookie and store it. Successive requests will presents the cookie with the new parameter, without the need to manage it and store it with javascript; at every request, server invalidates the token and generates a new one and sends it back to the client --> prevent replay-attacks with a single token.

When the client receives an HTTP status 401 unauthorized response from any REST Api, the angular controller clean all the cookies and redirect the user to the login page.

Should I have to consider other aspects? Is it better to store the token inside a new cookie or in localStorage? Any tips on how to generate a unique strong token?

Edit (improvements):


Solution

  • If you talk to the server via https, you don't have a problem with replay attacks.

    My suggestion would be to leverage your server's security technology. For example, JavaEE has an out-of-the-box login mechanism, declarative role-based protection of resources (your REST endpoints) etc. These are all managed with a set of cookies and you don't have to care about storage and expiration. Check out what your server/framework already gives you.

    If you plan to expose your API to a broader audience (not specifically to the browser-based UI that you serve) or other types of clients (e.g. mobile app), consider adopting OAuth.

    Off the top of my head, Angular has the following security features (will add more as they pop-out):

    CSRF/XSRF attacks

    Angular supports an out of the box mechanism for CSRF protection. Check out $http docs. Server-side support is needed.

    Content Security Policy

    Angular has a mode of expression evaluation that is compatible with more strict JavaScript runtimes that are enforced when CSP is enabled. Check out ng-csp docs.

    Strict Contextual Escaping

    Use Angular's new $sce feature (1.2+) to harden you UI against XSS attacks etc. It's a bit less convenient but more secure. Check out the docs here.