Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cognito JWT attribute-based routing

I'm new to AWS and it's services. What I want to achieve is a multi-tenancy SaaS application. What my concept looks like: I use Cognito for user authentication. There all users no matter what tenant they belong to should use one frontend to login. For the tenant-recognition I use a custom attribute "custom:tenant" which I get from the JWT when the login is successful. For the applicantion itself I want to use VPCs and to ensure encapsulation each tenant should have their own VPC.

Example:

  • User A of Tenant 1 login and gets back JWT with claim "custom:tenant":"1" should be routed to VPC 1
  • User B of Tenant 2 login and gets back JWT with claim "custom:tenant":"2" should be routed to VPC 2

Now my question is: how do I achieve this routing from the success of the login to the appropriate VPC? Do I need further Services for that or where do I find these settings?

like image 834
Soteri Avatar asked Dec 01 '21 12:12

Soteri


People also ask

How do I create a JWT authorizer using AWS Cognito?

The following AWS CLI command creates a JWT authorizer that uses Amazon Cognito as an identity provider. For Audience, specify the ID of a client that's associated with the user pool that you specify for Issuer. The following command updates a route to use a JWT authorizer.

What is a custom attribute in AWS Cognito user pool?

With a custom attribute-based multi-tenancy approach, you can generate and add an ID for every user profile as a custom attribute. Custom attributes are useful when you want to add additional user data to AWS Cognito User Pool.

How do I decode a JWT token generated by Amazon Cognito?

Amazon Cognito generates two pairs of RSA cryptographic keys for each user pool. One of the private keys is used to sign the token. Decode the ID token. You can use AWS Lambda to decode user pool JWTs. For more information see Decode and verify Amazon Cognito JWT tokens using Lambda .

How does AWS Cognito work with SAML?

Amazon Cognito handles the SAML response, and maps the SAML attributes to a just-in-time user profile. The SAML groups attribute is mapped to a custom user pool attribute named custom:groups. An AWS Lambda function named PreTokenGeneration reads the custom:groups custom attribute and converts it to a JSON Web Token (JWT) claim named cognito:groups.


Video Answer


1 Answers

There is a standard content based routing technique for routing based on the contents of JWTs. This type of thing is usually managed by a reverse proxy or API gateway placed in front of APIs, which runs some custom logic to read the JWT and route accordingly. This also keeps the plumbing outside of application components.

EXAMPLE

Here is an NGINX example coded in LUA, a high level scripting language, to read the JWT and extract a claim. In this example it is a zone whereas in your case it is a tenant ID:

  • NGINX Configuration
  • NGINX Plugin Code
  • Architecture Article

PREREQUISITES

Not all middleware supports this type of routing though. Eg you won't be able to do it in a simple load balancer. One option might be to use NGINX as a cloud managed service though it will cost money. A good gateway in front of APIs is an important architectural component though, so see if your company feels it is worth investing in.

like image 200
Gary Archer Avatar answered Nov 15 '22 05:11

Gary Archer