| 21543 |
ashik.ali |
1 |
package com.spice.profitmandi.common.util;
|
|
|
2 |
|
|
|
3 |
import java.io.UnsupportedEncodingException;
|
|
|
4 |
import java.time.Instant;
|
|
|
5 |
import java.util.Arrays;
|
|
|
6 |
import java.util.Date;
|
| 23780 |
ashik.ali |
7 |
import java.util.HashSet;
|
| 21543 |
ashik.ali |
8 |
import java.util.List;
|
|
|
9 |
import java.util.Map;
|
|
|
10 |
|
| 24490 |
amit.gupta |
11 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
12 |
import org.apache.logging.log4j.Logger;
|
| 21543 |
ashik.ali |
13 |
|
|
|
14 |
import com.auth0.jwt.JWT;
|
|
|
15 |
import com.auth0.jwt.JWTCreator.Builder;
|
|
|
16 |
import com.auth0.jwt.JWTVerifier;
|
|
|
17 |
import com.auth0.jwt.algorithms.Algorithm;
|
|
|
18 |
import com.auth0.jwt.exceptions.InvalidClaimException;
|
|
|
19 |
import com.auth0.jwt.exceptions.JWTCreationException;
|
|
|
20 |
import com.auth0.jwt.exceptions.JWTDecodeException;
|
|
|
21 |
import com.auth0.jwt.interfaces.Claim;
|
|
|
22 |
import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
23 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
|
|
24 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
25 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
26 |
import com.spice.profitmandi.common.model.UserInfo;
|
|
|
27 |
|
|
|
28 |
public class JWTUtil {
|
| 32951 |
amit.gupta |
29 |
private static final String SECRET_KEY = "newsecretkey";
|
| 21543 |
ashik.ali |
30 |
private static final String USER_ID = "userId";
|
|
|
31 |
private static final String EMAIL = "email";
|
|
|
32 |
private static final String PROFIT_MANDI = "profitmandi";
|
|
|
33 |
//60 days
|
|
|
34 |
private static final int EXPIRE_TIME_IN_SECONDS = ((60 * 60)*24)*60;
|
|
|
35 |
private static Algorithm ALGORITHM;
|
| 23568 |
govind |
36 |
private static final Logger LOGGER = LogManager.getLogger(JWTUtil.class);
|
| 21543 |
ashik.ali |
37 |
|
|
|
38 |
static{
|
|
|
39 |
try {
|
|
|
40 |
ALGORITHM = Algorithm.HMAC256(SECRET_KEY);
|
|
|
41 |
} catch (IllegalArgumentException e) {
|
|
|
42 |
// TODO Auto-generated catch block
|
|
|
43 |
e.printStackTrace();
|
|
|
44 |
} catch (UnsupportedEncodingException e) {
|
|
|
45 |
// TODO Auto-generated catch block
|
|
|
46 |
e.printStackTrace();
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
|
| 23846 |
ashik.ali |
50 |
public static String create(int userId, int retailerId, String[] roleIds){
|
| 21543 |
ashik.ali |
51 |
try{
|
|
|
52 |
return createBuilder()
|
| 23846 |
ashik.ali |
53 |
.withClaim(ProfitMandiConstants.USER_ID, userId)
|
|
|
54 |
.withClaim(ProfitMandiConstants.RETAILER_ID, retailerId)
|
| 23780 |
ashik.ali |
55 |
.withArrayClaim(ProfitMandiConstants.ROLE_IDS, roleIds)
|
| 21543 |
ashik.ali |
56 |
.sign(ALGORITHM);
|
|
|
57 |
}catch(JWTCreationException jwtCreationException){
|
|
|
58 |
throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1011"));
|
|
|
59 |
}
|
|
|
60 |
}
|
| 24490 |
amit.gupta |
61 |
|
|
|
62 |
public static String create(String email, int userId, int retailerId, String[] roleIds){
|
|
|
63 |
try{
|
|
|
64 |
return createBuilder()
|
|
|
65 |
.withClaim(ProfitMandiConstants.EMAIL_ID, email)
|
|
|
66 |
.withClaim(ProfitMandiConstants.USER_ID, userId)
|
|
|
67 |
.withClaim(ProfitMandiConstants.RETAILER_ID, retailerId)
|
|
|
68 |
.withArrayClaim(ProfitMandiConstants.ROLE_IDS, roleIds)
|
|
|
69 |
.sign(ALGORITHM);
|
|
|
70 |
}catch(JWTCreationException jwtCreationException){
|
|
|
71 |
throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1011"));
|
|
|
72 |
}
|
|
|
73 |
}
|
| 21543 |
ashik.ali |
74 |
public static String create(String email){
|
|
|
75 |
try{
|
|
|
76 |
return createBuilder().withClaim(EMAIL, email).sign(ALGORITHM);
|
|
|
77 |
}catch(JWTCreationException jwtCreationException){
|
|
|
78 |
throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1011"));
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
private static Builder createBuilder(){
|
|
|
83 |
Instant createTimestamp = Instant.now();
|
|
|
84 |
Instant expireTimestamp = Instant.now().plusSeconds(EXPIRE_TIME_IN_SECONDS);
|
|
|
85 |
//LOGGER.info("Creating token with issuer {}, issuedAt {}, expireAt {}", PROFIT_MANDI, createTimestamp.toString(), expireTimestamp.toString());
|
|
|
86 |
return JWT.create()
|
|
|
87 |
.withIssuer(PROFIT_MANDI)
|
|
|
88 |
.withIssuedAt(Date.from(createTimestamp))
|
|
|
89 |
.withExpiresAt(Date.from(expireTimestamp));
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public static boolean isExpired(String token)
|
|
|
93 |
throws ProfitMandiBusinessException{
|
|
|
94 |
DecodedJWT decodedJWT = parse(token);
|
| 23800 |
amit.gupta |
95 |
Map<String, Claim> claims = decodedJWT.getClaims();
|
|
|
96 |
if(claims.containsKey(USER_ID)){
|
|
|
97 |
final Claim roleIdsClaim = claims.get(ProfitMandiConstants.ROLE_IDS);
|
|
|
98 |
if(roleIdsClaim.isNull()) {
|
|
|
99 |
return true;
|
|
|
100 |
}
|
|
|
101 |
}
|
| 21543 |
ashik.ali |
102 |
Instant expireTime = decodedJWT.getExpiresAt().toInstant();
|
|
|
103 |
Instant currentTime = Instant.now();
|
|
|
104 |
//LOGGER.info("Checking token Expire time of token {} with currentTime {}, expireTime {}", token, currentTime, expireTime);
|
|
|
105 |
if(currentTime.toEpochMilli() > expireTime.toEpochMilli()){
|
|
|
106 |
return true;
|
|
|
107 |
}else{
|
|
|
108 |
return false;
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
public static UserInfo getUserInfo(String token)
|
|
|
113 |
throws ProfitMandiBusinessException{
|
|
|
114 |
DecodedJWT decodedJWT = parse(token);
|
|
|
115 |
Map<String, Claim> claims = decodedJWT.getClaims();
|
|
|
116 |
if(claims.containsKey(USER_ID)){
|
|
|
117 |
final Claim userIdclaim = claims.get(USER_ID);
|
|
|
118 |
int userId = userIdclaim.asInt();
|
| 23846 |
ashik.ali |
119 |
final Claim retailerIdclaim = claims.get(ProfitMandiConstants.RETAILER_ID);
|
|
|
120 |
int retailerId = retailerIdclaim.asInt();
|
| 23780 |
ashik.ali |
121 |
final Claim roleIdsClaim = claims.get(ProfitMandiConstants.ROLE_IDS);
|
| 23801 |
amit.gupta |
122 |
if(roleIdsClaim==null || roleIdsClaim.isNull()) {
|
|
|
123 |
throw new ProfitMandiBusinessException("Token", token, "Invalid Token");
|
|
|
124 |
}
|
| 24490 |
amit.gupta |
125 |
String emailId = null;
|
|
|
126 |
if(claims.containsKey(ProfitMandiConstants.EMAIL_ID)) {
|
|
|
127 |
emailId = claims.get(ProfitMandiConstants.EMAIL_ID).asString();
|
|
|
128 |
}
|
|
|
129 |
final UserInfo userInfo = new UserInfo(userId, retailerId, new HashSet<>(Arrays.asList(roleIdsClaim.asArray(Integer.class))), emailId);
|
| 21543 |
ashik.ali |
130 |
return userInfo;
|
|
|
131 |
}else if(claims.containsKey(EMAIL)){
|
|
|
132 |
final Claim emailClaim = claims.get("email");
|
| 23846 |
ashik.ali |
133 |
final UserInfo userInfo = new UserInfo(-1, -1, null, emailClaim.asString());
|
| 21543 |
ashik.ali |
134 |
return userInfo;
|
|
|
135 |
} else {
|
|
|
136 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.TOKEN, token, "USR_1008");
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
public static List<String> getRoleNames(String token)
|
|
|
141 |
throws ProfitMandiBusinessException{
|
|
|
142 |
DecodedJWT decodedJWT = parse(token);
|
|
|
143 |
Map<String, Claim> claims = decodedJWT.getClaims();
|
| 23780 |
ashik.ali |
144 |
if(claims.containsKey(ProfitMandiConstants.ROLE_IDS)){
|
|
|
145 |
Claim claim = claims.get(ProfitMandiConstants.ROLE_IDS);
|
| 21543 |
ashik.ali |
146 |
return Arrays.asList(claim.asArray(String.class));
|
|
|
147 |
}else{
|
|
|
148 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.TOKEN, token, "USR_1009");
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
private static DecodedJWT parse(String token)
|
|
|
153 |
throws ProfitMandiBusinessException{
|
|
|
154 |
try{
|
|
|
155 |
JWTVerifier verifier = JWT.require(ALGORITHM)
|
| 23429 |
amit.gupta |
156 |
.withIssuer(PROFIT_MANDI).acceptExpiresAt(100000000)
|
| 21543 |
ashik.ali |
157 |
.build(); //Reusable verifier instance
|
|
|
158 |
return verifier.verify(token);
|
|
|
159 |
} catch (JWTDecodeException exception){
|
|
|
160 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.TOKEN, token, "USR_1010");
|
|
|
161 |
} catch(InvalidClaimException invalidClaimException){
|
|
|
162 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.TOKEN, token, "USR_1012");
|
|
|
163 |
}
|
|
|
164 |
}
|
| 23428 |
amit.gupta |
165 |
|
|
|
166 |
public static void main(String[] args) throws Throwable{
|
|
|
167 |
String token = JWTUtil.create("amit.gupta@shop2020.in");
|
|
|
168 |
//System.out.println(token);
|
|
|
169 |
//System.out.println(JWTUtil.isExpired(token));
|
|
|
170 |
//System.out.println(JWTUtil.getUserInfo(token));
|
|
|
171 |
DecodedJWT decodeJwt = JWTUtil.parse("eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJwcm9maXRtYW5kaSIsImV4cCI6MTUxNDk3MDY4OSwiaWF0IjoxNTA5Nzg2Njg5LCJ1c2VySWQiOjMzMjM1LCJyb2xlTmFtZXMiOlsiVVNFUiJdfQ.C1lE6XvGpvQaCISG4IlJKwzEYWa3dWMLn1jXKB7fFvc");
|
|
|
172 |
System.out.println(decodeJwt.getExpiresAt());
|
|
|
173 |
}
|
| 21543 |
ashik.ali |
174 |
}
|