01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.jndi.provider.ldap.sasl;
19:
20: import java.util.Hashtable;
21:
22: import javax.naming.Context;
23: import javax.security.auth.callback.Callback;
24: import javax.security.auth.callback.CallbackHandler;
25: import javax.security.auth.callback.NameCallback;
26: import javax.security.auth.callback.PasswordCallback;
27: import javax.security.auth.callback.UnsupportedCallbackException;
28: import javax.security.sasl.RealmCallback;
29: import javax.security.sasl.RealmChoiceCallback;
30:
31: import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
32:
33: /*
34: * Default callback handler, may be customized through
35: * "java.naming.security.sasl.realm".
36: */
37: class DefaultCallbackHandler implements CallbackHandler {
38:
39: private static final String JAVA_NAMING_SECURITY_SASL_REALM = "java.naming.security.sasl.realm";
40:
41: private Hashtable env;
42:
43: private String realm = "";
44:
45: public DefaultCallbackHandler() {
46:
47: }
48:
49: public DefaultCallbackHandler(Hashtable env) {
50: this .env = env;
51: }
52:
53: public void handle(Callback[] callbacks)
54: throws java.io.IOException, UnsupportedCallbackException {
55: for (int i = 0; i < callbacks.length; i++) {
56: if (callbacks[i] instanceof RealmChoiceCallback) {
57: // TODO what to do here?
58: // RealmChoiceCallback rcc = (RealmChoiceCallback) callbacks[i];
59:
60: } else if (callbacks[i] instanceof RealmCallback) {
61: RealmCallback rc = (RealmCallback) callbacks[i];
62: if (env.get(JAVA_NAMING_SECURITY_SASL_REALM) != null) {
63: realm = (String) env
64: .get(JAVA_NAMING_SECURITY_SASL_REALM);
65: rc.setText(realm);
66: } else {
67: rc.setText(realm);
68: }
69: } else if (callbacks[i] instanceof PasswordCallback) {
70: PasswordCallback pc = (PasswordCallback) callbacks[i];
71: pc.setPassword(Utils.getCharArray(env
72: .get(Context.SECURITY_CREDENTIALS)));
73: } else if (callbacks[i] instanceof NameCallback) {
74: //authentication Id
75: NameCallback nc = (NameCallback) callbacks[i];
76: nc
77: .setName((String) env
78: .get(Context.SECURITY_PRINCIPAL));
79: } else {
80: throw new UnsupportedCallbackException(callbacks[i]);
81: }
82: }
83: }
84:
85: public void setRealm(String realm) {
86: this.realm = realm;
87: }
88: }
|