Multi-Tenant Architecture

How to Persist JWT Tokens for Your SaaS Application

Identity management is the first (and probably one of the most important) parts of every multi-tenant SaaS application, because the main idea of multi-tenancy is that a user belongs to one (or more) tenants. Separating users from tenants is one of the most important aspects of multi tenant applications, where several users (with different roles and permissions) can control the same application (tenant).

Multi Tenant Identity Management

While multi-tenancy is great for SaaS applications and end-users, it adds several tiers of complexity to identity management. When we approach the authentication “challenge”, it quickly turns into a checklist of items to handle on both the product side and the technical side. The list below is not exhaustive, but these questions always come up sooner or later:

  • What kind of authentication methods do we support? 
  • Do we allow social logins? Do we allow SSO and SAML?
  • Do we allow the user to login without activating the account via email?
  • What is our password policy? Do we allow each tenant to set a different one?
  • Do we support MFA? Which methods do we allow? Email? SMS? Authenticator app? Do we allow the tenant to force MFA for it’s admins?
  • Do we support server logout?
  • What is the defined session timeout?
  • Will multi tenant authentication work smoothly?
  • How do we implement team management and invites?
  • Do we allow a user to be associated with more than one tenant?

WOW! That’s a lot to think about and to handle.

This series of posts covers, with code samples, the main multi tenant identity management capabilities for SaaS, from the basic to the advanced.

This first post focuses on the basis of multi-tenant SaaS authentication: JWT authentication. We’ll cover what JWTs are, what they contain, and how to persist them, which is what you need to get a feel for how they work.

The Rise of JWT Multi Tenant Authentication

JWT (JSON Web Tokens) is the new de facto authentication method, loved by developers for several reasons. It is an open standard (RFC 7519) and highly trusted because it is digitally signed. JWTs can be signed with secret, public, or private key pairs, depending on your requirements.

JWT is a token generated by the server which includes several claims and a payload that represents the user and the characteristics of the token (when it was issued, when it expires, etc.). The structure of JSON Web Tokens is fixed and made up of Headers, Dynamic Payloads, and Signatures (xxxxx.yyyyy.zzzzz). 

That token is sent to the client upon login and needs to be sent back to each of the servers (or microservices) upon each request, which requires valid authentication. If the token is visible on the client side (and the client can decode it), what about the security aspect? What is stopping the client from generating tokens and impersonating other users?

The most important attribute of JWT tokens is their signature.

Each JWT token is signed on the back end by a private key that can be verified by clients (and other microservices) by the public key. This asymmetric signature method lets us verify that the user is authenticated and not impersonated without the need for maintaining a central tokens database / session management.

This is one of the reasons we like JWT so much. The rise of microservices created the need to decentralize authentication and verification mechanisms, and JWT based authentication fits that need like a glove.

The Contents of JWT tokens for Multi Tenant SaaS Applications

When a user logs into the application, we want to add to the JWT the notion of the tenant so that after the verification of the JWT token, we are in the correct context of the tenant and aren’t exposing our application to cross-tenant security issues.

So the flow is: 

Persisting Tokens

Another issue is how our client application handles the JWT tokens.  

Multi-tenant SaaS applications are built today using web technologies (and most of them are Single Page Applications as well). So we know that we got the JSON Web Token from the authentication endpoint after the login is done. We know that we need to pass it on each request. And we probably want it to last after a page refresh. Why not save it to the local storage?

Saving your JWT tokens on local storage exposes your application to XSS, which has been an OWASP Top 10 regular for many years. With XSS, an attacker’s malicious code can hijack the stored token from local storage, use it to call the back end on behalf of the user, and breach the system.

So, how should we protect ourselves?

  • How about limiting the expiration of JWT tokens? Setting the expiry to 5 minutes? That solves part of the problem but still leaves you exposed.
  • How about saving the token in memory? While this does solve the security problem, it breaks usability as we cannot handle scenarios such as page refresh / new tab.
  • Maybe saving the JWT token as an HTTP-only cookie? That works, but without additional protection it exposes us to CSRF attacks.

Related: OWASP Top Ten Web Application Security Risks

Persisting Through Refresh Tokens and HTTP-Only Cookies

Refresh tokens let us keep refreshing the session and get new JWT tokens. They also let us shorten the lifetime of the JWT tokens while the refresh tokens “live longer”.

Using this approach we can store the JSON Web Tokens in-memory while saving the refresh tokens using http-only cookies. The refresh tokens are not vulnerable to CSRF attacks on form submits because the attacker cannot get the value of the JWT which was returned from the endpoint.

What does the flow look like in this case?

So how will the full flow look upon page load? Because the HTTP-only cookie is persisted via the browser, the flow looks similar by using the same refresh tokens alternative.

Summary

This post covered how to handle JWT token-based authentication in your multi-tenant SaaS application and how to use refresh tokens to safely persist your users’ sessions. The next post shows how to start building a back end that securely stores user details and maintains the user’s activation flows.

Looking to take your User Management to the next level?
Sign up. It's free