|
| 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; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.security.cert.X509Certificate; |
| 21 | +import javax.security.auth.callback.Callback; |
| 22 | +import javax.security.auth.callback.CallbackHandler; |
| 23 | +import javax.security.auth.callback.UnsupportedCallbackException; |
| 24 | + |
| 25 | +import com.sun.xml.wss.impl.callback.CertificateValidationCallback; |
| 26 | +import com.sun.xml.wss.impl.callback.PasswordValidationCallback; |
| 27 | +import com.sun.xml.wss.impl.callback.TimestampValidationCallback; |
| 28 | + |
| 29 | +import org.springframework.ws.soap.security.callback.AbstractCallbackHandler; |
| 30 | + |
| 31 | +/** |
| 32 | + * Represents a chain of <code>CallbackHandler</code>s. For each callback, each of the handlers is called in term. If a |
| 33 | + * handler throws a <code>UnsupportedCallbackException</code>, the next handler is tried. |
| 34 | + * |
| 35 | + * @author Arjen Poutsma |
| 36 | + * @since 1.0.0 |
| 37 | + */ |
| 38 | +public class CallbackHandlerChain extends AbstractCallbackHandler { |
| 39 | + |
| 40 | + private CallbackHandler[] callbackHandlers; |
| 41 | + |
| 42 | + public CallbackHandlerChain(CallbackHandler[] callbackHandlers) { |
| 43 | + this.callbackHandlers = callbackHandlers; |
| 44 | + } |
| 45 | + |
| 46 | + public void setCallbackHandlers(CallbackHandler[] callbackHandlers) { |
| 47 | + this.callbackHandlers = callbackHandlers; |
| 48 | + } |
| 49 | + |
| 50 | + protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException { |
| 51 | + if (callback instanceof CertificateValidationCallback) { |
| 52 | + handleCertificateValidationCallback((CertificateValidationCallback) callback); |
| 53 | + } |
| 54 | + else if (callback instanceof PasswordValidationCallback) { |
| 55 | + handlePasswordValidationCallback((PasswordValidationCallback) callback); |
| 56 | + } |
| 57 | + else if (callback instanceof TimestampValidationCallback) { |
| 58 | + handleTimestampValidationCallback((TimestampValidationCallback) callback); |
| 59 | + } |
| 60 | + else { |
| 61 | + boolean allUnsupported = true; |
| 62 | + for (int i = 0; i < callbackHandlers.length; i++) { |
| 63 | + CallbackHandler callbackHandler = callbackHandlers[i]; |
| 64 | + try { |
| 65 | + callbackHandler.handle(new Callback[]{callback}); |
| 66 | + allUnsupported = false; |
| 67 | + } |
| 68 | + catch (UnsupportedCallbackException ex) { |
| 69 | + // if an UnsupportedCallbackException occurs, go to the next handler |
| 70 | + } |
| 71 | + } |
| 72 | + if (allUnsupported) { |
| 73 | + throw new UnsupportedCallbackException(callback); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void handleCertificateValidationCallback(CertificateValidationCallback callback) { |
| 79 | + callback.setValidator(new CertificateValidatorChain(callback)); |
| 80 | + } |
| 81 | + |
| 82 | + private void handlePasswordValidationCallback(PasswordValidationCallback callback) { |
| 83 | + callback.setValidator(new PasswordValidatorChain(callback)); |
| 84 | + } |
| 85 | + |
| 86 | + private void handleTimestampValidationCallback(TimestampValidationCallback callback) { |
| 87 | + callback.setValidator(new TimestampValidatorChain(callback)); |
| 88 | + } |
| 89 | + |
| 90 | + private class TimestampValidatorChain implements TimestampValidationCallback.TimestampValidator { |
| 91 | + |
| 92 | + private TimestampValidationCallback callback; |
| 93 | + |
| 94 | + private TimestampValidatorChain(TimestampValidationCallback callback) { |
| 95 | + this.callback = callback; |
| 96 | + } |
| 97 | + |
| 98 | + public void validate(TimestampValidationCallback.Request request) |
| 99 | + throws TimestampValidationCallback.TimestampValidationException { |
| 100 | + for (int i = 0; i < callbackHandlers.length; i++) { |
| 101 | + CallbackHandler callbackHandler = callbackHandlers[i]; |
| 102 | + try { |
| 103 | + callbackHandler.handle(new Callback[]{callback}); |
| 104 | + callback.getResult(); |
| 105 | + } |
| 106 | + catch (IOException e) { |
| 107 | + throw new TimestampValidationCallback.TimestampValidationException(e); |
| 108 | + } |
| 109 | + catch (UnsupportedCallbackException e) { |
| 110 | + // ignore |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private class PasswordValidatorChain implements PasswordValidationCallback.PasswordValidator { |
| 117 | + |
| 118 | + private PasswordValidationCallback callback; |
| 119 | + |
| 120 | + private PasswordValidatorChain(PasswordValidationCallback callback) { |
| 121 | + this.callback = callback; |
| 122 | + } |
| 123 | + |
| 124 | + public boolean validate(PasswordValidationCallback.Request request) |
| 125 | + throws PasswordValidationCallback.PasswordValidationException { |
| 126 | + boolean allUnsupported = true; |
| 127 | + for (int i = 0; i < callbackHandlers.length; i++) { |
| 128 | + CallbackHandler callbackHandler = callbackHandlers[i]; |
| 129 | + try { |
| 130 | + callbackHandler.handle(new Callback[]{callback}); |
| 131 | + allUnsupported = false; |
| 132 | + if (!callback.getResult()) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + } |
| 136 | + catch (IOException e) { |
| 137 | + throw new PasswordValidationCallback.PasswordValidationException(e); |
| 138 | + } |
| 139 | + catch (UnsupportedCallbackException e) { |
| 140 | + // ignore |
| 141 | + } |
| 142 | + } |
| 143 | + return !allUnsupported; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private class CertificateValidatorChain implements CertificateValidationCallback.CertificateValidator { |
| 148 | + |
| 149 | + private CertificateValidationCallback callback; |
| 150 | + |
| 151 | + private CertificateValidatorChain(CertificateValidationCallback callback) { |
| 152 | + this.callback = callback; |
| 153 | + } |
| 154 | + |
| 155 | + public boolean validate(X509Certificate certificate) |
| 156 | + throws CertificateValidationCallback.CertificateValidationException { |
| 157 | + boolean allUnsupported = true; |
| 158 | + for (int i = 0; i < callbackHandlers.length; i++) { |
| 159 | + CallbackHandler callbackHandler = callbackHandlers[i]; |
| 160 | + try { |
| 161 | + callbackHandler.handle(new Callback[]{callback}); |
| 162 | + allUnsupported = false; |
| 163 | + if (!callback.getResult()) { |
| 164 | + return false; |
| 165 | + } |
| 166 | + } |
| 167 | + catch (IOException e) { |
| 168 | + throw new CertificateValidationCallback.CertificateValidationException(e); |
| 169 | + } |
| 170 | + catch (UnsupportedCallbackException e) { |
| 171 | + // ignore |
| 172 | + } |
| 173 | + } |
| 174 | + return !allUnsupported; |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments