Subversion Repositories SmartDukaan

Rev

Rev 23846 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 23846 Rev 24490
Line 6... Line 6...
6
import java.util.Date;
6
import java.util.Date;
7
import java.util.HashSet;
7
import java.util.HashSet;
8
import java.util.List;
8
import java.util.List;
9
import java.util.Map;
9
import java.util.Map;
10
 
10
 
11
import org.apache.logging.log4j.Logger;
-
 
12
import org.apache.logging.log4j.LogManager;
11
import org.apache.logging.log4j.LogManager;
-
 
12
import org.apache.logging.log4j.Logger;
13
 
13
 
14
import com.auth0.jwt.JWT;
14
import com.auth0.jwt.JWT;
15
import com.auth0.jwt.JWTCreator.Builder;
15
import com.auth0.jwt.JWTCreator.Builder;
16
import com.auth0.jwt.JWTVerifier;
16
import com.auth0.jwt.JWTVerifier;
17
import com.auth0.jwt.algorithms.Algorithm;
17
import com.auth0.jwt.algorithms.Algorithm;
Line 56... Line 56...
56
				.sign(ALGORITHM);
56
				.sign(ALGORITHM);
57
		}catch(JWTCreationException jwtCreationException){
57
		}catch(JWTCreationException jwtCreationException){
58
			throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1011"));
58
			throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1011"));
59
		}
59
		}
60
	}
60
	}
-
 
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
	}
61
	public static String create(String email){
74
	public static String create(String email){
62
		try{
75
		try{
63
			return createBuilder().withClaim(EMAIL, email).sign(ALGORITHM);
76
			return createBuilder().withClaim(EMAIL, email).sign(ALGORITHM);
64
		}catch(JWTCreationException jwtCreationException){
77
		}catch(JWTCreationException jwtCreationException){
65
			throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1011"));
78
			throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1011"));
Line 97... Line 110...
97
	}
110
	}
98
	
111
	
99
	public static UserInfo getUserInfo(String token)
112
	public static UserInfo getUserInfo(String token)
100
		throws ProfitMandiBusinessException{
113
		throws ProfitMandiBusinessException{
101
		DecodedJWT decodedJWT = parse(token);
114
		DecodedJWT decodedJWT = parse(token);
102
		/*Instant expireTime = decodedJWT.getExpiresAt().toInstant();
-
 
103
		Instant currentTime = Instant.now();
-
 
104
		if(currentTime.toEpochMilli() > expireTime.toEpochMilli()) {
-
 
105
			throw new ProfitMandiBusinessException(ProfitMandiConstants.TOKEN, token, "USR_1012");
-
 
106
		}*/
-
 
107
		Map<String, Claim> claims = decodedJWT.getClaims();
115
		Map<String, Claim> claims = decodedJWT.getClaims();
108
		if(claims.containsKey(USER_ID)){
116
		if(claims.containsKey(USER_ID)){
109
			final Claim userIdclaim = claims.get(USER_ID);
117
			final Claim userIdclaim = claims.get(USER_ID);
110
			int userId = userIdclaim.asInt();
118
			int userId = userIdclaim.asInt();
111
			final Claim retailerIdclaim = claims.get(ProfitMandiConstants.RETAILER_ID);
119
			final Claim retailerIdclaim = claims.get(ProfitMandiConstants.RETAILER_ID);
112
			int retailerId = retailerIdclaim.asInt();
120
			int retailerId = retailerIdclaim.asInt();
113
			final Claim roleIdsClaim = claims.get(ProfitMandiConstants.ROLE_IDS);
121
			final Claim roleIdsClaim = claims.get(ProfitMandiConstants.ROLE_IDS);
114
			if(roleIdsClaim==null || roleIdsClaim.isNull()) {
122
			if(roleIdsClaim==null || roleIdsClaim.isNull()) {
115
				throw new ProfitMandiBusinessException("Token", token, "Invalid Token");
123
				throw new ProfitMandiBusinessException("Token", token, "Invalid Token");
116
			}
124
			}
-
 
125
			String emailId = null;
-
 
126
			if(claims.containsKey(ProfitMandiConstants.EMAIL_ID)) {
-
 
127
				emailId =  claims.get(ProfitMandiConstants.EMAIL_ID).asString();
-
 
128
			}
117
			final UserInfo userInfo = new UserInfo(userId, retailerId, new HashSet<>(Arrays.asList(roleIdsClaim.asArray(Integer.class))), null);
129
			final UserInfo userInfo = new UserInfo(userId, retailerId, new HashSet<>(Arrays.asList(roleIdsClaim.asArray(Integer.class))), emailId);
118
			return userInfo;
130
			return userInfo;
119
		}else if(claims.containsKey(EMAIL)){
131
		}else if(claims.containsKey(EMAIL)){
120
			final Claim emailClaim = claims.get("email");
132
			final Claim emailClaim = claims.get("email");
121
			final UserInfo userInfo = new UserInfo(-1, -1, null, emailClaim.asString());
133
			final UserInfo userInfo = new UserInfo(-1, -1, null, emailClaim.asString());
122
			return userInfo;
134
			return userInfo;