Ejemplo n.º 1
0
  public static void authenticateEvernote(
      RenderRequest renderRequest, PortletSession portletSession, ThemeDisplay themeDisplay)
      throws OAuthException {

    HttpServletRequest request = PortalUtil.getHttpServletRequest(renderRequest);
    String authorizationUrl = StringPool.BLANK;

    try {

      OAuthService service = getOAuthService(request, themeDisplay);

      if (PortalUtil.getOriginalServletRequest(request).getParameter(OAUTH_VERIFIER) == null) {
        // Send an OAuth message to the Provider asking for a new Request
        // Token because we don't have access to the current user's account.
        Token scribeRequestToken = service.getRequestToken();

        portletSession.setAttribute(REQUEST_TOKEN, scribeRequestToken.getToken());
        portletSession.setAttribute(REQUEST_TOKEN_SECRET, scribeRequestToken.getSecret());

        authorizationUrl = EVERNOTE_SERVICE.getAuthorizationUrl(scribeRequestToken.getToken());

      } else {
        // Send an OAuth message to the Provider asking to exchange the
        // existing Request Token for an Access Token
        Token scribeRequestToken =
            new Token(
                portletSession.getAttribute(REQUEST_TOKEN).toString(),
                portletSession.getAttribute(REQUEST_TOKEN_SECRET).toString());

        Verifier scribeVerifier =
            new Verifier(
                PortalUtil.getOriginalServletRequest(request).getParameter(OAUTH_VERIFIER));

        Token scribeAccessToken = service.getAccessToken(scribeRequestToken, scribeVerifier);

        EvernoteAuth evernoteAuth =
            EvernoteAuth.parseOAuthResponse(EVERNOTE_SERVICE, scribeAccessToken.getRawResponse());

        portletSession.setAttribute(ACCESS_TOKEN, evernoteAuth.getToken());
      }

    } catch (Exception e) {
      throw new OAuthException(e);
    }

    renderRequest.setAttribute(AUTHORIZATION_URL, authorizationUrl);
  }
Ejemplo 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");
  }