-
Notifications
You must be signed in to change notification settings - Fork 0
/
JwtProvider.java
109 lines (96 loc) · 4.21 KB
/
JwtProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package org.cardanofoundation.authentication.provider;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.security.SignatureException;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.cardanofoundation.authentication.config.RsaConfig;
import org.cardanofoundation.authentication.config.properties.MailProperties;
import org.cardanofoundation.explorer.common.exceptions.BusinessException;
import org.cardanofoundation.explorer.common.exceptions.enums.CommonErrorCode;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
@Log4j2
@RequiredArgsConstructor
public class JwtProvider {
private final RsaConfig rsaConfig;
private final MailProperties mail;
public String generateCodeForVerify(String email) {
return Jwts.builder().setSubject(email).setIssuedAt(new Date())
.setExpiration(new Date((new Date()).getTime() + mail.getExpirationMs()))
.signWith(rsaConfig.getPrivateKeyMail(), SignatureAlgorithm.RS256).compact();
}
public String getAccountIdFromJwtToken(String token) {
return Jwts.parserBuilder().setSigningKey(rsaConfig.getPublicKeyAuth()).build()
.parseClaimsJws(token).getBody().getSubject();
}
@SuppressWarnings("unchecked")
public List<String> getRolesFromJwtToken(String token) {
Claims claims = Jwts.parserBuilder().setSigningKey(rsaConfig.getPublicKeyAuth()).build()
.parseClaimsJws(token).getBody();
Map<String, List<String>> realmAccessMap = (Map<String, List<String>>) claims.get(
"realm_access");
if (Objects.nonNull(realmAccessMap)) {
List<String> allRoles = realmAccessMap.get("roles");
return allRoles.stream().filter(role -> role.startsWith("ROLE_")).toList();
}
return Collections.emptyList();
}
public String getAccountIdFromJwtToken(HttpServletRequest httpServletRequest) {
String token = parseJwt(httpServletRequest);
return Jwts.parserBuilder().setSigningKey(rsaConfig.getPublicKeyAuth()).build()
.parseClaimsJws(token).getBody().getSubject();
}
public String parseJwt(HttpServletRequest request) {
final String headerAuth = request.getHeader("Authorization");
if (StringUtils.hasText(headerAuth) && headerAuth.startsWith("Bearer ")) {
return headerAuth.substring(7);
}
return null;
}
public void validateJwtToken(String token) {
try {
Jwts.parserBuilder().setSigningKey(rsaConfig.getPublicKeyAuth()).build()
.parseClaimsJws(token);
} catch (SignatureException e) {
log.error("Invalid JWT signature: {}", e.getMessage());
throw new BusinessException(CommonErrorCode.TOKEN_INVALID_SIGNATURE);
} catch (MalformedJwtException e) {
log.error("Invalid JWT token: {}", e.getMessage());
throw new BusinessException(CommonErrorCode.INVALID_TOKEN);
} catch (ExpiredJwtException e) {
log.error("JWT token is expired: {}", e.getMessage());
throw new BusinessException(CommonErrorCode.TOKEN_EXPIRED);
} catch (UnsupportedJwtException e) {
log.error("JWT token is unsupported: {}", e.getMessage());
throw new BusinessException(CommonErrorCode.TOKEN_UNSUPPORTED);
} catch (IllegalArgumentException e) {
log.error("JWT claims string is empty: {}", e.getMessage());
throw new BusinessException(CommonErrorCode.TOKEN_IS_NOT_EMPTY);
}
}
public Boolean validateVerifyCode(String code) {
try {
Jwts.parserBuilder().setSigningKey(rsaConfig.getPublicKeyMail()).build().parseClaimsJws(code);
} catch (RuntimeException e) {
log.error("Invalid verify code: {}", e.getMessage());
return Boolean.FALSE;
}
return Boolean.TRUE;
}
public String getAccountIdFromVerifyCode(String code) {
return Jwts.parserBuilder().setSigningKey(rsaConfig.getPublicKeyMail()).build()
.parseClaimsJws(code).getBody().getSubject();
}
}