use of org.springframework.security.oauth.consumer.OAuthSecurityContext in project spring-security-oauth by spring-projects.
the class HttpSessionBasedTokenServices method getSession.
protected HttpSession getSession() {
OAuthSecurityContext context = OAuthSecurityContextHolder.getContext();
if (context == null) {
throw new IllegalStateException("A security context must be established.");
}
HttpServletRequest request;
try {
request = (HttpServletRequest) context.getDetails();
} catch (ClassCastException e) {
throw new IllegalStateException("The security context must have the HTTP servlet request as its details.");
}
if (request == null) {
throw new IllegalStateException("The security context must have the HTTP servlet request as its details.");
}
HttpSession session = request.getSession(true);
if (session == null) {
throw new IllegalStateException("Unable to create a session in which to store the tokens.");
}
return session;
}
use of org.springframework.security.oauth.consumer.OAuthSecurityContext in project spring-security-oauth by spring-projects.
the class OAuthClientHttpRequestFactory method createRequest.
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
OAuthSecurityContext context = OAuthSecurityContextHolder.getContext();
if (context == null) {
context = new OAuthSecurityContextImpl();
}
Map<String, OAuthConsumerToken> accessTokens = context.getAccessTokens();
OAuthConsumerToken accessToken = accessTokens == null ? null : accessTokens.get(this.resource.getId());
boolean useAuthHeader = this.resource.isAcceptsAuthorizationHeader();
if (!useAuthHeader) {
String queryString = this.support.getOAuthQueryString(this.resource, accessToken, uri.toURL(), httpMethod.name(), this.additionalOAuthParameters);
String uriValue = String.valueOf(uri);
uri = URI.create((uriValue.contains("?") ? uriValue.substring(0, uriValue.indexOf('?')) : uriValue) + "?" + queryString);
}
ClientHttpRequest req = delegate.createRequest(uri, httpMethod);
if (useAuthHeader) {
String authHeader = this.support.getAuthorizationHeader(this.resource, accessToken, uri.toURL(), httpMethod.name(), this.additionalOAuthParameters);
req.getHeaders().add("Authorization", authHeader);
}
Map<String, String> additionalHeaders = this.resource.getAdditionalRequestHeaders();
if (additionalHeaders != null) {
for (Map.Entry<String, String> header : additionalHeaders.entrySet()) {
req.getHeaders().add(header.getKey(), header.getValue());
}
}
return req;
}
use of org.springframework.security.oauth.consumer.OAuthSecurityContext in project spring-security-oauth by spring-projects.
the class OAuthConsumerProcessingFilter method doFilter.
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.");
}
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);
}
}
Aggregations