001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.soap.security.xwss.callback;
018:
019: import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
020: import com.sun.xml.wss.impl.callback.TimestampValidationCallback;
021: import org.springframework.beans.factory.InitializingBean;
022: import org.springframework.util.Assert;
023:
024: import javax.security.auth.callback.Callback;
025: import javax.security.auth.callback.UnsupportedCallbackException;
026: import java.io.IOException;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Properties;
030:
031: /**
032: * Simple callback handler that validates passwords agains a in-memory <code>Properties</code> object. Password
033: * validation is done on a case-sensitive basis.
034: * <p/>
035: * This class only handles <code>PasswordValidationCallback</code>s, and throws an
036: * <code>UnsupportedCallbackException</code> for others
037: *
038: * @author Arjen Poutsma
039: * @see #setUsers(java.util.Properties)
040: */
041: public class SimplePasswordValidationCallbackHandler extends
042: AbstractCallbackHandler implements InitializingBean {
043:
044: private Properties users = new Properties();
045:
046: /**
047: * Sets the users to validate against. Property names are usernames, property values are passwords.
048: */
049: public void setUsers(Properties users) {
050: this .users = users;
051: }
052:
053: public void afterPropertiesSet() throws Exception {
054: Assert.notNull(users, "users is required");
055: }
056:
057: protected void handleInternal(Callback callback)
058: throws IOException, UnsupportedCallbackException {
059: if (callback instanceof PasswordValidationCallback) {
060: PasswordValidationCallback passwordCallback = (PasswordValidationCallback) callback;
061: if (passwordCallback.getRequest() instanceof PasswordValidationCallback.PlainTextPasswordRequest) {
062: passwordCallback
063: .setValidator(new SimplePlainTextPasswordValidator());
064: } else if (passwordCallback.getRequest() instanceof PasswordValidationCallback.DigestPasswordRequest) {
065: PasswordValidationCallback.DigestPasswordRequest digestPasswordRequest = (PasswordValidationCallback.DigestPasswordRequest) passwordCallback
066: .getRequest();
067: String password = users
068: .getProperty(digestPasswordRequest
069: .getUsername());
070: digestPasswordRequest.setPassword(password);
071: passwordCallback
072: .setValidator(new PasswordValidationCallback.DigestPasswordValidator());
073: }
074: } else if (callback instanceof TimestampValidationCallback) {
075: TimestampValidationCallback timestampCallback = (TimestampValidationCallback) callback;
076: timestampCallback
077: .setValidator(new DefaultTimestampValidator());
078: } else {
079: throw new UnsupportedCallbackException(callback);
080: }
081: }
082:
083: public void setUsersMap(Map users) {
084: for (Iterator iterator = users.keySet().iterator(); iterator
085: .hasNext();) {
086: String username = (String) iterator.next();
087: String password = (String) users.get(username);
088: this .users.setProperty(username, password);
089: }
090: }
091:
092: private class SimplePlainTextPasswordValidator implements
093: PasswordValidationCallback.PasswordValidator {
094:
095: public boolean validate(
096: PasswordValidationCallback.Request request)
097: throws PasswordValidationCallback.PasswordValidationException {
098: PasswordValidationCallback.PlainTextPasswordRequest plainTextPasswordRequest = (PasswordValidationCallback.PlainTextPasswordRequest) request;
099: String password = users
100: .getProperty(plainTextPasswordRequest.getUsername());
101: return password != null
102: && password.equals(plainTextPasswordRequest
103: .getPassword());
104: }
105: }
106: }
|