use of javax.servlet.FilterChain in project spring-security-oauth by spring-projects.
the class OAuthProcessingFilterTests method testValidateParams.
/**
* tests validation of the params.
*/
@Test
public void testValidateParams() throws Exception {
OAuthProviderProcessingFilter filter = new OAuthProviderProcessingFilter() {
protected void onValidSignature(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
}
};
ConsumerDetails consumerDetails = mock(ConsumerDetails.class);
HashMap<String, String> params = new HashMap<String, String>();
params.put(OAuthConsumerParameter.oauth_version.toString(), "1.1");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials.");
} catch (OAuthVersionUnsupportedException e) {
params.remove(OAuthConsumerParameter.oauth_version.toString());
}
filter.getAuthenticationEntryPoint().setRealmName("anywho");
params.put("realm", "hello");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials.");
} catch (InvalidOAuthParametersException e) {
}
params.put("realm", "anywho");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials for missing signature method.");
} catch (InvalidOAuthParametersException e) {
}
params.remove("realm");
params.put(OAuthConsumerParameter.oauth_signature_method.toString(), "sigmethod");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials for missing signature.");
} catch (InvalidOAuthParametersException e) {
}
params.remove("realm");
params.put(OAuthConsumerParameter.oauth_signature_method.toString(), "sigmethod");
params.put(OAuthConsumerParameter.oauth_signature.toString(), "value");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials for missing timestamp.");
} catch (InvalidOAuthParametersException e) {
}
params.remove("realm");
params.put(OAuthConsumerParameter.oauth_signature_method.toString(), "sigmethod");
params.put(OAuthConsumerParameter.oauth_signature.toString(), "value");
params.put(OAuthConsumerParameter.oauth_timestamp.toString(), "value");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials for missing nonce.");
} catch (InvalidOAuthParametersException e) {
}
params.remove("realm");
params.put(OAuthConsumerParameter.oauth_signature_method.toString(), "sigmethod");
params.put(OAuthConsumerParameter.oauth_signature.toString(), "value");
params.put(OAuthConsumerParameter.oauth_timestamp.toString(), "value");
params.put(OAuthConsumerParameter.oauth_nonce.toString(), "value");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials for bad timestamp.");
} catch (InvalidOAuthParametersException e) {
}
OAuthNonceServices nonceServices = mock(OAuthNonceServices.class);
filter.setNonceServices(nonceServices);
params.remove("realm");
params.put(OAuthConsumerParameter.oauth_signature_method.toString(), "sigmethod");
params.put(OAuthConsumerParameter.oauth_signature.toString(), "value");
params.put(OAuthConsumerParameter.oauth_timestamp.toString(), "1111111");
params.put(OAuthConsumerParameter.oauth_nonce.toString(), "value");
filter.validateOAuthParams(consumerDetails, params);
verify(nonceServices).validateNonce(consumerDetails, 1111111L, "value");
}
use of javax.servlet.FilterChain in project spring-security-oauth by spring-projects.
the class OAuthProcessingFilterTests method testValidateSignature.
/**
* test validating the signature.
*/
@Test
public void testValidateSignature() throws Exception {
OAuthProviderProcessingFilter filter = new OAuthProviderProcessingFilter() {
@Override
protected void onValidSignature(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
}
};
ConsumerDetails details = mock(ConsumerDetails.class);
SignatureSecret secret = mock(SignatureSecret.class);
OAuthProviderToken token = mock(OAuthProviderToken.class);
OAuthSignatureMethod sigMethod = mock(OAuthSignatureMethod.class);
ConsumerCredentials credentials = new ConsumerCredentials("id", "sig", "method", "base", "token");
when(details.getAuthorities()).thenReturn(new ArrayList<GrantedAuthority>());
when(details.getSignatureSecret()).thenReturn(secret);
filter.setTokenServices(tokenServices);
when(tokenServices.getToken("token")).thenReturn(token);
filter.setSignatureMethodFactory(signatureFactory);
when(token.getSecret()).thenReturn("shhh!!!");
when(signatureFactory.getSignatureMethod("method", secret, "shhh!!!")).thenReturn(sigMethod);
ConsumerAuthentication authentication = new ConsumerAuthentication(details, credentials);
filter.validateSignature(authentication);
verify(sigMethod).verify("base", "sig");
}
use of javax.servlet.FilterChain in project spring-security-oauth by spring-projects.
the class ProtectedResourceProcessingFilterTests method testOnValidSignature.
/**
* test onValidSignature
*/
@Test
public void testOnValidSignature() throws Exception {
ProtectedResourceProcessingFilter filter = new ProtectedResourceProcessingFilter();
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain chain = mock(FilterChain.class);
ConsumerCredentials creds = new ConsumerCredentials("key", "sig", "meth", "base", "tok");
ConsumerAuthentication authentication = new ConsumerAuthentication(mock(ConsumerDetails.class), creds);
authentication.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(authentication);
OAuthProviderTokenServices tokenServices = mock(OAuthProviderTokenServices.class);
OAuthAccessProviderToken token = mock(OAuthAccessProviderToken.class);
filter.setTokenServices(tokenServices);
when(tokenServices.getToken("tok")).thenReturn(token);
when(token.isAccessToken()).thenReturn(true);
Authentication userAuthentication = mock(Authentication.class);
when(token.getUserAuthentication()).thenReturn(userAuthentication);
filter.onValidSignature(request, response, chain);
verify(chain).doFilter(request, response);
assertSame(userAuthentication, SecurityContextHolder.getContext().getAuthentication());
SecurityContextHolder.getContext().setAuthentication(null);
}
use of javax.servlet.FilterChain in project microservices by pwillhan.
the class SsoUiApplication method csrfHeaderFilter.
private Filter csrfHeaderFilter() {
return new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
if (csrf != null) {
Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
String token = csrf.getToken();
if (cookie == null || token != null && !token.equals(cookie.getValue())) {
cookie = new Cookie("XSRF-TOKEN", token);
cookie.setPath("/");
response.addCookie(cookie);
}
}
filterChain.doFilter(request, response);
}
};
}
use of javax.servlet.FilterChain in project knox by apache.
the class CommonIdentityAssertionFilterTest method testSimpleFilter.
@Test
public void testSimpleFilter() throws ServletException, IOException, URISyntaxException {
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.replay(config);
final HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.replay(request);
final HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(response);
final FilterChain chain = new FilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
}
};
Subject subject = new Subject();
subject.getPrincipals().add(new PrimaryPrincipal("larry"));
subject.getPrincipals().add(new GroupPrincipal("users"));
subject.getPrincipals().add(new GroupPrincipal("admin"));
try {
Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
filter.doFilter(request, response, chain);
return null;
}
});
} catch (PrivilegedActionException e) {
Throwable t = e.getCause();
if (t instanceof IOException) {
throw (IOException) t;
} else if (t instanceof ServletException) {
throw (ServletException) t;
} else {
throw new ServletException(t);
}
}
assertEquals("LARRY", username);
assertEquals(mappedGroups.length, 2);
assertTrue(mappedGroups[0].equals("USERS") || mappedGroups[0].equals("ADMIN"));
assertTrue(mappedGroups[1], mappedGroups[1].equals("USERS") || mappedGroups[1].equals("ADMIN"));
}
Aggregations