Subversion Repositories SmartDukaan

Rev

Rev 33741 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21561 ashik.ali 1
package com.spice.profitmandi.web.util;
2
 
3
import java.io.IOException;
4
import java.util.HashMap;
5
import java.util.Map;
6
 
23505 ashik.ali 7
import org.apache.http.conn.HttpHostConnectException;
31637 tejbeer 8
import org.apache.logging.log4j.LogManager;
9
import org.apache.logging.log4j.Logger;
37913 amit 10
import org.springframework.beans.factory.annotation.Value;
22217 ashik.ali 11
import org.springframework.http.HttpHeaders;
12
import org.springframework.http.MediaType;
21561 ashik.ali 13
import org.springframework.stereotype.Component;
14
 
15
import com.fasterxml.jackson.core.JsonProcessingException;
16
import com.fasterxml.jackson.databind.JsonNode;
17
import com.fasterxml.jackson.databind.ObjectMapper;
18
import com.spice.profitmandi.common.enumuration.SchemeType;
19
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
20
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21
import com.spice.profitmandi.common.web.client.RestClient;
31637 tejbeer 22
import com.spice.profitmandi.web.controller.LoginController;
21561 ashik.ali 23
 
24
@Component
22111 ashik.ali 25
public class GoogleTokenUtil {
31637 tejbeer 26
 
21561 ashik.ali 27
	private static final String V3_HOST_NAME = "www.googleapis.com";
23528 ashik.ali 28
	private static final String V3_URI = "oauth2/v3/tokeninfo";
21561 ashik.ali 29
	private static final int PORT_NUMBER = 443;
30
	private final ObjectMapper objectMapper = new ObjectMapper();
31637 tejbeer 31
 
32
	private static final Logger LOGGER = LogManager.getLogger(GoogleTokenUtil.class);
33
 
37913 amit 34
	@Value("${google.api.key}")
35
	private String googleClientId;
36
 
31637 tejbeer 37
	public String getEmailId(String token) throws ProfitMandiBusinessException {
23528 ashik.ali 38
		RestClient restClient = new RestClient();
21561 ashik.ali 39
		Map<String, String> params = new HashMap<>();
40
		params.put(ProfitMandiConstants.ID_TOKEN, token);
22217 ashik.ali 41
		Map<String, String> headers = new HashMap<>(1);
42
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
23505 ashik.ali 43
		String responseString = null;
21561 ashik.ali 44
		try {
23528 ashik.ali 45
			responseString = restClient.get(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER, V3_URI, params, headers);
23505 ashik.ali 46
		} catch (HttpHostConnectException e) {
47
			throw new ProfitMandiBusinessException("googleServerResponse", responseString, "GE_1006");
48
		}
49
		try {
21561 ashik.ali 50
			JsonNode rootNode = objectMapper.readTree(responseString);
31637 tejbeer 51
 
52
			LOGGER.info("rootNode {}", rootNode);
37913 amit 53
			JsonNode aud = rootNode.get("aud");
54
			JsonNode email = rootNode.get("email");
55
			JsonNode emailVerified = rootNode.get("email_verified");
56
			if (aud == null || !googleClientId.equals(aud.asText())) {
57
				throw new ProfitMandiBusinessException("googleToken", aud == null ? null : aud.asText(), "Google token was not issued for this application");
58
			}
59
			if (email == null || emailVerified == null || !"true".equalsIgnoreCase(emailVerified.asText())) {
60
				throw new ProfitMandiBusinessException("googleToken", email == null ? null : email.asText(), "Google account email is not verified");
61
			}
62
			return email.asText();
31637 tejbeer 63
 
64
		} catch (JsonProcessingException jsonProcessingException) {
21561 ashik.ali 65
			throw new ProfitMandiBusinessException("", "", "VE_1001");
66
		}
67
	}
68
}