Skip to content

Commit 9ae163e

Browse files
author
Rob Winch
committed
Rename to RequestAttributeAuthenticationFilter
Rename EnvironmentVariableAuthenticationFilter to RequestAttributeAuthenticationFilterTests Polish gh-3978
1 parent a8120e7 commit 9ae163e

2 files changed

Lines changed: 46 additions & 38 deletions

File tree

web/src/main/java/org/springframework/security/web/authentication/preauth/EnvironmentVariableAuthenticationFilter.java renamed to web/src/main/java/org/springframework/security/web/authentication/preauth/RequestAttributeAuthenticationFilter.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,52 +20,53 @@
2020
import org.springframework.util.Assert;
2121

2222
/**
23-
* A simple pre-authenticated filter which obtains the username from an environment variable, for
24-
* use with SSO systems such as Stanford WebAuth or Shibboleth.
23+
* A simple pre-authenticated filter which obtains the username from request attributes,
24+
* for use with SSO systems such as Stanford WebAuth or Shibboleth.
2525
* <p>
2626
* As with most pre-authenticated scenarios, it is essential that the external
2727
* authentication system is set up correctly as this filter does no authentication
2828
* whatsoever.
2929
* <p>
30-
* The property {@code principalEnvironmentVariable} is the name of the request environment variable
31-
* that contains the username. It defaults to "REMOTE_USER" for compatibility with WebAuth and Shibboleth.
30+
* The property {@code principalEnvironmentVariable} is the name of the request attribute
31+
* that contains the username. It defaults to "REMOTE_USER" for compatibility with WebAuth
32+
* and Shibboleth.
3233
* <p>
33-
* If the environment variable is missing from the request, {@code getPreAuthenticatedPrincipal} will
34-
* throw an exception. You can override this behaviour by setting the
35-
* {@code exceptionIfVariableMissing} property.
34+
* If the environment variable is missing from the request,
35+
* {@code getPreAuthenticatedPrincipal} will throw an exception. You can override this
36+
* behaviour by setting the {@code exceptionIfVariableMissing} property.
3637
*
3738
*
3839
* @author Milan Sevcik
3940
* @since 4.2
4041
*/
41-
public class EnvironmentVariableAuthenticationFilter extends
42-
AbstractPreAuthenticatedProcessingFilter {
42+
public class RequestAttributeAuthenticationFilter
43+
extends AbstractPreAuthenticatedProcessingFilter {
4344
private String principalEnvironmentVariable = "REMOTE_USER";
4445
private String credentialsEnvironmentVariable;
4546
private boolean exceptionIfVariableMissing = true;
4647

4748
/**
48-
* Read and returns the variable named by {@code principalEnvironmentVariable} from the
49-
* request.
49+
* Read and returns the variable named by {@code principalEnvironmentVariable} from
50+
* the request.
5051
*
51-
* @throws PreAuthenticatedCredentialsNotFoundException if the environment variable
52-
* is missing and {@code exceptionIfVariableMissing} is set to {@code true}.
52+
* @throws PreAuthenticatedCredentialsNotFoundException if the environment variable is
53+
* missing and {@code exceptionIfVariableMissing} is set to {@code true}.
5354
*/
5455
protected Object getPreAuthenticatedPrincipal(HttpServletRequest request) {
55-
String principal = (String)request.getAttribute(principalEnvironmentVariable);
56+
String principal = (String) request.getAttribute(principalEnvironmentVariable);
5657

5758
if (principal == null && exceptionIfVariableMissing) {
58-
throw new PreAuthenticatedCredentialsNotFoundException(principalEnvironmentVariable
59-
+ " variable not found in request.");
59+
throw new PreAuthenticatedCredentialsNotFoundException(
60+
principalEnvironmentVariable + " variable not found in request.");
6061
}
6162

6263
return principal;
6364
}
6465

6566
/**
66-
* Credentials aren't usually applicable, but if a {@code credentialsEnvironmentVariable} is
67-
* set, this will be read and used as the credentials value. Otherwise a dummy value
68-
* will be used.
67+
* Credentials aren't usually applicable, but if a
68+
* {@code credentialsEnvironmentVariable} is set, this will be read and used as the
69+
* credentials value. Otherwise a dummy value will be used.
6970
*/
7071
protected Object getPreAuthenticatedCredentials(HttpServletRequest request) {
7172
if (credentialsEnvironmentVariable != null) {
@@ -77,13 +78,13 @@ protected Object getPreAuthenticatedCredentials(HttpServletRequest request) {
7778

7879
public void setPrincipalEnvironmentVariable(String principalEnvironmentVariable) {
7980
Assert.hasText(principalEnvironmentVariable,
80-
"principalEnvironmentVariable must not be empty or null");
81+
"principalEnvironmentVariable must not be empty or null");
8182
this.principalEnvironmentVariable = principalEnvironmentVariable;
8283
}
8384

8485
public void setCredentialsEnvironmentVariable(String credentialsEnvironmentVariable) {
8586
Assert.hasText(credentialsEnvironmentVariable,
86-
"credentialsEnvironmentVariable must not be empty or null");
87+
"credentialsEnvironmentVariable must not be empty or null");
8788
this.credentialsEnvironmentVariable = credentialsEnvironmentVariable;
8889
}
8990

web/src/test/java/org/springframework/security/web/authentication/preauth/envvariable/EnvironmentVariableAuthenticationFilterTests.java renamed to web/src/test/java/org/springframework/security/web/authentication/preauth/RequestAttributeAuthenticationFilterTests.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.security.web.authentication.preauth.envvariable;
16+
package org.springframework.security.web.authentication.preauth;
1717

1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.mockito.Mockito.*;
@@ -30,13 +30,13 @@
3030
import org.springframework.security.core.Authentication;
3131
import org.springframework.security.core.context.SecurityContextHolder;
3232
import org.springframework.security.web.authentication.preauth.PreAuthenticatedCredentialsNotFoundException;
33-
import org.springframework.security.web.authentication.preauth.EnvironmentVariableAuthenticationFilter;
33+
import org.springframework.security.web.authentication.preauth.RequestAttributeAuthenticationFilter;
3434

3535
/**
3636
*
3737
* @author Milan Sevcik
3838
*/
39-
public class EnvironmentVariableAuthenticationFilterTests {
39+
public class RequestAttributeAuthenticationFilterTests {
4040

4141
@After
4242
@Before
@@ -49,7 +49,7 @@ public void rejectsMissingHeader() throws Exception {
4949
MockHttpServletRequest request = new MockHttpServletRequest();
5050
MockHttpServletResponse response = new MockHttpServletResponse();
5151
MockFilterChain chain = new MockFilterChain();
52-
EnvironmentVariableAuthenticationFilter filter = new EnvironmentVariableAuthenticationFilter();
52+
RequestAttributeAuthenticationFilter filter = new RequestAttributeAuthenticationFilter();
5353

5454
filter.doFilter(request, response, chain);
5555
}
@@ -60,13 +60,16 @@ public void defaultsToUsingSiteminderHeader() throws Exception {
6060
request.setAttribute("REMOTE_USER", "cat");
6161
MockHttpServletResponse response = new MockHttpServletResponse();
6262
MockFilterChain chain = new MockFilterChain();
63-
EnvironmentVariableAuthenticationFilter filter = new EnvironmentVariableAuthenticationFilter();
63+
RequestAttributeAuthenticationFilter filter = new RequestAttributeAuthenticationFilter();
6464
filter.setAuthenticationManager(createAuthenticationManager());
6565

6666
filter.doFilter(request, response, chain);
6767
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
68-
assertThat(SecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("cat");
69-
assertThat(SecurityContextHolder.getContext().getAuthentication().getCredentials()).isEqualTo("N/A");
68+
assertThat(SecurityContextHolder.getContext().getAuthentication().getName())
69+
.isEqualTo("cat");
70+
assertThat(
71+
SecurityContextHolder.getContext().getAuthentication().getCredentials())
72+
.isEqualTo("N/A");
7073
}
7174

7275
@Test
@@ -75,37 +78,40 @@ public void alternativeHeaderNameIsSupported() throws Exception {
7578
request.setAttribute("myUsernameVariable", "wolfman");
7679
MockHttpServletResponse response = new MockHttpServletResponse();
7780
MockFilterChain chain = new MockFilterChain();
78-
EnvironmentVariableAuthenticationFilter filter = new EnvironmentVariableAuthenticationFilter();
81+
RequestAttributeAuthenticationFilter filter = new RequestAttributeAuthenticationFilter();
7982
filter.setAuthenticationManager(createAuthenticationManager());
8083
filter.setPrincipalEnvironmentVariable("myUsernameVariable");
8184

8285
filter.doFilter(request, response, chain);
8386
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
84-
assertThat(SecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("wolfman");
87+
assertThat(SecurityContextHolder.getContext().getAuthentication().getName())
88+
.isEqualTo("wolfman");
8589
}
8690

8791
@Test
8892
public void credentialsAreRetrievedIfHeaderNameIsSet() throws Exception {
8993
MockHttpServletRequest request = new MockHttpServletRequest();
9094
MockHttpServletResponse response = new MockHttpServletResponse();
9195
MockFilterChain chain = new MockFilterChain();
92-
EnvironmentVariableAuthenticationFilter filter = new EnvironmentVariableAuthenticationFilter();
96+
RequestAttributeAuthenticationFilter filter = new RequestAttributeAuthenticationFilter();
9397
filter.setAuthenticationManager(createAuthenticationManager());
9498
filter.setCredentialsEnvironmentVariable("myCredentialsVariable");
9599
request.setAttribute("REMOTE_USER", "cat");
96100
request.setAttribute("myCredentialsVariable", "catspassword");
97101

98102
filter.doFilter(request, response, chain);
99103
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
100-
assertThat(SecurityContextHolder.getContext().getAuthentication().getCredentials()).isEqualTo("catspassword");
104+
assertThat(
105+
SecurityContextHolder.getContext().getAuthentication().getCredentials())
106+
.isEqualTo("catspassword");
101107
}
102108

103109
@Test
104110
public void userIsReauthenticatedIfPrincipalChangesAndCheckForPrincipalChangesIsSet()
105111
throws Exception {
106112
MockHttpServletRequest request = new MockHttpServletRequest();
107113
MockHttpServletResponse response = new MockHttpServletResponse();
108-
EnvironmentVariableAuthenticationFilter filter = new EnvironmentVariableAuthenticationFilter();
114+
RequestAttributeAuthenticationFilter filter = new RequestAttributeAuthenticationFilter();
109115
filter.setAuthenticationManager(createAuthenticationManager());
110116
filter.setCheckForPrincipalChanges(true);
111117
request.setAttribute("REMOTE_USER", "cat");
@@ -116,7 +122,8 @@ public void userIsReauthenticatedIfPrincipalChangesAndCheckForPrincipalChangesIs
116122
Authentication dog = SecurityContextHolder.getContext().getAuthentication();
117123
assertThat(dog).isNotNull();
118124
assertThat(dog.getName()).isEqualTo("dog");
119-
// Make sure authentication doesn't occur every time (i.e. if the variable *doesn't*
125+
// Make sure authentication doesn't occur every time (i.e. if the variable
126+
// *doesn't*
120127
// change)
121128
filter.setAuthenticationManager(mock(AuthenticationManager.class));
122129
filter.doFilter(request, response, new MockFilterChain());
@@ -128,7 +135,7 @@ public void missingHeaderCausesException() throws Exception {
128135
MockHttpServletRequest request = new MockHttpServletRequest();
129136
MockHttpServletResponse response = new MockHttpServletResponse();
130137
MockFilterChain chain = new MockFilterChain();
131-
EnvironmentVariableAuthenticationFilter filter = new EnvironmentVariableAuthenticationFilter();
138+
RequestAttributeAuthenticationFilter filter = new RequestAttributeAuthenticationFilter();
132139
filter.setAuthenticationManager(createAuthenticationManager());
133140

134141
filter.doFilter(request, response, chain);
@@ -140,7 +147,7 @@ public void missingHeaderIsIgnoredIfExceptionIfHeaderMissingIsFalse()
140147
MockHttpServletRequest request = new MockHttpServletRequest();
141148
MockHttpServletResponse response = new MockHttpServletResponse();
142149
MockFilterChain chain = new MockFilterChain();
143-
EnvironmentVariableAuthenticationFilter filter = new EnvironmentVariableAuthenticationFilter();
150+
RequestAttributeAuthenticationFilter filter = new RequestAttributeAuthenticationFilter();
144151
filter.setExceptionIfVariableMissing(false);
145152
filter.setAuthenticationManager(createAuthenticationManager());
146153
filter.doFilter(request, response, chain);
@@ -151,8 +158,8 @@ public void missingHeaderIsIgnoredIfExceptionIfHeaderMissingIsFalse()
151158
*/
152159
private AuthenticationManager createAuthenticationManager() {
153160
AuthenticationManager am = mock(AuthenticationManager.class);
154-
when(am.authenticate(any(Authentication.class))).thenAnswer(
155-
new Answer<Authentication>() {
161+
when(am.authenticate(any(Authentication.class)))
162+
.thenAnswer(new Answer<Authentication>() {
156163
public Authentication answer(InvocationOnMock invocation)
157164
throws Throwable {
158165
return (Authentication) invocation.getArguments()[0];

0 commit comments

Comments
 (0)