| Line 2... |
Line 2... |
| 2 |
|
2 |
|
| 3 |
import java.io.UnsupportedEncodingException;
|
3 |
import java.io.UnsupportedEncodingException;
|
| 4 |
import java.time.Instant;
|
4 |
import java.time.Instant;
|
| 5 |
import java.util.Arrays;
|
5 |
import java.util.Arrays;
|
| 6 |
import java.util.Date;
|
6 |
import java.util.Date;
|
| - |
|
7 |
import java.util.HashSet;
|
| 7 |
import java.util.List;
|
8 |
import java.util.List;
|
| 8 |
import java.util.Map;
|
9 |
import java.util.Map;
|
| 9 |
|
10 |
|
| 10 |
import org.apache.logging.log4j.Logger;
|
11 |
import org.apache.logging.log4j.Logger;
|
| 11 |
import org.apache.logging.log4j.LogManager;
|
12 |
import org.apache.logging.log4j.LogManager;
|
| Line 26... |
Line 27... |
| 26 |
|
27 |
|
| 27 |
public class JWTUtil {
|
28 |
public class JWTUtil {
|
| 28 |
private static final String SECRET_KEY = "secrate";
|
29 |
private static final String SECRET_KEY = "secrate";
|
| 29 |
private static final String USER_ID = "userId";
|
30 |
private static final String USER_ID = "userId";
|
| 30 |
private static final String EMAIL = "email";
|
31 |
private static final String EMAIL = "email";
|
| 31 |
private static final String ROLE_NAMES = "roleNames";
|
- |
|
| 32 |
private static final String PROFIT_MANDI = "profitmandi";
|
32 |
private static final String PROFIT_MANDI = "profitmandi";
|
| 33 |
//60 days
|
33 |
//60 days
|
| 34 |
private static final int EXPIRE_TIME_IN_SECONDS = ((60 * 60)*24)*60;
|
34 |
private static final int EXPIRE_TIME_IN_SECONDS = ((60 * 60)*24)*60;
|
| 35 |
private static Algorithm ALGORITHM;
|
35 |
private static Algorithm ALGORITHM;
|
| 36 |
private static final Logger LOGGER = LogManager.getLogger(JWTUtil.class);
|
36 |
private static final Logger LOGGER = LogManager.getLogger(JWTUtil.class);
|
| Line 45... |
Line 45... |
| 45 |
// TODO Auto-generated catch block
|
45 |
// TODO Auto-generated catch block
|
| 46 |
e.printStackTrace();
|
46 |
e.printStackTrace();
|
| 47 |
}
|
47 |
}
|
| 48 |
}
|
48 |
}
|
| 49 |
|
49 |
|
| 50 |
public static String create(int userId, String[] roleNames){
|
50 |
public static String create(int userId, String[] roleIds){
|
| 51 |
try{
|
51 |
try{
|
| 52 |
return createBuilder()
|
52 |
return createBuilder()
|
| 53 |
.withClaim(USER_ID, userId)
|
53 |
.withClaim(USER_ID, userId)
|
| 54 |
.withArrayClaim(ROLE_NAMES, roleNames)
|
54 |
.withArrayClaim(ProfitMandiConstants.ROLE_IDS, roleIds)
|
| 55 |
.sign(ALGORITHM);
|
55 |
.sign(ALGORITHM);
|
| 56 |
}catch(JWTCreationException jwtCreationException){
|
56 |
}catch(JWTCreationException jwtCreationException){
|
| 57 |
throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1011"));
|
57 |
throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1011"));
|
| 58 |
}
|
58 |
}
|
| 59 |
}
|
59 |
}
|
| Line 98... |
Line 98... |
| 98 |
}*/
|
98 |
}*/
|
| 99 |
Map<String, Claim> claims = decodedJWT.getClaims();
|
99 |
Map<String, Claim> claims = decodedJWT.getClaims();
|
| 100 |
if(claims.containsKey(USER_ID)){
|
100 |
if(claims.containsKey(USER_ID)){
|
| 101 |
final Claim userIdclaim = claims.get(USER_ID);
|
101 |
final Claim userIdclaim = claims.get(USER_ID);
|
| 102 |
int userId = userIdclaim.asInt();
|
102 |
int userId = userIdclaim.asInt();
|
| 103 |
final Claim roleNamesClaim = claims.get(ROLE_NAMES);
|
103 |
final Claim roleIdsClaim = claims.get(ProfitMandiConstants.ROLE_IDS);
|
| 104 |
final UserInfo userInfo = new UserInfo(userId, Arrays.asList(roleNamesClaim.asArray(String.class)), null);
|
104 |
final UserInfo userInfo = new UserInfo(userId, new HashSet<>(Arrays.asList(roleIdsClaim.asArray(Integer.class))), null);
|
| 105 |
return userInfo;
|
105 |
return userInfo;
|
| 106 |
}else if(claims.containsKey(EMAIL)){
|
106 |
}else if(claims.containsKey(EMAIL)){
|
| 107 |
final Claim emailClaim = claims.get("email");
|
107 |
final Claim emailClaim = claims.get("email");
|
| 108 |
final UserInfo userInfo = new UserInfo(-1, null, emailClaim.asString());
|
108 |
final UserInfo userInfo = new UserInfo(-1, null, emailClaim.asString());
|
| 109 |
return userInfo;
|
109 |
return userInfo;
|
| Line 114... |
Line 114... |
| 114 |
|
114 |
|
| 115 |
public static List<String> getRoleNames(String token)
|
115 |
public static List<String> getRoleNames(String token)
|
| 116 |
throws ProfitMandiBusinessException{
|
116 |
throws ProfitMandiBusinessException{
|
| 117 |
DecodedJWT decodedJWT = parse(token);
|
117 |
DecodedJWT decodedJWT = parse(token);
|
| 118 |
Map<String, Claim> claims = decodedJWT.getClaims();
|
118 |
Map<String, Claim> claims = decodedJWT.getClaims();
|
| 119 |
if(claims.containsKey(ROLE_NAMES)){
|
119 |
if(claims.containsKey(ProfitMandiConstants.ROLE_IDS)){
|
| 120 |
Claim claim = claims.get(ROLE_NAMES);
|
120 |
Claim claim = claims.get(ProfitMandiConstants.ROLE_IDS);
|
| 121 |
return Arrays.asList(claim.asArray(String.class));
|
121 |
return Arrays.asList(claim.asArray(String.class));
|
| 122 |
}else{
|
122 |
}else{
|
| 123 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.TOKEN, token, "USR_1009");
|
123 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.TOKEN, token, "USR_1009");
|
| 124 |
}
|
124 |
}
|
| 125 |
}
|
125 |
}
|