Subversion Repositories SmartDukaan

Rev

Rev 33741 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.conn.HttpHostConnectException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spice.profitmandi.common.enumuration.SchemeType;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.web.controller.LoginController;

@Component
public class GoogleTokenUtil {

        private static final String V3_HOST_NAME = "www.googleapis.com";
        private static final String V3_URI = "oauth2/v3/tokeninfo";
        private static final int PORT_NUMBER = 443;
        private final ObjectMapper objectMapper = new ObjectMapper();

        private static final Logger LOGGER = LogManager.getLogger(GoogleTokenUtil.class);

        @Value("${google.api.key}")
        private String googleClientId;

        public String getEmailId(String token) throws ProfitMandiBusinessException {
                RestClient restClient = new RestClient();
                Map<String, String> params = new HashMap<>();
                params.put(ProfitMandiConstants.ID_TOKEN, token);
                Map<String, String> headers = new HashMap<>(1);
                headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
                String responseString = null;
                try {
                        responseString = restClient.get(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER, V3_URI, params, headers);
                } catch (HttpHostConnectException e) {
                        throw new ProfitMandiBusinessException("googleServerResponse", responseString, "GE_1006");
                }
                try {
                        JsonNode rootNode = objectMapper.readTree(responseString);

                        LOGGER.info("rootNode {}", rootNode);
                        JsonNode aud = rootNode.get("aud");
                        JsonNode email = rootNode.get("email");
                        JsonNode emailVerified = rootNode.get("email_verified");
                        if (aud == null || !googleClientId.equals(aud.asText())) {
                                throw new ProfitMandiBusinessException("googleToken", aud == null ? null : aud.asText(), "Google token was not issued for this application");
                        }
                        if (email == null || emailVerified == null || !"true".equalsIgnoreCase(emailVerified.asText())) {
                                throw new ProfitMandiBusinessException("googleToken", email == null ? null : email.asText(), "Google account email is not verified");
                        }
                        return email.asText();

                } catch (JsonProcessingException jsonProcessingException) {
                        throw new ProfitMandiBusinessException("", "", "VE_1001");
                }
        }
}