001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.soap.handlers.security;
018:
019: import java.io.IOException;
020:
021: import javax.security.auth.callback.Callback;
022: import javax.security.auth.callback.CallbackHandler;
023: import javax.security.auth.callback.UnsupportedCallbackException;
024:
025: import org.apache.ws.security.WSPasswordCallback;
026:
027: /**
028: * Base implementation for security callback handler.
029: *
030: * @author gnodet
031: */
032: public class BaseSecurityCallbackHandler implements CallbackHandler {
033:
034: public void handle(Callback[] callbacks) throws IOException,
035: UnsupportedCallbackException {
036: if (callbacks == null || callbacks.length == 0) {
037: throw new IllegalStateException(
038: "callbacks is null or empty");
039: }
040: for (int i = 0; i < callbacks.length; i++) {
041: if (callbacks[i] instanceof WSPasswordCallback == false) {
042: throw new UnsupportedCallbackException(callbacks[i]);
043: }
044: processCallback((WSPasswordCallback) callbacks[i]);
045: }
046: }
047:
048: protected void processCallback(WSPasswordCallback callback)
049: throws IOException, UnsupportedCallbackException {
050: switch (callback.getUsage()) {
051: case WSPasswordCallback.DECRYPT:
052: processDecrypt(callback);
053: break;
054: case WSPasswordCallback.USERNAME_TOKEN:
055: processUsernameToken(callback);
056: break;
057: case WSPasswordCallback.SIGNATURE:
058: processSignature(callback);
059: break;
060: case WSPasswordCallback.KEY_NAME:
061: processKeyName(callback);
062: break;
063: case WSPasswordCallback.USERNAME_TOKEN_UNKNOWN:
064: processUsernameTokenUnkown(callback);
065: break;
066: default:
067: throw new UnsupportedCallbackException(callback);
068: }
069: }
070:
071: /**
072: * Need a password to get the private key of
073: * this identifier (username) from the keystore. WSS4J uses this private
074: * key to decrypt the session (symmetric) key. Because the encryption
075: * method uses the public key to encrypt the session key it needs no
076: * password (a public key is usually not protected by a password)
077: */
078: protected void processDecrypt(WSPasswordCallback callback)
079: throws IOException, UnsupportedCallbackException {
080: throw new UnsupportedCallbackException(callback);
081: }
082:
083: /**
084: * Need the password to fill in or to
085: * verify a <code>UsernameToken</code>
086: */
087: protected void processUsernameToken(WSPasswordCallback callback)
088: throws IOException, UnsupportedCallbackException {
089: throw new UnsupportedCallbackException(callback);
090: }
091:
092: /**
093: * Need the password to get the private key of
094: * this identifier (username) from the keystore. WSS4J uses this private
095: * key to produce a signature. The signature verfication uses the public
096: * key to verfiy the signature
097: */
098: protected void processSignature(WSPasswordCallback callback)
099: throws IOException, UnsupportedCallbackException {
100: throw new UnsupportedCallbackException(callback);
101: }
102:
103: /**
104: * Need the <i>key</i>, not the password,
105: * associated with the identifier. WSS4J uses this key to encrypt or
106: * decrypt parts of the SOAP request. Note, the key must match the
107: * symmetric encryption/decryption algorithm specified (refer to
108: * {@link org.apache.ws.security.handler.WSHandlerConstants#ENC_SYM_ALGO})
109: */
110: protected void processKeyName(WSPasswordCallback callback)
111: throws IOException, UnsupportedCallbackException {
112: throw new UnsupportedCallbackException(callback);
113: }
114:
115: /**
116: * Either a not specified
117: * password type or a password type passwordText. In these both cases <b>only</b>
118: * the password variable is <b>set</>. The callback class now may check if
119: * the username and password match. If they don't match the callback class must
120: * throw an exception. The exception can be a UnsupportedCallbackException or
121: * an IOException.</li>
122: */
123: protected void processUsernameTokenUnkown(
124: WSPasswordCallback callback) throws IOException,
125: UnsupportedCallbackException {
126: throw new UnsupportedCallbackException(callback);
127: }
128:
129: }
|