1
|
|
|
package unicon.matthews.security.model.token; |
2
|
|
|
|
3
|
|
|
import io.jsonwebtoken.Claims; |
4
|
|
|
import io.jsonwebtoken.ExpiredJwtException; |
5
|
|
|
import io.jsonwebtoken.Jws; |
6
|
|
|
import io.jsonwebtoken.Jwts; |
7
|
|
|
import io.jsonwebtoken.MalformedJwtException; |
8
|
|
|
import io.jsonwebtoken.SignatureException; |
9
|
|
|
import io.jsonwebtoken.UnsupportedJwtException; |
10
|
|
|
|
11
|
|
|
import org.slf4j.Logger; |
12
|
|
|
import org.slf4j.LoggerFactory; |
13
|
|
|
import org.springframework.security.authentication.BadCredentialsException; |
14
|
|
|
|
15
|
|
|
import unicon.matthews.security.exception.JwtExpiredTokenException; |
16
|
|
|
|
17
|
|
|
public class RawAccessJwtToken implements JwtToken { |
18
|
|
|
private static Logger logger = LoggerFactory.getLogger(RawAccessJwtToken.class); |
19
|
|
|
|
20
|
|
|
private String token; |
21
|
|
|
|
22
|
|
|
public RawAccessJwtToken(String token) { |
23
|
|
|
this.token = token; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Parses and validates JWT Token signature. |
28
|
|
|
* |
29
|
|
|
* @throws BadCredentialsException |
30
|
|
|
* @throws JwtExpiredTokenException |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
public Jws<Claims> parseClaims(String signingKey) { |
34
|
|
|
try { |
35
|
|
|
return Jwts.parser().setSigningKey(signingKey).parseClaimsJws(this.token); |
36
|
|
|
} catch (UnsupportedJwtException | MalformedJwtException | IllegalArgumentException | SignatureException ex) { |
37
|
|
|
logger.error("Invalid JWT Token", ex); |
38
|
|
|
throw new BadCredentialsException("Invalid JWT token: ", ex); |
39
|
|
|
} catch (ExpiredJwtException expiredEx) { |
40
|
|
|
logger.info("JWT Token is expired", expiredEx); |
41
|
|
|
throw new JwtExpiredTokenException(this, "JWT Token expired", expiredEx); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
@Override |
46
|
|
|
public String getToken() { |
47
|
|
|
return token; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|