ITZone

Authentication in SPA (ReactJs, VueJs) “Where should the token be stored, are there security related issues?

Summary

Cookie, session, token, JWT where should Cookie, session, token, JWT be stored, what security issues should you care about? In this article, I will share what I know about it.

I will focus on the important concepts that need to be remembered when handling user authentication in the most basic model, Client – Server :

Prerequisites to improve Security

HttpS encrypted protocol

Do not use URL query params to exchange sensitive data

Avoid brute force attacks

Update dependencies regularly.

Authentication

There are 2 main mechanisms for enforcing 1 client authentication in the REST API :

Bearer Token

What is a Bearer token ?

Bearer token is the value passed into the Authentication header of an HTTP Request . It is not automatically saved, does not expire and is not attached to the domain . It is only 1 value =))

GET https://www.example.com/api/users

Authorization: Bearer bearer_token_value

For a stateless application, we often use JWT for token creation. In simple terms, JWT consists of 3 parts:

JWT is a secure password, in the process of exchanging information, making authentication becomes stateless . Signature will authenticate the payload without being modified using symmetric or asymmetric (RSA) algorithms. Header contains the public key information to verify the Signature . The client application , the first time it requests on the server, will retrieve a JWT token through the username and password login. Then, through Javascript to attach the JWT token inside each HTTP header . The server will authenticate the signature corresponding to the payload , if they overlap, we can trust the contents of the Payload .


Cases should use Bearer Token

Where should the JWT token be stored?

We can store JWT on the clients side ( memory, local / session cookie ,, local storage, … ). But JWT not recommended to store in the browser local storage by:

In my opinion, saving JWT in session cookie is probably the best solution.

Get more information about saving tokens.

Types of attacks should be avoided

Cross-site Scripting (XSS) attack is the most common form of attack when using Javascript to solve security issues. Hackers will take advantage of user input to insert malicious javascript code to steal victim's JWT token , then use it to impersonate the user.

We can minimize the possibility of XSS by controlling user input.

Cookie are a name-value pair, stored on a web browser and they have an expiration time, which is immediately associated with a domain . Cookie are generated by the client browser using Javascript .

or from the Server clearance HTTP Response header :

The web browser automatically sends cookie with each request to the cookie domain .

Typically, Cookie are used to store a Session ID . Session ID is managed by the server. Here, I am referring to a stateful app where the server needs to manage the state on the server while the JWT token is stateless . There are 2 types of Cookie :

Server Cookies can be configured with the following options:

The case should use

Where should cookies be stored?

Cookies are automatically saved on the web browser with the expiration time and a specified domain name.

Types of attacks should be avoided

Combining both mechanisms

After a while of rambling around, grabbing our Server API is in need of an authentication mechanism that:

What if JWT token is put into Cookie to combine the advantages of both?

The Server API should use the JWT bearer token in the request header , as well as the JWT inside the session cookie . If you want to authenticate to javascript to read JWT payload , you should use two cookie authentication solution by including two types of cookies to avoid XSS attack.

JWT can be updated on every successive request by the server, as well as JWT inside the cookie response and they are automatically saved by the browser. To restrict CSRF, changes will not be implemented via the GET protocol, instead you use PUT or POST . But dynamic changes to issues that require high security should require user credentials . A temporary cookie (random number) that javascript can read and they are submitted in a hidden value of the form along with the form data. The server will have to check, if the value in the cookie does not match the value in the form, the user behavior will not be executed.

summary

In summary, we have learned that an authentication flow for SPA should follow these steps:

Share the news now