Exemplo n.º 1
0
  @HandlesEvent("loginGoogle")
  public Resolution loginGoogle() {
    logger.info("Entrando en LoginGoogle....");
    OAuthService service = googleServiceProvider.getService();
    logger.debug("OAuth service----->{}", service.getVersion());

    Token accessToken =
        (Token)
            getContext().getRequest().getSession().getAttribute(ATTR_OAUTH_ACCESS_TOKEN + "Google");
    if (accessToken == null) {
      logger.debug("El access Token es nulo, se crea uno nuevo.");
      Token requestToken = service.getRequestToken();
      getContext()
          .getRequest()
          .getSession()
          .setAttribute(ATTR_OAUTH_REQUEST_TOKEN + "Google", requestToken);

      String url = AUTHORIZE_URL.concat(requestToken.getToken());
      logger.debug("Se redirecciona a la pagina de google: {}", url);
      return new RedirectResolution(url);
    }
    logger.debug("Forward a pagina inicial, el access token esta en sesion: {}", accessToken);

    // coge perfil usuario
    OAuthRequest oauthRequest = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
    logger.debug("Se va a conectar al servicio de google: ");
    service.signRequest(accessToken, oauthRequest);
    Response oauthResponse = oauthRequest.send();

    String body = oauthResponse.getBody();

    ObjectMapper mapper = new ObjectMapper();
    try {

      respuestaJson = mapper.readValue(body, new TypeReference<Map<String, Object>>() {});
    } catch (IOException e) {
      logger.error("El mapeado de Json fallo : {}", e.getMessage());
      getContext()
          .getValidationErrors()
          .addGlobalError(new SimpleError("error.excepcion.jackson", e.getMessage()));
    }

    logger.debug("La respuesta  body: {}", body);
    return new ForwardResolution("/WEB-INF/jsp/google.jsp");
  }
Exemplo n.º 2
0
  @HandlesEvent("callback")
  public Resolution callback() {
    logger.debug("El codigo para verificar es: {}", oauth_verifier);
    // coge request token
    OAuthService service = googleServiceProvider.getService();
    Token requestToken =
        (Token)
            getContext()
                .getRequest()
                .getSession()
                .getAttribute(ATTR_OAUTH_REQUEST_TOKEN + "Google");

    // coge access token
    Verifier verifier = new Verifier(oauth_verifier);
    Token accessToken = service.getAccessToken(requestToken, verifier);

    logger.debug("El access token es: {}", accessToken.getRawResponse());

    // guarda access token en session
    getContext()
        .getRequest()
        .getSession()
        .setAttribute(ATTR_OAUTH_ACCESS_TOKEN + "Google", accessToken);

    // coge perfil usuario
    OAuthRequest oauthRequest = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
    logger.debug("Se va a conectar al servicio de google: ");
    service.signRequest(accessToken, oauthRequest);
    Response oauthResponse = oauthRequest.send();
    String body = oauthResponse.getBody();

    ObjectMapper mapper = new ObjectMapper();
    try {
      respuestaJson = mapper.readValue(body, new TypeReference<Map<String, Object>>() {});

    } catch (IOException e) {
      getContext()
          .getValidationErrors()
          .addGlobalError(new SimpleError("error.excepcion.jackson", e.getMessage()));
    }

    logger.debug("La respuesta  body: {}", oauthResponse.getBody());
    return new ForwardResolution("/WEB-INF/jsp/google.jsp");
  }