Example usage for org.springframework.security.oauth.consumer OAuthSecurityContext getAccessTokens

List of usage examples for org.springframework.security.oauth.consumer OAuthSecurityContext getAccessTokens

Introduction

In this page you can find the example usage for org.springframework.security.oauth.consumer OAuthSecurityContext getAccessTokens.

Prototype

Map<String, OAuthConsumerToken> getAccessTokens();

Source Link

Document

Get the access tokens for the current context.

Usage

From source file:info.raack.appliancelabeler.service.DefaultDataService.java

public List<Ted5000> getTEDIdsForUserId(String userId, boolean needContext) {
    String requestURI = String.format(tedListURLPattern, stepgreenBasehost,
            userId == null ? "current_user" : userId);

    OAuthSecurityContext context = null;
    if (userId != null) {
        context = getOAuthDataForUserId(userId).getSecurityContext();
        if (context.getAccessTokens().isEmpty()) {
            if (needContext) {
                throw new OAuthUnauthorizedException();
            } else {
                // this context may have just been saved
                context = null;/* w  w w .  j av  a2 s  .  c om*/
            }
        }
    }

    try {
        if (context == null) {
            return oAuthRequestProcessor.processRequest(requestURI,
                    new ResponseHandler<AggregatedTeds, List<Ted5000>>() {
                        public List<Ted5000> extractValue(AggregatedTeds aggteds) {
                            return removeUnfetchableTeds(aggteds);
                        }
                    });
        } else {
            return oAuthRequestProcessor.processRequest(requestURI, context,
                    new ResponseHandler<AggregatedTeds, List<Ted5000>>() {
                        public List<Ted5000> extractValue(AggregatedTeds ted) {
                            return removeUnfetchableTeds(ted);
                        }
                    });
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not get energy monitor information for user id " + userId, e);
    }
}

From source file:org.springframework.security.oauth.consumer.filter.OAuthConsumerProcessingFilter.java

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;

    Set<String> accessTokenDeps = getAccessTokenDependencies(request, response, chain);
    if (!accessTokenDeps.isEmpty()) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (isRequireAuthenticated() && !authentication.isAuthenticated()) {
            throw new InsufficientAuthenticationException("An authenticated principal must be present.");
        }// ww w  .j a va2s. co m

        OAuthSecurityContext context = OAuthSecurityContextHolder.getContext();
        if (context == null) {
            throw new IllegalStateException(
                    "No OAuth security context has been established. Unable to access resources.");
        }

        Map<String, OAuthConsumerToken> accessTokens = context.getAccessTokens();

        for (String dependency : accessTokenDeps) {
            if (!accessTokens.containsKey(dependency)) {
                throw new AccessTokenRequiredException(
                        getProtectedResourceDetailsService().loadProtectedResourceDetailsById(dependency));
            }
        }

        chain.doFilter(request, response);
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No access token dependencies for request.");
        }
        chain.doFilter(servletRequest, servletResponse);
    }
}