Package org.hoteia.qalingo.core.web.mvc.controller.oauth

Source Code of org.hoteia.qalingo.core.web.mvc.controller.oauth.ConnectGoogleAccountController

/**
* Most of the code in the Qalingo project is copyrighted Hoteia and licensed
* under the Apache License Version 2.0 (release version 0.8.0)
*         http://www.apache.org/licenses/LICENSE-2.0
*
*                   Copyright (c) Hoteia, 2012-2014
* http://www.hoteia.com - http://twitter.com/hoteia - contact@hoteia.com
*
*/
package org.hoteia.qalingo.core.web.mvc.controller.oauth;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hoteia.qalingo.core.domain.EngineSetting;
import org.hoteia.qalingo.core.domain.EngineSettingValue;
import org.hoteia.qalingo.core.domain.enumtype.FoUrls;
import org.hoteia.qalingo.core.domain.enumtype.OAuthType;
import org.hoteia.qalingo.core.pojo.RequestData;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.Google2Api;
import org.scribe.oauth.OAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
*
*/
@Controller("connectGoogleAccountController")
public class ConnectGoogleAccountController extends AbstractOAuthFrontofficeController {

  protected final Logger logger = LoggerFactory.getLogger(getClass());

  @RequestMapping("/connect-oauth-google-account.html*")
  public ModelAndView connectGoogleAccount(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final RequestData requestData = requestUtil.getRequestData(request);
   
    // SANITY CHECK
    if(!requestUtil.hasKnownCustomerLogged(request)){
      try {
          // CLIENT ID
          EngineSetting clientIdEngineSetting = engineSettingService.getSettingOAuthAppKeyOrId();
          EngineSettingValue clientIdEngineSettingValue = clientIdEngineSetting.getEngineSettingValue(OAuthType.GOOGLE_ACCOUNT.name());
         
          // CLIENT SECRET
          EngineSetting clientSecretEngineSetting = engineSettingService.getSettingOAuthAppSecret();
          EngineSettingValue clientSecretEngineSettingValue = clientSecretEngineSetting.getEngineSettingValue(OAuthType.GOOGLE_ACCOUNT.name());
         
          // CLIENT PERMISSIONS
          EngineSetting permissionsEngineSetting = engineSettingService.getSettingOAuthAppPermissions();
          EngineSettingValue permissionsEngineSettingValue = permissionsEngineSetting.getEngineSettingValue(OAuthType.GOOGLE_ACCOUNT.name());
         
          if(clientIdEngineSettingValue != null
              && clientSecretEngineSetting != null
              && permissionsEngineSettingValue != null){
          final String clientId = clientIdEngineSettingValue.getValue();
          final String clientSecret = clientSecretEngineSettingValue.getValue();
          final String permissions = permissionsEngineSettingValue.getValue();
         
            final String googleAccountCallBackURL = urlService.buildAbsoluteUrl(requestData, urlService.buildOAuthCallBackUrl(requestData, OAuthType.GOOGLE_ACCOUNT.getPropertyKey().toLowerCase()));

              OAuthService service = new ServiceBuilder()
              .provider(Google2Api.class)
              .apiKey(clientId)
              .apiSecret(clientSecret)
              .scope(permissions)
                .callback(googleAccountCallBackURL)
                .build();

                    // Obtain the Authorization URL
                    String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);

                    response.sendRedirect(authorizationUrl);
          }

      } catch (Exception e) {
        logger.error("Connect With " + OAuthType.GOOGLE_ACCOUNT.name() + " failed!");
      }
    }

    // DEFAULT FALLBACK VALUE
    if(!response.isCommitted()){
      response.sendRedirect(urlService.generateUrl(FoUrls.LOGIN, requestData));
    }
   
    return null;
  }
 
}
TOP

Related Classes of org.hoteia.qalingo.core.web.mvc.controller.oauth.ConnectGoogleAccountController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.