View Javadoc

1   /*
2    * Copyright 2006 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.security.xwss.callback.acegi;
18  
19  import java.io.IOException;
20  import javax.security.auth.callback.Callback;
21  import javax.security.auth.callback.UnsupportedCallbackException;
22  
23  import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
24  import org.acegisecurity.Authentication;
25  import org.acegisecurity.AuthenticationException;
26  import org.acegisecurity.AuthenticationManager;
27  import org.acegisecurity.context.SecurityContextHolder;
28  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
29  
30  import org.springframework.beans.factory.InitializingBean;
31  import org.springframework.util.Assert;
32  import org.springframework.ws.soap.security.callback.AbstractCallbackHandler;
33  import org.springframework.ws.soap.security.callback.CleanupCallback;
34  
35  /**
36   * Callback handler that validates a certificate uses an Acegi <code>AuthenticationManager</code>. Logic based on
37   * Acegi's <code>BasicProcessingFilter</code>.
38   * <p/>
39   * This handler requires an Acegi <code>AuthenticationManager</code> to operate. It can be set using the
40   * <code>authenticationManager</code> property. An Acegi <code>UsernamePasswordAuthenticationToken</code> is created
41   * with the username as principal and password as credentials.
42   * <p/>
43   * This class only handles <code>PasswordValidationCallback</code>s that contain a
44   * <code>PlainTextPasswordRequest</code>, and throws an <code>UnsupportedCallbackException</code> for others.
45   *
46   * @author Arjen Poutsma
47   * @see UsernamePasswordAuthenticationToken
48   * @see PasswordValidationCallback
49   * @see com.sun.xml.wss.impl.callback.PasswordValidationCallback.PlainTextPasswordRequest
50   * @see org.acegisecurity.ui.basicauth.BasicProcessingFilter
51   * @since 1.0.0
52   */
53  public class AcegiPlainTextPasswordValidationCallbackHandler extends AbstractCallbackHandler
54          implements InitializingBean {
55  
56      private AuthenticationManager authenticationManager;
57  
58      private boolean ignoreFailure = false;
59  
60      /** Sets the Acegi authentication manager. Required. */
61      public void setAuthenticationManager(AuthenticationManager authenticationManager) {
62          this.authenticationManager = authenticationManager;
63      }
64  
65      public void setIgnoreFailure(boolean ignoreFailure) {
66          this.ignoreFailure = ignoreFailure;
67      }
68  
69      public void afterPropertiesSet() throws Exception {
70          Assert.notNull(authenticationManager, "authenticationManager is required");
71      }
72  
73      /**
74       * Handles <code>PasswordValidationCallback</code>s that contain a <code>PlainTextPasswordRequest</code>, and throws
75       * an <code>UnsupportedCallbackException</code> for others.
76       *
77       * @throws UnsupportedCallbackException when the callback is not supported
78       */
79      protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
80          if (callback instanceof PasswordValidationCallback) {
81              PasswordValidationCallback validationCallback = (PasswordValidationCallback) callback;
82              if (validationCallback.getRequest() instanceof PasswordValidationCallback.PlainTextPasswordRequest) {
83                  validationCallback.setValidator(new AcegiPlainTextPasswordValidator());
84                  return;
85              }
86          }
87          else if (callback instanceof CleanupCallback) {
88              SecurityContextHolder.clearContext();
89              return;
90          }
91          throw new UnsupportedCallbackException(callback);
92      }
93  
94      private class AcegiPlainTextPasswordValidator implements PasswordValidationCallback.PasswordValidator {
95  
96          public boolean validate(PasswordValidationCallback.Request request)
97                  throws PasswordValidationCallback.PasswordValidationException {
98              PasswordValidationCallback.PlainTextPasswordRequest plainTextRequest =
99                      (PasswordValidationCallback.PlainTextPasswordRequest) request;
100             try {
101                 Authentication authResult = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
102                         plainTextRequest.getUsername(), plainTextRequest.getPassword()));
103                 if (logger.isDebugEnabled()) {
104                     logger.debug("Authentication success: " + authResult.toString());
105                 }
106                 SecurityContextHolder.getContext().setAuthentication(authResult);
107                 return true;
108             }
109             catch (AuthenticationException failed) {
110                 if (logger.isDebugEnabled()) {
111                     logger.debug("Authentication request for user '" + plainTextRequest.getUsername() + "' failed: " +
112                             failed.toString());
113                 }
114                 SecurityContextHolder.clearContext();
115                 return ignoreFailure;
116             }
117         }
118     }
119 
120 }