Cognito: Is there a way to verify JWT tokens at front-end using aws-sdk?

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.

decode the taken the expiration. you don’t want to validate if on the client because that requires the secret

Why would you ever validate in the front end? You need to validate when the token is used in an API…

On a separate note, it’s not recommended to use refresh tokens in SPA front end apps due to risks of exposure through various OWASP attacks.

You should be using either Code flow or implicit flow without refresh tokens.

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.

Do you have a backend which can make these decisions? I don’t think it should be the frontend deciding whether the user is logged-in.

Doing that client side would pose a security risk. I would suggest not doing that.

The front end of any web application should not have access to the token at all.

The token should be stored in a secure http only cookie.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies

Just query the back end to know the login status.

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

With any of the JWTs, you should be able to read the expiry timestamp in the token with an existing JWT Javascript library. You can also verify the integrity of the token using the JWKS URL for your cognito user pool. See https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html

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.

I’m not entirely sure how you have your authentication flow set up to do a refresh when your access token expires but the closest thing I could find was this thread on StackOverflow: https://stackoverflow.com/questions/37442973/cognito-user-pool-how-to-refresh-access-token-using-refresh-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.

See the following package:
https://www.npmjs.com/package/jsonwebtoken

See the following method and options:
https://github.com/auth0/node-jsonwebtoken/blob/master/README.md#jwtdecodetoken--options

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).

Here’s the link: https://aws-amplify.github.io/docs/

Here’s a really good breakdown of how cognito works:

The amplify SDK, which is the way AWS recommends that you authenticate SPAs, uses refresh tokens.

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.

Code flow or implicit flow without refresh tokens

The pkec (pixie) spec has been avail for a while. It can use refresh tokens more securely. (the tokens are not visible in the browser history):

https://www.oauth.com/oauth2-servers/pkce/

Oh, yeah? I did not know that.

Unfortunately, I cannot move to aws amplify now since most part of my code is already written using the js sdk.

How did you register new user through aws-sdk on frontend side?

Do not fall into the sunk cost fallacy. If this needs to run for any amount of time it’s better to redo correctly before launching.

PKCE is definately an option.

However Refresh Tokens are still accessible via XSS/CRSF attacks in spa’s, most recommendations I’ve seen say avoid refresh tokens in SPA’s.

Eg
https://auth0.com/docs/tokens/concepts/refresh-tokens

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).