Examples of ProtectedResourceDetails


Examples of org.springframework.security.oauth.consumer.ProtectedResourceDetails

        request.setAttribute(getAccessTokensRequestAttribute(), new ArrayList<OAuthConsumerToken>(accessTokens.values()));
        chain.doFilter(request, response);
      }
      catch (Exception e) {
        try {
          ProtectedResourceDetails resourceThatNeedsAuthorization = checkForResourceThatNeedsAuthorization(e);
          String neededResourceId = resourceThatNeedsAuthorization.getId();
          while (!accessTokens.containsKey(neededResourceId)) {
            OAuthConsumerToken token = requestTokens.remove(neededResourceId);
            if (token == null) {
              token = getTokenServices().getToken(neededResourceId);
            }

            String verifier = request.getParameter(OAuthProviderParameter.oauth_verifier.toString());
            // if the token is null OR
            // if there is NO access token and (we're not using 1.0a or the verifier is not null)
            if (token == null || (!token.isAccessToken() && (!resourceThatNeedsAuthorization.isUse10a() || verifier == null))) {
              //no token associated with the resource, start the oauth flow.
              //if there's a request token, but no verifier, we'll assume that a previous oauth request failed and we need to get a new request token.
              if (LOG.isDebugEnabled()) {
                LOG.debug("Obtaining request token for resource: " + neededResourceId);
              }

              //obtain authorization.
              String callbackURL = response.encodeRedirectURL(getCallbackURL(request));
              token = getConsumerSupport().getUnauthorizedRequestToken(neededResourceId, callbackURL);
              if (LOG.isDebugEnabled()) {
                LOG.debug("Request token obtained for resource " + neededResourceId + ": " + token);
              }

              //okay, we've got a request token, now we need to authorize it.
              requestTokens.put(neededResourceId, token);
              getTokenServices().storeToken(neededResourceId, token);
              String redirect = getUserAuthorizationRedirectURL(resourceThatNeedsAuthorization, token, callbackURL);

              if (LOG.isDebugEnabled()) {
                LOG.debug("Redirecting request to " + redirect + " for user authorization of the request token for resource " + neededResourceId + ".");
              }

              request.setAttribute("org.springframework.security.oauth.consumer.AccessTokenRequiredException", e);
              this.redirectStrategy.sendRedirect(request, response, redirect);
              return;
            }
            else if (!token.isAccessToken()) {
              //we have a presumably authorized request token, let's try to get an access token with it.
              if (LOG.isDebugEnabled()) {
                LOG.debug("Obtaining access token for resource: " + neededResourceId);
              }

              //authorize the request token and store it.
              try {
                token = getConsumerSupport().getAccessToken(token, verifier);
              }
              finally {
                getTokenServices().removeToken(neededResourceId);
              }

              if (LOG.isDebugEnabled()) {
                LOG.debug("Access token " + token + " obtained for resource " + neededResourceId + ". Now storing and using.");
              }

              getTokenServices().storeToken(neededResourceId, token);
            }

            accessTokens.put(neededResourceId, token);

            try {
              //try again
              if (!response.isCommitted()) {
                request.setAttribute(getAccessTokensRequestAttribute(), new ArrayList<OAuthConsumerToken>(accessTokens.values()));
                chain.doFilter(request, response);
              }
              else {
                //dang. what do we do now?
                throw new IllegalStateException("Unable to reprocess filter chain with needed OAuth2 resources because the response is already committed.");
              }
            }
            catch (Exception e1) {
              resourceThatNeedsAuthorization = checkForResourceThatNeedsAuthorization(e1);
              neededResourceId = resourceThatNeedsAuthorization.getId();
            }
          }
        }
        catch (OAuthRequestFailedException eo) {
          fail(request, response, eo);
View Full Code Here

Examples of org.springframework.security.oauth.consumer.ProtectedResourceDetails

   * @return The resource that needed authorization (never null).
   */
  protected ProtectedResourceDetails checkForResourceThatNeedsAuthorization(Exception ex) throws ServletException, IOException {
    Throwable[] causeChain = getThrowableAnalyzer().determineCauseChain(ex);
    AccessTokenRequiredException ase = (AccessTokenRequiredException) getThrowableAnalyzer().getFirstThrowableOfType(AccessTokenRequiredException.class, causeChain);
    ProtectedResourceDetails resourceThatNeedsAuthorization;
    if (ase != null) {
      resourceThatNeedsAuthorization = ase.getResource();
      if (resourceThatNeedsAuthorization == null) {
        throw new OAuthRequestFailedException(ase.getMessage());
      }
View Full Code Here

Examples of org.springframework.security.oauth.consumer.ProtectedResourceDetails

    Assert.notNull(streamHandlerFactory, "A stream handler factory is required.");
  }

  // Inherited.
  public OAuthConsumerToken getUnauthorizedRequestToken(String resourceId, String callback) throws OAuthRequestFailedException {
    ProtectedResourceDetails details = getProtectedResourceDetailsService().loadProtectedResourceDetailsById(resourceId);
    return getUnauthorizedRequestToken(details, callback);
  }
View Full Code Here

Examples of org.springframework.security.oauth.consumer.ProtectedResourceDetails

    return getTokenFromProvider(details, requestTokenURL, httpMethod, null, additionalParameters);
  }

  // Inherited.
  public OAuthConsumerToken getAccessToken(OAuthConsumerToken requestToken, String verifier) throws OAuthRequestFailedException {
    ProtectedResourceDetails details = getProtectedResourceDetailsService().loadProtectedResourceDetailsById(requestToken.getResourceId());
    return getAccessToken(details, requestToken, verifier);
  }
View Full Code Here

Examples of org.springframework.security.oauth.consumer.ProtectedResourceDetails

  public InputStream readProtectedResource(URL url, OAuthConsumerToken accessToken, String httpMethod) throws OAuthRequestFailedException {
    if (accessToken == null) {
      throw new OAuthRequestFailedException("A valid access token must be supplied.");
    }

    ProtectedResourceDetails resourceDetails = getProtectedResourceDetailsService().loadProtectedResourceDetailsById(accessToken.getResourceId());
    if ((!resourceDetails.isAcceptsAuthorizationHeader()) && !"POST".equalsIgnoreCase(httpMethod) && !"PUT".equalsIgnoreCase(httpMethod)) {
      throw new IllegalArgumentException("Protected resource " + resourceDetails.getId() + " cannot be accessed with HTTP method " +
        httpMethod + " because the OAuth provider doesn't accept the OAuth Authorization header.");
    }

    return readResource(resourceDetails, url, httpMethod, accessToken, resourceDetails.getAdditionalParameters(), null);
  }
View Full Code Here
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.