001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.collab.xmpp;
043:
044: import java.util.LinkedList;
045: import java.util.List;
046: import javax.security.auth.callback.Callback;
047: import javax.security.auth.callback.CallbackHandler;
048: import javax.security.auth.callback.NameCallback;
049: import javax.security.auth.callback.PasswordCallback;
050: import javax.security.auth.callback.UnsupportedCallbackException;
051: import org.jabberstudio.jso.sasl.SASLClientInfo;
052: import org.jabberstudio.jso.sasl.SASLMechanism;
053: import org.jabberstudio.jso.sasl.SASLMechanismManager;
054: import org.jabberstudio.jso.sasl.SASLPacket;
055: import org.netbeans.lib.collab.SASLClientProvider;
056: import org.netbeans.lib.collab.SASLData;
057: import org.netbeans.lib.collab.SASLProviderException;
058:
059: /**
060: * Implementation which delegates to JSO (native) providers for SASL.
061: *
062: * @author Mridul Muralidharan
063: */
064: public class JSOSASLProvider implements NativeSASLClientProvider {
065: private String mechanism;
066: private String loginName;
067: private String password;
068: private String server;
069: private SASLMechanism saslMechanism;
070:
071: /** Creates a new instance of JSOSASLProvider */
072: public JSOSASLProvider(String mechanism) {
073: this .mechanism = mechanism;
074: }
075:
076: public void setLoginName(String loginName) {
077: this .loginName = loginName;
078: }
079:
080: public void setPassword(String password) {
081: this .password = password;
082: }
083:
084: public void setServer(String server) {
085: this .server = server;
086: }
087:
088: protected synchronized void initialise() {
089: if (null == saslMechanism) {
090:
091: CallbackHandler cbh = new CallbackHandler() {
092: public void handle(Callback[] callbacks)
093: throws java.io.IOException,
094: UnsupportedCallbackException {
095: int count = 0;
096:
097: while (count < callbacks.length) {
098: Callback cb = callbacks[count];
099: count++;
100: if (cb instanceof NameCallback) {
101: ((NameCallback) cb).setName(loginName);
102: }
103: if (cb instanceof PasswordCallback) {
104: ((PasswordCallback) cb)
105: .setPassword(password.toCharArray());
106: }
107: }
108: }
109: };
110:
111: SASLClientInfo client = new SASLClientInfo();
112: List tlist = new LinkedList();
113: tlist.add(mechanism);
114: client.setMechanismNames(tlist);
115: client.setCallbackHandler(cbh);
116: client.setServer(server);
117:
118: saslMechanism = SASLMechanismManager.getInstance()
119: .createClientMechanism(client);
120:
121: if (null == saslMechanism) {
122: // should not happen ....
123: throw new UnsupportedOperationException(
124: "SASL Mechanism : " + mechanism
125: + " does not seem to be supported");
126: }
127: }
128: }
129:
130: public void init() throws SASLProviderException {
131: loginName = null;
132: password = null;
133: server = null;
134: saslMechanism = null;
135: }
136:
137: public void close() {
138: loginName = null;
139: password = null;
140: server = null;
141: saslMechanism = null;
142: }
143:
144: public void process(SASLData data) throws SASLProviderException {
145: throw new SASLProviderException("unsupported method");
146: }
147:
148: public Object process(Object packet) throws SASLProviderException {
149: try {
150: initialise();
151:
152: return saslMechanism.evaluate((SASLPacket) packet);
153: } catch (Exception ex) {
154: throw new SASLProviderException(
155: "Exception evaluating sasl challenge by jso", ex);
156: }
157: }
158: }
|