|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2002-2018 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2002-2019 the original author or authors.
|
|
*
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -17,12 +17,12 @@ package org.springframework.security.oauth2.server.resource.web;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.ServletException;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
import org.junit.Before;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.junit.runner.RunWith;
|
|
import org.mockito.ArgumentCaptor;
|
|
import org.mockito.ArgumentCaptor;
|
|
-import org.mockito.InjectMocks;
|
|
|
|
import org.mockito.Mock;
|
|
import org.mockito.Mock;
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
|
|
|
|
@@ -31,6 +31,7 @@ import org.springframework.mock.web.MockFilterChain;
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
+import org.springframework.security.authentication.AuthenticationManagerResolver;
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
|
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
|
import org.springframework.security.oauth2.server.resource.BearerTokenError;
|
|
import org.springframework.security.oauth2.server.resource.BearerTokenError;
|
|
@@ -57,6 +58,9 @@ public class BearerTokenAuthenticationFilterTests {
|
|
@Mock
|
|
@Mock
|
|
AuthenticationManager authenticationManager;
|
|
AuthenticationManager authenticationManager;
|
|
|
|
|
|
|
|
+ @Mock
|
|
|
|
+ AuthenticationManagerResolver<HttpServletRequest> authenticationManagerResolver;
|
|
|
|
+
|
|
@Mock
|
|
@Mock
|
|
BearerTokenResolver bearerTokenResolver;
|
|
BearerTokenResolver bearerTokenResolver;
|
|
|
|
|
|
@@ -66,9 +70,6 @@ public class BearerTokenAuthenticationFilterTests {
|
|
|
|
|
|
MockFilterChain filterChain;
|
|
MockFilterChain filterChain;
|
|
|
|
|
|
- @InjectMocks
|
|
|
|
- BearerTokenAuthenticationFilter filter;
|
|
|
|
-
|
|
|
|
@Before
|
|
@Before
|
|
public void httpMocks() {
|
|
public void httpMocks() {
|
|
this.request = new MockHttpServletRequest();
|
|
this.request = new MockHttpServletRequest();
|
|
@@ -76,17 +77,31 @@ public class BearerTokenAuthenticationFilterTests {
|
|
this.filterChain = new MockFilterChain();
|
|
this.filterChain = new MockFilterChain();
|
|
}
|
|
}
|
|
|
|
|
|
- @Before
|
|
|
|
- public void setterMocks() {
|
|
|
|
- this.filter.setAuthenticationEntryPoint(this.authenticationEntryPoint);
|
|
|
|
- this.filter.setBearerTokenResolver(this.bearerTokenResolver);
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void doFilterWhenBearerTokenPresentThenAuthenticates() throws ServletException, IOException {
|
|
|
|
+ when(this.bearerTokenResolver.resolve(this.request)).thenReturn("token");
|
|
|
|
+
|
|
|
|
+ BearerTokenAuthenticationFilter filter =
|
|
|
|
+ addMocks(new BearerTokenAuthenticationFilter(this.authenticationManager));
|
|
|
|
+ filter.doFilter(this.request, this.response, this.filterChain);
|
|
|
|
+
|
|
|
|
+ ArgumentCaptor<BearerTokenAuthenticationToken> captor =
|
|
|
|
+ ArgumentCaptor.forClass(BearerTokenAuthenticationToken.class);
|
|
|
|
+
|
|
|
|
+ verify(this.authenticationManager).authenticate(captor.capture());
|
|
|
|
+
|
|
|
|
+ assertThat(captor.getValue().getPrincipal()).isEqualTo("token");
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
- public void doFilterWhenBearerTokenPresentThenAuthenticates() throws ServletException, IOException {
|
|
|
|
|
|
+ public void doFilterWhenUsingAuthenticationManagerResolverThenAuthenticates() throws Exception {
|
|
|
|
+ BearerTokenAuthenticationFilter filter =
|
|
|
|
+ addMocks(new BearerTokenAuthenticationFilter(this.authenticationManagerResolver));
|
|
|
|
+
|
|
when(this.bearerTokenResolver.resolve(this.request)).thenReturn("token");
|
|
when(this.bearerTokenResolver.resolve(this.request)).thenReturn("token");
|
|
|
|
+ when(this.authenticationManagerResolver.resolve(any())).thenReturn(this.authenticationManager);
|
|
|
|
|
|
- this.filter.doFilter(this.request, this.response, this.filterChain);
|
|
|
|
|
|
+ filter.doFilter(this.request, this.response, this.filterChain);
|
|
|
|
|
|
ArgumentCaptor<BearerTokenAuthenticationToken> captor =
|
|
ArgumentCaptor<BearerTokenAuthenticationToken> captor =
|
|
ArgumentCaptor.forClass(BearerTokenAuthenticationToken.class);
|
|
ArgumentCaptor.forClass(BearerTokenAuthenticationToken.class);
|
|
@@ -137,36 +152,56 @@ public class BearerTokenAuthenticationFilterTests {
|
|
when(this.authenticationManager.authenticate(any(BearerTokenAuthenticationToken.class)))
|
|
when(this.authenticationManager.authenticate(any(BearerTokenAuthenticationToken.class)))
|
|
.thenThrow(exception);
|
|
.thenThrow(exception);
|
|
|
|
|
|
- this.filter.doFilter(this.request, this.response, this.filterChain);
|
|
|
|
|
|
+ BearerTokenAuthenticationFilter filter =
|
|
|
|
+ addMocks(new BearerTokenAuthenticationFilter(this.authenticationManager));
|
|
|
|
+ filter.doFilter(this.request, this.response, this.filterChain);
|
|
|
|
|
|
verify(this.authenticationEntryPoint).commence(this.request, this.response, exception);
|
|
verify(this.authenticationEntryPoint).commence(this.request, this.response, exception);
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void setAuthenticationEntryPointWhenNullThenThrowsException() {
|
|
public void setAuthenticationEntryPointWhenNullThenThrowsException() {
|
|
- assertThatCode(() -> this.filter.setAuthenticationEntryPoint(null))
|
|
|
|
|
|
+ BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(this.authenticationManager);
|
|
|
|
+ assertThatCode(() -> filter.setAuthenticationEntryPoint(null))
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
.hasMessageContaining("authenticationEntryPoint cannot be null");
|
|
.hasMessageContaining("authenticationEntryPoint cannot be null");
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void setBearerTokenResolverWhenNullThenThrowsException() {
|
|
public void setBearerTokenResolverWhenNullThenThrowsException() {
|
|
- assertThatCode(() -> this.filter.setBearerTokenResolver(null))
|
|
|
|
|
|
+ BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(this.authenticationManager);
|
|
|
|
+ assertThatCode(() -> filter.setBearerTokenResolver(null))
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
.hasMessageContaining("bearerTokenResolver cannot be null");
|
|
.hasMessageContaining("bearerTokenResolver cannot be null");
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void constructorWhenNullAuthenticationManagerThenThrowsException() {
|
|
public void constructorWhenNullAuthenticationManagerThenThrowsException() {
|
|
- assertThatCode(() -> new BearerTokenAuthenticationFilter(null))
|
|
|
|
|
|
+ assertThatCode(() -> new BearerTokenAuthenticationFilter((AuthenticationManager) null))
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
.hasMessageContaining("authenticationManager cannot be null");
|
|
.hasMessageContaining("authenticationManager cannot be null");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void constructorWhenNullAuthenticationManagerResolverThenThrowsException() {
|
|
|
|
+ assertThatCode(() ->
|
|
|
|
+ new BearerTokenAuthenticationFilter((AuthenticationManagerResolver<HttpServletRequest>) null))
|
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
|
+ .hasMessageContaining("authenticationManagerResolver cannot be null");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private BearerTokenAuthenticationFilter addMocks(BearerTokenAuthenticationFilter filter) {
|
|
|
|
+ filter.setAuthenticationEntryPoint(this.authenticationEntryPoint);
|
|
|
|
+ filter.setBearerTokenResolver(this.bearerTokenResolver);
|
|
|
|
+ return filter;
|
|
|
|
+ }
|
|
|
|
+
|
|
private void dontAuthenticate()
|
|
private void dontAuthenticate()
|
|
throws ServletException, IOException {
|
|
throws ServletException, IOException {
|
|
|
|
|
|
- this.filter.doFilter(this.request, this.response, this.filterChain);
|
|
|
|
|
|
+ BearerTokenAuthenticationFilter filter =
|
|
|
|
+ addMocks(new BearerTokenAuthenticationFilter(this.authenticationManager));
|
|
|
|
+ filter.doFilter(this.request, this.response, this.filterChain);
|
|
|
|
|
|
verifyNoMoreInteractions(this.authenticationManager);
|
|
verifyNoMoreInteractions(this.authenticationManager);
|
|
}
|
|
}
|