I’m using aws-sdk at front-end of my web application. Till now, I’ve set-up the flow to register new users, authenticate users that will get the access token, id token, and refresh token.
I’m confused about what’s next !!!
The access and id tokens are valid for 1 hour and refresh token for 30days, and all are in JWT format.
So, in order to check the log-in status of the user, the access token needs to be parsed to check for the expiration time. Is it possible to do this at front end?
Consequently, if expired then using the refresh token will provide fresh access and id tokens.
Basically, I want to check the validity of the tokens and expiration time to maintain user log-in status.
You can use an RSA signing algorithm instead of HMAC if you want to verify the token with a public key, rather than sharing the HMAC secret, but I suspect you don’t need/want to do this.
You can just decode the token client side and get the exp value.
there are only a few moments you want to update the login status display in the ui.
when user logs in or out
when you receive a unauthorized response from your api
In almost any use case it does not matter that you display that your user’s session is active when technically it has has expired.
You can also send a new token when the user interacts with your api within his timeout period so that if the user is active for a period longer than the timeout period he/she will never notice that timeout period.
This reduces your ‘invalid’ login status display to situations where the user has been idle for a long time
I’d definitely recommend verifying the access token both on the frontend - if a token is found - and the backend when resources e.g. the user’s data, are accessed. The verification procedure is really quick and cheap so don’t worry about load/performance. By performing this verification step, you can be sure that the tokens haven’t been tampered with and you can trust the contents of the token.
You don’t need the secret to validate the tokens given by Cognito as they are asymmetric tokens using a public/private key pair. All you need is the JWKS URL for your cognito pool to verify the integrity of your token to make sure your tokens haven’t been tampered with and you can trust the contents of the tokens.
That’s completely incorrect. You use asymmetric keys so the secret isn’t used after the token is signed. Check out jwt.io for libraries you can use and more info on this topic.
If you use the amplify SDK provided by Amazon then you don’t have to validate the token yourself since the SDK will do it for you and it will also refresh the token automatically.
And yes this happens on the front end (although you can do it on the front end or on the back end).
The amplify Auth package makes all of this dead simple. It automatically checks token expiration and refreshes them for you. I would use that over the AWS sdk directly in javascript.
There is really no need to use refresh tokens, is pretty easy to request a new access token without a refresh token anyway. (Assuming you are signed in to an IDP which in most cases for a spa, you are).