Authentication in SPA (ReactJS and VueJS). Cookie, session, token, JWT, attacks, where to store token, security concerns? Everything you need to know is here.
Authentication in a single page application (SPA) involves several patterns with pros and cons. This article will list the main important concepts to know and have in mind when dealing with user authentication, especially in this common architecture:
X-Powered-Bythat says what kind of server you use in the response header. You may use Helmetjs if you are operating ExpressJS.
# List security breaches npm audit # Upgrade of minor and patch version following your version ranges in package.json yarn outdated yarn update # Interactive upgrade of minor and patch version following your version ranges in package.json yarn upgrade-interactive # List outdated dependencies including major version yarn upgrade-interactive --latest # Same with npm npm outdated npm update # Tool for upgrading to major versions (with potential breaking changes) npm install -g npm-check-updates ncu
There are 2 main authentication mechanisms (you will see later that we can combine them) to identify a client on a REST API:
A bearer token is a value that goes into the Authorization header of any HTTP requests. It is not automatically stored anywhere, it has no expiry date and no associated domain. It’s just a value:
GET https://www.example.com/api/users Authorization: Bearer my_bearer_token_value
To have a stateless application, we can use JWT for our token format. Basically, a JWT contains 3 parts:
JWT is a cryptographically secure means of exchanging information that make stateless authentication possible. The signature certifies that the payload has not been modified (i.e. that is not compromised) thanks to symmetric or asymmetric (RSA) signature. The header contains the format and public key address to verify the signature (for asymmetric).
Basically, the client application gets a JWT token once authenticated by a user/password authentication (or other means).
We have to manually store the JWT in the clients (memory, local/session cookie, local storage, etc…).
It is not recommended to store the JWT in the browser local storage:
Storing JWT in session cookie may be the solution, we will talk about that later.
To go further: https://auth0.com/docs/security/store-tokens
XSS attacks can be mitigated by escaping and controlling user-generated content but it will be very difficult to detect and mitigate a compromised web dependency served by a public CDN.
or from the server using an HTTP Response header:
Set-Cookie: my_cookie_name=my_cookie_value // HTTP Response Header
The web browser automatically sends cookies with every request to the cookie’s domain.
GET https://www.example.com/api/users Cookie: my_cookie_name=my_cookie_value
In most (stateful) use cases, a cookie is used to store a session ID. The session ID is managed by the server (creation and timeout). We talk about stateful as the server needs to manage a state on the server whereas a JWT token is stateless.
There are 2 kinds of cookies (source):
Server cookies can be configured with several options:
They are automatically stored in the web browser with an expiry date (optional) and associated domain.
Another example of CSRF: let’s assume that the user, while he is still logged in to facebook.com, visits a page on bad.com. Now, bad.com belongs to an attacker where he has coded the following on bad.com:
To mitigate XSS, the HttpOnly option must be set on the cookie.
To mitigate CSRF, the SameSite option must be set on the cookie. The SameSite option is not supported by all browsers so it will not prevent all CSRF attacks. Some other mitigation strategies (that can be combined) can be used:
Let’s summarize what we are looking for our authentication mechanism on our server API:
What about putting a JWT inside a cookie to get the best of both worlds?
The two cookie authentication approach has been described by Peter Locke in https://medium.com/lightrail/getting-token-authentication-right-in-a-stateless-single-page-application-57d0c6474e3
JWT can be updated on each request seamlessly by the server as the new one will be in the cookie response and automatically stored by the browser. This way, the expiration date of the JWT can be put back.
To limit CSRF, mutations should never be done using a GET query, use PUT or POST. Mutations with high-security concerns should ask user credentials again, for instance, the change email mutation should ask the user password to validate the change. The temporary cookie could also embed a random number that is read by JS and submit in a hidden form field as long with the form data. The server has to check if the random number in the cookie matches the value from the form data.
The authentication flow for our SPA is now the following:
Feel free to comment and put your thought on this post. If you like what you read, don’t forget to clap!
Vue js Axios Tutorial. We will see Vue axios post request, Vue js headers and Vue js get request. We will use Node.js as backend platform.
In this article, I will collect 10 Vue icon component to bring more interactivity, better UI design to your Vue application.
Markdown is a way to style text on the web. The 10 Vue markdown components below will give you a clear view.
In this article, I'll show you the reasons behind Vue CLI's creation and take you on a journey through its evolution. This will help you not only appreciate it more but also have a deeper understanding of its possibilities. If you're new to the Vue ecosystem and frontend development you may not have an appreciation yet for how much power a tool like Vue CLI provides.