Skip to content

Commit 5c7e080

Browse files
author
Tareq Abedrabbo
committed
SWS-555 - Check User's status in SpringDigestPasswordValidationCallbackHandler
1 parent 2c650a5 commit 5c7e080

File tree

9 files changed

+172
-10
lines changed

9 files changed

+172
-10
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package org.springframework.ws.soap.security.support;
17+
18+
import org.acegisecurity.userdetails.UserDetails;
19+
import org.acegisecurity.LockedException;
20+
import org.acegisecurity.DisabledException;
21+
import org.acegisecurity.AccountExpiredException;
22+
import org.acegisecurity.CredentialsExpiredException;
23+
24+
/**
25+
* Generic utility methods for Spring Security
26+
*
27+
* @author Tareq Abedrabbo
28+
* @since 1.5.8
29+
*/
30+
public abstract class AcegiUtils {
31+
32+
/**
33+
* Checks the validity of a user's account and credentials.
34+
* @param user the user to check
35+
* @throws org.springframework.security.AccountExpiredException if the account has expired
36+
* @throws org.springframework.security.CredentialsExpiredException if the credentials have expired
37+
* @throws org.springframework.security.DisabledException if the account is disabled
38+
* @throws org.springframework.security.LockedException if the account is locked
39+
*/
40+
public static void checkUserValidity(UserDetails user)
41+
throws AccountExpiredException, CredentialsExpiredException, DisabledException, LockedException {
42+
if (!user.isAccountNonLocked()) {
43+
throw new LockedException("User account is locked");
44+
}
45+
46+
if (!user.isEnabled()) {
47+
throw new DisabledException("User is disabled");
48+
}
49+
50+
if (!user.isAccountNonExpired()) {
51+
throw new AccountExpiredException("User account has expired");
52+
}
53+
54+
if (!user.isCredentialsNonExpired()) {
55+
throw new CredentialsExpiredException("User credentials have expired");
56+
}
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package org.springframework.ws.soap.security.support;
17+
18+
import org.springframework.security.userdetails.UserDetails;
19+
import org.springframework.security.LockedException;
20+
import org.springframework.security.DisabledException;
21+
import org.springframework.security.AccountExpiredException;
22+
import org.springframework.security.CredentialsExpiredException;
23+
24+
/**
25+
* Generic utility methods for Spring Security
26+
*
27+
* @author Tareq Abedrabbo
28+
* @since 1.5.8
29+
*/
30+
public abstract class SpringSecurityUtils {
31+
32+
/**
33+
* Checks the validity of a user's account and credentials.
34+
* @param user the user to check
35+
* @throws AccountExpiredException if the account has expired
36+
* @throws CredentialsExpiredException if the credentials have expired
37+
* @throws DisabledException if the account is disabled
38+
* @throws LockedException if the account is locked
39+
*/
40+
public static void checkUserValidity(UserDetails user)
41+
throws AccountExpiredException, CredentialsExpiredException, DisabledException, LockedException {
42+
if (!user.isAccountNonLocked()) {
43+
throw new LockedException("User account is locked", user);
44+
}
45+
46+
if (!user.isEnabled()) {
47+
throw new DisabledException("User is disabled", user);
48+
}
49+
50+
if (!user.isAccountNonExpired()) {
51+
throw new AccountExpiredException("User account has expired", user);
52+
}
53+
54+
if (!user.isCredentialsNonExpired()) {
55+
throw new CredentialsExpiredException("User credentials have expired", user);
56+
}
57+
}
58+
}

security/src/main/java/org/springframework/ws/soap/security/wss4j/callback/SpringDigestPasswordValidationCallbackHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.security.userdetails.UsernameNotFoundException;
3434
import org.springframework.util.Assert;
3535
import org.springframework.ws.soap.security.callback.CleanupCallback;
36+
import org.springframework.ws.soap.security.support.SpringSecurityUtils;
3637

3738
/**
3839
* Callback handler that validates a password digest using an Spring Security <code>UserDetailsService</code>. Logic
@@ -71,6 +72,7 @@ protected void handleUsernameToken(WSPasswordCallback callback) throws IOExcepti
7172
String identifier = callback.getIdentifier();
7273
UserDetails user = loadUserDetails(identifier);
7374
if (user != null) {
75+
SpringSecurityUtils.checkUserValidity(user);
7476
callback.setPassword(user.getPassword());
7577
}
7678
}

security/src/main/java/org/springframework/ws/soap/security/wss4j/callback/acegi/AcegiDigestPasswordValidationCallbackHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.ws.soap.security.callback.CleanupCallback;
3636
import org.springframework.ws.soap.security.wss4j.callback.AbstractWsPasswordCallbackHandler;
3737
import org.springframework.ws.soap.security.wss4j.callback.UsernameTokenPrincipalCallback;
38+
import org.springframework.ws.soap.security.support.AcegiUtils;
3839

3940
/**
4041
* Callback handler that validates a password digest using an Acegi <code>UserDetailsService</code>. Logic based on
@@ -73,6 +74,7 @@ protected void handleUsernameToken(WSPasswordCallback callback) throws IOExcepti
7374
String identifier = callback.getIdentifier();
7475
UserDetails user = loadUserDetails(identifier);
7576
if (user != null) {
77+
AcegiUtils.checkUserValidity(user);
7678
callback.setPassword(user.getPassword());
7779
}
7880
}

security/src/main/java/org/springframework/ws/soap/security/xwss/callback/SpringDigestPasswordValidationCallbackHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.util.Assert;
3636
import org.springframework.ws.soap.security.callback.AbstractCallbackHandler;
3737
import org.springframework.ws.soap.security.callback.CleanupCallback;
38+
import org.springframework.ws.soap.security.support.SpringSecurityUtils;
3839

3940
/**
4041
* Callback handler that validates a password digest using an Spring Security <code>UserDetailsService</code>. Logic
@@ -89,6 +90,7 @@ protected void handleInternal(Callback callback) throws IOException, Unsupported
8990
String username = request.getUsername();
9091
UserDetails user = loadUserDetails(username);
9192
if (user != null) {
93+
SpringSecurityUtils.checkUserValidity(user);
9294
request.setPassword(user.getPassword());
9395
}
9496
SpringSecurityDigestPasswordValidator validator = new SpringSecurityDigestPasswordValidator(user);

security/src/main/java/org/springframework/ws/soap/security/xwss/callback/acegi/AcegiDigestPasswordValidationCallbackHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.ws.soap.security.callback.AbstractCallbackHandler;
3737
import org.springframework.ws.soap.security.callback.CleanupCallback;
3838
import org.springframework.ws.soap.security.xwss.callback.DefaultTimestampValidator;
39+
import org.springframework.ws.soap.security.support.AcegiUtils;
3940

4041
/**
4142
* Callback handler that validates a password digest using an Acegi <code>UserDetailsService</code>. Logic based on
@@ -89,6 +90,7 @@ protected void handleInternal(Callback callback) throws IOException, Unsupported
8990
String username = request.getUsername();
9091
UserDetails user = loadUserDetails(username);
9192
if (user != null) {
93+
AcegiUtils.checkUserValidity(user);
9294
request.setPassword(user.getPassword());
9395
}
9496
AcegiDigestPasswordValidator validator = new AcegiDigestPasswordValidator(user);

security/src/test/java/org/springframework/ws/soap/security/wss4j/callback/acegi/AcegiDigestPasswordValidationCallbackHandlerTest.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import org.acegisecurity.Authentication;
2121
import org.acegisecurity.GrantedAuthority;
2222
import org.acegisecurity.GrantedAuthorityImpl;
23+
import org.acegisecurity.DisabledException;
2324
import org.acegisecurity.context.SecurityContext;
2425
import org.acegisecurity.context.SecurityContextHolder;
2526
import org.acegisecurity.userdetails.User;
2627
import org.acegisecurity.userdetails.UserDetails;
2728
import org.acegisecurity.userdetails.UserDetailsService;
2829
import org.apache.ws.security.WSUsernameTokenPrincipal;
30+
import org.apache.ws.security.WSPasswordCallback;
2931
import org.easymock.MockControl;
3032

3133
import org.springframework.ws.soap.security.wss4j.callback.UsernameTokenPrincipalCallback;
@@ -41,31 +43,29 @@ public class AcegiDigestPasswordValidationCallbackHandlerTest extends TestCase {
4143

4244
private MockControl control;
4345

44-
private WSUsernameTokenPrincipal principal;
45-
46-
private UsernameTokenPrincipalCallback callback;
47-
4846
private UserDetails user;
4947

5048
protected void setUp() throws Exception {
5149
callbackHandler = new AcegiDigestPasswordValidationCallbackHandler();
5250

5351
grantedAuthority = new GrantedAuthorityImpl("ROLE_1");
54-
user = new User("Ernie", "Bert", true, true, true, true, new GrantedAuthority[]{grantedAuthority});
5552

5653
control = MockControl.createControl(UserDetailsService.class);
5754
userDetailsService = (UserDetailsService) control.getMock();
5855
userDetailsService.loadUserByUsername("Ernie");
59-
control.setDefaultReturnValue(user);
60-
control.replay();
6156
callbackHandler.setUserDetailsService(userDetailsService);
57+
}
6258

63-
principal = new WSUsernameTokenPrincipal("Ernie", true);
64-
callback = new UsernameTokenPrincipalCallback(principal);
65-
59+
protected void tearDown() throws Exception {
60+
control.reset();
6661
}
6762

6863
public void testHandleUsernameTokenPrincipal() throws Exception {
64+
user = new User("Ernie", "Bert", true, true, true, true, new GrantedAuthority[]{grantedAuthority});
65+
WSUsernameTokenPrincipal principal = new WSUsernameTokenPrincipal("Ernie", true);
66+
UsernameTokenPrincipalCallback callback = new UsernameTokenPrincipalCallback(principal);
67+
control.setDefaultReturnValue(user);
68+
control.replay();
6969
callbackHandler.handleUsernameTokenPrincipal(callback);
7070
SecurityContext context = SecurityContextHolder.getContext();
7171
assertNotNull("SecurityContext must not be null", context);
@@ -75,4 +75,16 @@ public void testHandleUsernameTokenPrincipal() throws Exception {
7575
assertTrue("GrantedAuthority[] must not be null or empty", (authorities != null && authorities.length > 0));
7676
assertEquals("Unexpected authority", grantedAuthority, authorities[0]);
7777
}
78+
79+
public void testHandleUsernameTokenWithDisabledUser() throws Exception {
80+
user = new User("Ernie", "Bert", false, true, true, true, new GrantedAuthority[]{grantedAuthority});
81+
WSPasswordCallback callback = new WSPasswordCallback("ID", WSPasswordCallback.USERNAME_TOKEN);
82+
control.setDefaultReturnValue(user);
83+
control.replay();
84+
try {
85+
callbackHandler.handleUsernameToken(callback);
86+
fail("disabled user authenticated");
87+
} catch (DisabledException expected) {
88+
}
89+
}
7890
}

security/src/test/java/org/springframework/ws/soap/security/xwss/callback/SpringDigestPasswordValidationCallbackHandlerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.easymock.MockControl;
2222

2323
import org.springframework.security.GrantedAuthority;
24+
import org.springframework.security.DisabledException;
2425
import org.springframework.security.context.SecurityContextHolder;
2526
import org.springframework.security.providers.TestingAuthenticationToken;
2627
import org.springframework.security.userdetails.User;
@@ -93,6 +94,18 @@ public void testAuthenticateUserDigestValidInvalid() throws Exception {
9394
control.verify();
9495
}
9596

97+
public void testAuthenticateUserDigestDisbaled() throws Exception {
98+
User user = new User(username, "Ernie", false, true, true, true, new GrantedAuthority[0]);
99+
control.expectAndReturn(mock.loadUserByUsername(username), user);
100+
control.replay();
101+
try {
102+
callbackHandler.handleInternal(callback);
103+
fail("disabled user authenticated");
104+
} catch (
105+
DisabledException expected) {
106+
}
107+
}
108+
96109
public void testCleanUp() throws Exception {
97110
TestingAuthenticationToken authentication =
98111
new TestingAuthenticationToken(new Object(), new Object(), new GrantedAuthority[0]);

security/src/test/java/org/springframework/ws/soap/security/xwss/callback/acegi/AcegiDigestPasswordValidationCallbackHandlerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
2020
import junit.framework.TestCase;
2121
import org.acegisecurity.GrantedAuthority;
22+
import org.acegisecurity.DisabledException;
2223
import org.acegisecurity.context.SecurityContextHolder;
2324
import org.acegisecurity.providers.TestingAuthenticationToken;
2425
import org.acegisecurity.userdetails.User;
@@ -93,6 +94,18 @@ public void testAuthenticateUserDigestValidInvalid() throws Exception {
9394
control.verify();
9495
}
9596

97+
public void testAuthenticateUserDigestDisbaled() throws Exception {
98+
User user = new User(username, "Ernie", false, true, true, true, new GrantedAuthority[0]);
99+
control.expectAndReturn(mock.loadUserByUsername(username), user);
100+
control.replay();
101+
try {
102+
callbackHandler.handleInternal(callback);
103+
fail("disabled user authenticated");
104+
} catch (
105+
DisabledException expected) {
106+
}
107+
}
108+
96109
public void testCleanUp() throws Exception {
97110
TestingAuthenticationToken authentication =
98111
new TestingAuthenticationToken(new Object(), new Object(), new GrantedAuthority[0]);

0 commit comments

Comments
 (0)