01: // ========================================================================
02: // $Id: DefaultCallbackHandler.java 305 2006-03-07 10:32:14Z janb $
03: // Copyright 2003-2004 Mort Bay Consulting Pty. Ltd.
04: // ------------------------------------------------------------------------
05: // Licensed under the Apache License, Version 2.0 (the "License");
06: // you may not use this file except in compliance with the License.
07: // You may obtain a copy of the License at
08: // http://www.apache.org/licenses/LICENSE-2.0
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14: // ========================================================================
15:
16: package org.mortbay.jetty.plus.jaas.callback;
17:
18: import java.io.IOException;
19: import java.util.Arrays;
20:
21: import javax.security.auth.callback.Callback;
22: import javax.security.auth.callback.NameCallback;
23: import javax.security.auth.callback.PasswordCallback;
24: import javax.security.auth.callback.UnsupportedCallbackException;
25:
26: import org.mortbay.jetty.Request;
27: import org.mortbay.jetty.security.Password;
28:
29: /* ---------------------------------------------------- */
30: /** DefaultUsernameCredentialCallbackHandler
31: * <p>
32: *
33: * <p><h4>Notes</h4>
34: * <p>
35: *
36: * <p><h4>Usage</h4>
37: * <pre>
38: */
39: /*
40: * </pre>
41: *
42: * @see
43: * @version 1.0 Tue Apr 15 2003
44: * @author Jan Bartel (janb)
45: */
46: public class DefaultCallbackHandler extends AbstractCallbackHandler {
47:
48: private Request request;
49:
50: public void setRequest(Request request) {
51: this .request = request;
52: }
53:
54: public void handle(Callback[] callbacks) throws IOException,
55: UnsupportedCallbackException {
56: for (int i = 0; i < callbacks.length; i++) {
57: if (callbacks[i] instanceof NameCallback) {
58: ((NameCallback) callbacks[i]).setName(getUserName());
59: } else if (callbacks[i] instanceof ObjectCallback) {
60: ((ObjectCallback) callbacks[i])
61: .setObject(getCredential());
62: } else if (callbacks[i] instanceof PasswordCallback) {
63: if (getCredential() instanceof Password)
64: ((PasswordCallback) callbacks[i])
65: .setPassword(((Password) getCredential())
66: .toString().toCharArray());
67: else if (getCredential() instanceof String) {
68: ((PasswordCallback) callbacks[i])
69: .setPassword(((String) getCredential())
70: .toCharArray());
71: } else
72: throw new UnsupportedCallbackException(
73: callbacks[i],
74: "User supplied credentials cannot be converted to char[] for PasswordCallback: try using an ObjectCallback instead");
75: } else if (callbacks[i] instanceof RequestParameterCallback) {
76: RequestParameterCallback callback = (RequestParameterCallback) callbacks[i];
77: callback.setParameterValues(Arrays
78: .asList(request.getParameterValues(callback
79: .getParameterName())));
80: } else
81: throw new UnsupportedCallbackException(callbacks[i]);
82: }
83:
84: }
85:
86: }
|