authorized users only: cognito and JS how to?

Everyone, I need a refresher on the topic in general, but especially with Cognito thrown into the mix.

I am building a severless web app and have the whole, signup, confirmation, and login thing working.

I get back access tokens that seem to be automatically put into local storage.

My question is, how am I supposed to work with that? Does the same user always get the same access token (I assume not)?

What I basically want is someone to check, if this user is authenticated or not. If they are they can see this content, if they are not they should not be here.

I am thinking like a $(‘document’).ready(function(){}); or w.e. But what I am I checking for? I can’t just check if they have an access token or because someone could just give me random one.

I would like to just have a local storage like a boolean for is authenticated, but I assume someone could fool that as well.

I need to check that the user has the right access token… Or any other suggestions?


Related discussions:

You should never check the authentication status on the client side. You might check whether the current token is valid in order to alert the user that they need to reauthenticate, but if an attacker was trying to access sensitive information they could trivially modify your JS code to bypass the check. You can’t trust code running in the browser, full stop.

What you want to do is store all sensitive data server-side (probably in DynamoDB if you are building a Serverless web app.) Then you will create an API Gateway backed by Lambda functions that retrieve the necessary data. Your client application will pass the identity token you get from Cognito to the API in the Authorization HTTP header and you can configure API Gateway to verify this token automatically using a built in Cognito User Pools authorizer.

The official AWS Serverless web application workshop on GitHub walks through a full example of how to configure this.

Ok, thank you. But I don’t think I understand quite yet.

What you suggest is half of the issue to me? I still have to make sure that the client has the correct token.

How do I know that the client’s token matches the username? When a user logs in Cognito creates tokens and puts them in local storage. What I want to do is verify that those tokens are as they should be.

But I can’t visualize what that verification process looks like. How do I get that token on the backend to check it against the one in local storage on the client?

Generally you wouldn’t check the tokens on the client side because your whole system is predicated on the fact the Cognito is trustworthy. So when your user logs in and your app fetches the tokens from Cognito, they are pretty much by definition the correct ones.

For your server-side api, the easiest thing to do is to use API Gateway’s built in Cognito authorizer which will validate the tokens and add all of their claims to the context of the request so that your backend can determine the identity (e.g. the username) of the requesting principal. If you want to do the verification yourself, you should look up how JWT token signing and verification works, but generally any language you’re using will have a JWT library you can use to parse and validate the token from Cognito using the published public key from the Cognito service.

https://aws.amazon.com/premiumsupport/knowledge-center/decode-verify-cognito-json-token/

EDIT: I just reread your comment and it makes me think maybe you don’t quite understand how Cognito is supposed to work so let me give you this explanation. Imagine you own a bar but you don’t want to let anyone in who hasn’t bought a pre-paid membership. You can think of Cognito as an outsourced membership management company. But there’s a problem, Cognito has an office down the street from your bar and you don’t have time to walk down there with each person who wants to get into your establishment to ask if they are in fact a member in good standing. So instead you ask Cognito to stamp people’s hand with a special glow in the dark ink that can’t be forged. So now people who want to come to your bar first go down the block to Cognito’s office, prove their identity and get their stamp. When they come to your door now all you have to do is check their stamp. If it’s got the special ink, you know they are good, you don’t even have to go back and ask Cognito about it, you can just trust the token. To extend the metaphor a bit further, the stamp on their hand fades away pretty quickly, so periodically people have to go back and re-prove their identity to Cognito in order to get a new stamp. But for you, all you have to do is make sure the stamp is fresh and uses the right ink.

In this metaphor the stamp is the JWT Cognito provides that gets stored in local storage. Local storage is essentially the back of each person’s hand here. The special ink is a very bad analogy for a cryptographic signature, but there aren’t really any good physical analogs for most crypto concepts. The other piece that I didn’t really include is the fact that these stamps aren’t the same for everyone. They have each person’s username and other information about them embedded in the stamp itself. In the real world these are what are know as JWT claims. Finally the bouncer at the front door who is checking these stamps is API Gateway and specifically the Cognito authorizer you configured within it.

The last thing I’ll note that might also be tripping you up is that you need to stop thinking of the JavaScript application that runs in the user’s browser as part of your system. That belongs to the user. You wrote it for them because you are a nice guy, but they could just as easily build their own client to access your API since Cognito doesn’t care what app is asking for tokens, it just cares that the user properly proves his identity.

Hopefully that makes things a little clearer.