| 21277 |
ashik.ali |
1 |
package com.spice.profitmandi.web.processor;
|
|
|
2 |
|
|
|
3 |
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
4 |
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
5 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
| 21556 |
ashik.ali |
6 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
| 21277 |
ashik.ali |
7 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
8 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
9 |
import com.spice.profitmandi.common.util.JWTUtil;
|
| 21556 |
ashik.ali |
10 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 24491 |
amit.gupta |
11 |
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
|
| 27043 |
amit.gupta |
12 |
import com.spice.profitmandi.dao.repository.cs.CsService;
|
| 30295 |
amit.gupta |
13 |
import com.spice.profitmandi.dao.repository.dtr.*;
|
| 24491 |
amit.gupta |
14 |
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
|
|
|
15 |
import com.spice.profitmandi.service.AuthService;
|
| 26590 |
amit.gupta |
16 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 30295 |
amit.gupta |
17 |
import org.apache.http.conn.HttpHostConnectException;
|
|
|
18 |
import org.apache.logging.log4j.LogManager;
|
|
|
19 |
import org.apache.logging.log4j.Logger;
|
|
|
20 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
21 |
import org.springframework.http.HttpHeaders;
|
|
|
22 |
import org.springframework.http.MediaType;
|
|
|
23 |
import org.springframework.stereotype.Component;
|
| 21277 |
ashik.ali |
24 |
|
| 30295 |
amit.gupta |
25 |
import java.io.IOException;
|
|
|
26 |
import java.util.HashMap;
|
|
|
27 |
import java.util.List;
|
|
|
28 |
import java.util.Map;
|
|
|
29 |
|
| 21282 |
ashik.ali |
30 |
@Component
|
| 21277 |
ashik.ali |
31 |
public class GoogleLoginProcessor {
|
| 24491 |
amit.gupta |
32 |
|
| 21556 |
ashik.ali |
33 |
private static final String V1_HOST_NAME = "content.googleapis.com";
|
|
|
34 |
private static final String V1_URI = "/plus/v1/people/me";
|
|
|
35 |
private static final String V3_HOST_NAME = "www.googleapis.com";
|
|
|
36 |
private static final String V3_URI = "/oauth2/v3/tokeninfo";
|
| 24491 |
amit.gupta |
37 |
private static final Logger LOGGER = LogManager.getLogger(GoogleLoginProcessor.class);
|
| 21277 |
ashik.ali |
38 |
private static final int PORT_NUMBER = 443;
|
|
|
39 |
private final ObjectMapper objectMapper = new ObjectMapper();
|
| 24491 |
amit.gupta |
40 |
|
| 21278 |
ashik.ali |
41 |
@Autowired
|
| 22931 |
ashik.ali |
42 |
private SocialUserRepository socialUserRepository;
|
| 24491 |
amit.gupta |
43 |
|
| 21278 |
ashik.ali |
44 |
@Autowired
|
| 22931 |
ashik.ali |
45 |
private UserRepository userRepository;
|
| 24491 |
amit.gupta |
46 |
|
|
|
47 |
@Autowired
|
|
|
48 |
private AuthService authService;
|
| 25388 |
tejbeer |
49 |
|
| 22011 |
ashik.ali |
50 |
@Autowired
|
| 25388 |
tejbeer |
51 |
com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
|
|
|
52 |
|
|
|
53 |
@Autowired
|
| 24491 |
amit.gupta |
54 |
private AuthRepository authRepository;
|
| 25388 |
tejbeer |
55 |
|
| 26590 |
amit.gupta |
56 |
|
| 23860 |
ashik.ali |
57 |
@Autowired
|
| 24491 |
amit.gupta |
58 |
private UserRoleRepository userRoleRepository;
|
|
|
59 |
|
|
|
60 |
@Autowired
|
|
|
61 |
private PromoterRepository promoterRepository;
|
|
|
62 |
|
|
|
63 |
@Autowired
|
| 23860 |
ashik.ali |
64 |
private UserAccountRepository userAccountRepository;
|
| 27043 |
amit.gupta |
65 |
|
|
|
66 |
@Autowired
|
| 28094 |
amit.gupta |
67 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
68 |
|
|
|
69 |
@Autowired
|
| 27043 |
amit.gupta |
70 |
private CsService csService;
|
| 24491 |
amit.gupta |
71 |
|
| 23860 |
ashik.ali |
72 |
@Autowired
|
|
|
73 |
private RestClient restClient;
|
| 26590 |
amit.gupta |
74 |
@Autowired
|
|
|
75 |
private RetailerService retailerService;
|
| 25388 |
tejbeer |
76 |
|
| 30295 |
amit.gupta |
77 |
public String process(String token) throws ProfitMandiBusinessException {
|
| 21556 |
ashik.ali |
78 |
Map<String, String> params = new HashMap<>();
|
|
|
79 |
params.put(ProfitMandiConstants.ID_TOKEN, token);
|
| 26673 |
amit.gupta |
80 |
Map<String, String> headers = new HashMap<>();
|
| 22355 |
ashik.ali |
81 |
headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
| 23860 |
ashik.ali |
82 |
String responseString = null;
|
| 21556 |
ashik.ali |
83 |
try {
|
| 23532 |
amit.gupta |
84 |
responseString = restClient.get(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER, V3_URI, params, headers);
|
| 23860 |
ashik.ali |
85 |
} catch (HttpHostConnectException e) {
|
|
|
86 |
// TODO Auto-generated catch block
|
|
|
87 |
e.printStackTrace();
|
| 23532 |
amit.gupta |
88 |
}
|
|
|
89 |
try {
|
| 21556 |
ashik.ali |
90 |
JsonNode rootNode = objectMapper.readTree(responseString);
|
| 26396 |
amit.gupta |
91 |
String email = rootNode.get("email").asText();
|
|
|
92 |
|
| 30295 |
amit.gupta |
93 |
return email;
|
| 24491 |
amit.gupta |
94 |
} catch (JsonProcessingException jsonProcessingException) {
|
| 21556 |
ashik.ali |
95 |
throw new ProfitMandiBusinessException("", "", "VE_1001");
|
|
|
96 |
}
|
|
|
97 |
}
|
| 26590 |
amit.gupta |
98 |
|
|
|
99 |
public Map<String, Object> processStore(String storeCode) throws ProfitMandiBusinessException {
|
|
|
100 |
Map<String, Object> responseMap = new HashMap<>();
|
| 26598 |
amit.gupta |
101 |
int retailerId = retailerService.getStoreCodeRetailerMap().get(storeCode);
|
| 26590 |
amit.gupta |
102 |
int userId = userAccountRepository.selectUserIdByRetailerId(retailerId);
|
|
|
103 |
List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(userId);
|
|
|
104 |
|
|
|
105 |
String[] roleTypes = new String[roleIds.size()];
|
|
|
106 |
int index = 0;
|
|
|
107 |
for (int roleId : roleIds) {
|
|
|
108 |
roleTypes[index++] = String.valueOf(roleId);
|
|
|
109 |
}
|
|
|
110 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(userId, retailerId, roleTypes));
|
|
|
111 |
responseMap.put(ProfitMandiConstants.REGISTERED, true);
|
|
|
112 |
return responseMap;
|
|
|
113 |
}
|
| 21277 |
ashik.ali |
114 |
}
|