Package org.apache.shindig.gadgets.oauth2.handler

Examples of org.apache.shindig.gadgets.oauth2.handler.OAuth2HandlerError


      final GrantRequestHandler grantRequestHandler, final String completeAuthUrl) {
    final String method = "authorize";
    log.entering(CLASS, method, new Object[] {
          accessor, grantRequestHandler, completeAuthUrl });
     
    OAuth2HandlerError ret = null;

    HttpRequest authorizationRequest;
    try {
      authorizationRequest = grantRequestHandler.getAuthorizationRequest(accessor, completeAuthUrl);
    } catch (final OAuth2RequestException e) {
      authorizationRequest = null;
      ret = new OAuth2HandlerError(e.getError(), e.getErrorText(), e);
    }

    if (authorizationRequest != null) {
      HttpResponse authorizationResponse;
      try {
        authorizationResponse = this.fetcher.fetch(authorizationRequest);
      } catch (final GadgetException e) {
        log.logp(Level.WARNING, CLASS, method, "Exception while making authorizating request", e);
        authorizationResponse = null;
        ret = new OAuth2HandlerError(OAuth2Error.AUTHORIZE_PROBLEM,
            "exception thrown fetching authorization", e);
      }

      if (authorizationResponse != null) {
        if (grantRequestHandler.isAuthorizationEndpointResponse()) {
View Full Code Here


  }

  private OAuth2HandlerError refreshToken(final DominoOAuth2Accessor accessor) {
    final String method = "refreshToken";
    log.entering(CLASS, method, new Object[] { accessor });
    OAuth2HandlerError ret = null;
    String refershTokenUrl;
    refershTokenUrl = buildRefreshTokenUrl(accessor);

    if (log.isLoggable(Level.FINEST)) {
      log.logp(Level.FINEST, CLASS, method, "refershTokenUrl = {0}", new Object[]{refershTokenUrl});
    }

    if (refershTokenUrl != null) {
      HttpResponse response = null;
      final HttpRequest request = new HttpRequest(Uri.parse(refershTokenUrl));
      request.setSecurityToken(new AnonymousSecurityToken("", 0L, accessor.getGadgetUri()));
      request.setMethod("POST");
      request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

      for (final ClientAuthenticationHandler clientAuthenticationHandler : this.clientAuthenticationHandlers) {
        if (clientAuthenticationHandler.geClientAuthenticationType().equalsIgnoreCase(
            accessor.getClientAuthenticationType())) {
          clientAuthenticationHandler.addOAuth2Authentication(request, accessor);
        }
      }

      try {
        final byte[] body = getRefreshBody(accessor).getBytes("UTF-8");
        request.setPostBody(body);
      } catch (final Exception e) {
        log.logp(Level.WARNING, CLASS, method, "Error while getting body for refresh request.", e);
        ret = new OAuth2HandlerError(OAuth2Error.REFRESH_TOKEN_PROBLEM,
            "error generating refresh body", e);
      }

      if (!isUriAllowed(request.getUri(), accessor.getAllowedDomains())) {
        ret = new OAuth2HandlerError(OAuth2Error.REFRESH_TOKEN_PROBLEM,
            "error fetching refresh token - domain not allowed", null);
      }

      if (ret == null) {
        try {
          response = this.fetcher.fetch(request);
        } catch (final GadgetException e) {
          log.logp(Level.WARNING, CLASS, method, "Error while making refresh request.", e);
          ret = new OAuth2HandlerError(OAuth2Error.REFRESH_TOKEN_PROBLEM,
              "error fetching refresh token", e);
        }

        if (log.isLoggable(Level.FINEST)) {
          log.logp(Level.FINEST, CLASS, method, "response = {0}", new Object[] {response});
        }

        if (ret == null) {
          // response is not null..
          final int statusCode = response.getHttpStatusCode();
          if (statusCode == HttpResponse.SC_UNAUTHORIZED
              || statusCode == HttpResponse.SC_BAD_REQUEST) {
            try {
              this.tokenStore.removeRefreshToken(accessor);
            } catch (final GadgetException e) {
              ret = new OAuth2HandlerError(OAuth2Error.REFRESH_TOKEN_PROBLEM,
                  "failed to remove refresh token", e);
            }
            accessor.setRefreshToken(null);
            if (log.isLoggable(Level.FINEST)) {
              log.logp(Level.FINEST, CLASS, method,
                  "received {0} from provider, removed refresh token.  response = {1}",
                  new Object[] { statusCode, response.getResponseAsString() });
            }
            return ret;
          } else if (statusCode != HttpResponse.SC_OK) {
            ret = new OAuth2HandlerError(OAuth2Error.REFRESH_TOKEN_PROBLEM,
                "bad response from server : " + statusCode, null, "",
                response.getResponseAsString());
          }

          if (ret == null) {
            for (final TokenEndpointResponseHandler tokenEndpointResponseHandler : this.tokenEndpointResponseHandlers) {
              if (tokenEndpointResponseHandler.handlesResponse(accessor, response)) {
                final OAuth2HandlerError error = tokenEndpointResponseHandler.handleResponse(
                    accessor, response);
                if (error != null) {
                  try {
                    this.tokenStore.removeRefreshToken(accessor);
                  } catch (GadgetException e) {
View Full Code Here

    }
    try {
      boolean foundHandler = false;
      for (final AuthorizationEndpointResponseHandler authorizationEndpointResponseHandler : this.authorizationEndpointResponseHandlers) {
        if (authorizationEndpointResponseHandler.handlesRequest(accessor, request)) {
          final OAuth2HandlerError handlerError = authorizationEndpointResponseHandler
              .handleRequest(accessor, request);
          if (handlerError != null) {
            sendError(handlerError.getError(),
                handlerError.getContextMessage(), handlerError.getDescription(),
                handlerError.getUri(), accessor, resp, handlerError.getCause(),
                this.sendTraceToClient);
            return;
          }
          foundHandler = true;
          break;
View Full Code Here

  @Override
  public OAuth2HandlerError handleResponse(final OAuth2Accessor oa2Accessor,
      final HttpResponse response) {
    final String method = "handleResponse";
    log.entering(CLASS, method, oa2Accessor);
    OAuth2HandlerError ret = null;

    try {
      if (response == null) {
        ret = getError("response is null");
      }
View Full Code Here

    return getError(contextMessage, null, "", "");
  }

  private OAuth2HandlerError getError(final String contextMessage, final Exception e,
      final String uri, final String description) {
    return new OAuth2HandlerError(OAuth2Error.TOKEN_RESPONSE_PROBLEM, contextMessage, e, uri,
        description);
  }
View Full Code Here

      } else {
        // We don't have an access token, we need to try and get one.
        // First step see if we have a refresh token
        if (BasicOAuth2Request.haveRefreshToken(accessor) != null) {
          if (BasicOAuth2Request.checkCanRefresh()) {
            final OAuth2HandlerError handlerError = this.refreshToken(accessor);
            if (handlerError == null) {
              // No errors refreshing, attempt the fetch again.
              this.store.removeOAuth2Accessor(accessor);
              this.internalAccessor.invalidate();
              this.internalAccessor = null;
              ret = this.attemptFetch(this.getAccessor());
            } else {
              // There was an error refreshing, stop.
              final OAuth2Error error = handlerError.getError();
              ret = this.getErrorResponseBuilder(handlerError.getCause(), error,
                      handlerError.getContextMessage(), "");
            }
          } else {
            // User cannot refresh, they'll have to try to authorize again.
            accessor.setRefreshToken(null);
            ret = this.attemptFetch(accessor);
View Full Code Here

        }
      }
      if (grantRequestHandlerUsed.isRedirectRequired()) {
        ret = completeAuthUrl;
      } else {
        final OAuth2HandlerError error = this.authorize(accessor, grantRequestHandlerUsed,
                completeAuthUrl);
        if (error != null) {
          accessor.setErrorResponse(error.getCause(), OAuth2Error.AUTHENTICATION_PROBLEM,
                  error.getContextMessage(), "");
        }
      }
    }

    if (isLogging) {
View Full Code Here

    if (isLogging) {
      BasicOAuth2Request.LOG.entering(BasicOAuth2Request.LOG_CLASS, "authorize", new Object[] {
              accessor, grantRequestHandler, completeAuthUrl });
    }

    OAuth2HandlerError ret = null;

    HttpRequest authorizationRequest;
    try {
      authorizationRequest = grantRequestHandler.getAuthorizationRequest(accessor, completeAuthUrl);
    } catch (final OAuth2RequestException e) {
      authorizationRequest = null;
      ret = new OAuth2HandlerError(e.getError(), e.getErrorText(), e);
    }

    if (isLogging) {
      BasicOAuth2Request.LOG.log("authorizationRequest = {0}", authorizationRequest);
    }

    if (authorizationRequest != null) {
      HttpResponse authorizationResponse;
      try {
        authorizationResponse = this.fetcher.fetch(authorizationRequest);
      } catch (final GadgetException e) {
        if (isLogging) {
          BasicOAuth2Request.LOG.log("authorize()", e);
        }
        authorizationResponse = null;
        ret = new OAuth2HandlerError(OAuth2Error.AUTHORIZE_PROBLEM,
                "exception thrown fetching authorization", e);
      }

      if (isLogging) {
        BasicOAuth2Request.LOG.log("authorizationResponse = {0}", authorizationResponse);
View Full Code Here

    if (isLogging) {
      BasicOAuth2Request.LOG.entering(BasicOAuth2Request.LOG_CLASS, "refreshToken",
              new Object[] { accessor });
    }

    OAuth2HandlerError ret = null;

    String refershTokenUrl;

    refershTokenUrl = BasicOAuth2Request.buildRefreshTokenUrl(accessor);

    if (isLogging) {
      BasicOAuth2Request.LOG.log("refershTokenUrl = {0}", refershTokenUrl);
    }

    if (refershTokenUrl != null) {
      HttpResponse response = null;
      final HttpRequest request = new HttpRequest(Uri.parse(refershTokenUrl));
      request.setMethod("POST");
      request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

      for (final ClientAuthenticationHandler clientAuthenticationHandler : this.clientAuthenticationHandlers) {
        if (clientAuthenticationHandler.geClientAuthenticationType().equalsIgnoreCase(
                accessor.getClientAuthenticationType())) {
          clientAuthenticationHandler.addOAuth2Authentication(request, accessor);
        }
      }

      try {
        final byte[] body = BasicOAuth2Request.getRefreshBody(accessor).getBytes("UTF-8");
        request.setPostBody(body);
      } catch (final Exception e) {
        if (isLogging) {
          BasicOAuth2Request.LOG.log("refreshToken()", e);
        }
        ret = new OAuth2HandlerError(OAuth2Error.REFRESH_TOKEN_PROBLEM,
                "error generating refresh body", e);
      }

      if (!isUriAllowed(request.getUri(), accessor.getAllowedDomains())) {
        ret = new OAuth2HandlerError(OAuth2Error.REFRESH_TOKEN_PROBLEM,
                "error fetching refresh token - domain not allowed", null);
      }

      if (ret == null) {
        try {
          response = this.fetcher.fetch(request);
        } catch (final GadgetException e) {
          if (isLogging) {
            BasicOAuth2Request.LOG.log("refreshToken()", e);
          }
          ret = new OAuth2HandlerError(OAuth2Error.REFRESH_TOKEN_PROBLEM,
                  "error fetching refresh token", e);
        }

        if (isLogging) {
          BasicOAuth2Request.LOG.log("response = {0}", response);
        }

        if (response == null) {
          ret = new OAuth2HandlerError(OAuth2Error.REFRESH_TOKEN_PROBLEM, "response is null", null);
        }

        if (ret == null) {
          // response is not null..
          final int statusCode = response.getHttpStatusCode();
          if ((statusCode == HttpResponse.SC_UNAUTHORIZED) || (statusCode == HttpResponse.SC_BAD_REQUEST)) {
            try {
              this.store.removeToken(accessor.getRefreshToken());
            } catch (final GadgetException e) {
              ret = new OAuth2HandlerError(OAuth2Error.REFRESH_TOKEN_PROBLEM,
                      "failed to remove refresh token", e);
            }
            accessor.setRefreshToken(null);
          } else if (statusCode != HttpResponse.SC_OK) {
            ret = new OAuth2HandlerError(OAuth2Error.REFRESH_TOKEN_PROBLEM,
                    "bad response from server : " + statusCode, null);
          }

          if (ret == null) {
            for (final TokenEndpointResponseHandler tokenEndpointResponseHandler : this.tokenEndpointResponseHandlers) {
              if (tokenEndpointResponseHandler.handlesResponse(accessor, response)) {
                final OAuth2HandlerError error = tokenEndpointResponseHandler.handleResponse(
                        accessor, response);
                if (error != null) {
                  return error;
                }
              }
View Full Code Here

                  BasicOAuth2Request.LOG.log("found an access token from another refresh",
                          new Object[] {});
                }
                attempt = true;
              } else {
                final OAuth2HandlerError handlerError = this.refreshToken(accessor);
                if (handlerError == null) {
                  // No errors refreshing, attempt the fetch again.
                  attempt = true;
                  if (isLogging) {
                    BasicOAuth2Request.LOG.log("no refresh errors reported", new Object[] {});
                  }
                } else {
                  if (isLogging) {
                    BasicOAuth2Request.LOG.log("refresh errors reported", new Object[] {});
                  }
                  // There was an error refreshing, stop.
                  final OAuth2Error error = handlerError.getError();
                  ret = this.getErrorResponseBuilder(handlerError.getCause(), error,
                          handlerError.getContextMessage(), handlerError.getUri(),
                          handlerError.getDescription());
                }
              }
            }
            if (attempt) {
              if (isLogging) {
View Full Code Here

TOP

Related Classes of org.apache.shindig.gadgets.oauth2.handler.OAuth2HandlerError

Copyright © 2018 www.massapicom. 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.