Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to parse claims from an expired JWT token?

If we try to parse an expired JWT, results in expired exception.

Is there a way to read claims even the JWT was expired.

Below is used to parse JWT in java:

Jwts.parser().setSigningKey(secret.getBytes()).parseClaimsJws(token).getBody();

like image 765
Sivaprakash Avatar asked Mar 04 '16 08:03

Sivaprakash


People also ask

What happens if JWT token expires?

The JWT access token is only valid for a finite period of time. Using an expired JWT will cause operations to fail.

How do you handle a JWT token expiry?

So in summary when authorization is successful you need to issue two token ACCESS_TOKEN and REFRESH_TOKEN. When ACCESS_TOKEN expires you need to call another api with REFRESH_TOKEN to get new ACCESS_TOKEN. The client application can get a new access token as long as the refresh token is valid and unexpired.

How do you check JWT token is expired or not in node?

how to check whether my token is expired or not? var token = jwt. sign(user,app. get('superSecret'),{ expiresIn : 2 });


1 Answers

There is a better approach to do this. if you see JWT Exception handler object e.g. ExpiredJwtException, expection object itself contains the following:- header, claims and message

so claims can easily extracted through this object i.e. e.getClaims().getId() where e is ExpiredJwtException object.

ExpiredJwtException consturct is as follow:-

public ExpiredJwtException(Header header, Claims claims, String message) {
        super(header, claims, message);
}

Example:-

    try{
        // executable code
   }catch(ExpiredJwtException e){
        System.out.println("token expired for id : " + e.getClaims().getId());
    }
like image 103
Gautam Gupta Avatar answered Oct 22 '22 17:10

Gautam Gupta