View Javadoc

1   /*
2    * Copyright 2005-2010 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;
18  
19  import java.io.IOException;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Properties;
23  import javax.security.auth.callback.Callback;
24  import javax.security.auth.callback.UnsupportedCallbackException;
25  
26  import org.springframework.beans.factory.InitializingBean;
27  import org.springframework.util.Assert;
28  import org.springframework.ws.soap.security.callback.AbstractCallbackHandler;
29  
30  import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
31  import com.sun.xml.wss.impl.callback.TimestampValidationCallback;
32  
33  /**
34   * Simple callback handler that validates passwords agains a in-memory <code>Properties</code> object. Password
35   * validation is done on a case-sensitive basis.
36   * <p/>
37   * This class only handles <code>PasswordValidationCallback</code>s, and throws an
38   * <code>UnsupportedCallbackException</code> for others
39   *
40   * @author Arjen Poutsma
41   * @see #setUsers(java.util.Properties)
42   * @since 1.0.0
43   */
44  public class SimplePasswordValidationCallbackHandler extends AbstractCallbackHandler implements InitializingBean {
45  
46      private Map<String, String> users = new HashMap<String, String>();
47  
48      /** Sets the users to validate against. Property names are usernames, property values are passwords. */
49      public void setUsers(Properties users) {
50          for (Map.Entry<Object, Object> entry : users.entrySet()) {
51              if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
52                  this.users.put((String) entry.getKey(), (String) entry.getValue());
53              }
54          }
55      }
56  
57      public void setUsersMap(Map<String, String> users) {
58          this.users = users;
59      }
60  
61      public void afterPropertiesSet() throws Exception {
62          Assert.notNull(users, "users is required");
63      }
64  
65      @Override
66      protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
67          if (callback instanceof PasswordValidationCallback) {
68              PasswordValidationCallback passwordCallback = (PasswordValidationCallback) callback;
69              if (passwordCallback.getRequest() instanceof PasswordValidationCallback.PlainTextPasswordRequest) {
70                  passwordCallback.setValidator(new SimplePlainTextPasswordValidator());
71              }
72              else if (passwordCallback.getRequest() instanceof PasswordValidationCallback.DigestPasswordRequest) {
73                  PasswordValidationCallback.DigestPasswordRequest digestPasswordRequest =
74                          (PasswordValidationCallback.DigestPasswordRequest) passwordCallback.getRequest();
75                  String password = users.get(digestPasswordRequest.getUsername());
76                  digestPasswordRequest.setPassword(password);
77                  passwordCallback.setValidator(new PasswordValidationCallback.DigestPasswordValidator());
78              }
79          }
80          else if (callback instanceof TimestampValidationCallback) {
81              TimestampValidationCallback timestampCallback = (TimestampValidationCallback) callback;
82              timestampCallback.setValidator(new DefaultTimestampValidator());
83          }
84          else {
85              throw new UnsupportedCallbackException(callback);
86          }
87      }
88  
89      private class SimplePlainTextPasswordValidator implements PasswordValidationCallback.PasswordValidator {
90  
91          public boolean validate(PasswordValidationCallback.Request request)
92                  throws PasswordValidationCallback.PasswordValidationException {
93              PasswordValidationCallback.PlainTextPasswordRequest plainTextPasswordRequest =
94                      (PasswordValidationCallback.PlainTextPasswordRequest) request;
95              String password = users.get(plainTextPasswordRequest.getUsername());
96              return password != null && password.equals(plainTextPasswordRequest.getPassword());
97          }
98      }
99  }