Search in sources :

Example 1 with CallbackHandler

use of org.eclipse.ecf.core.security.CallbackHandler in project ecf by eclipse.

the class HttpClientFileSystemBrowser method getFileRequestCredentials.

/**
 * Retrieves the credentials for requesting the file.
 * @return the {@link Credentials} necessary to retrieve the file
 * @throws UnsupportedCallbackException if the callback fails
 * @throws IOException if IO fails
 * @since 5.0
 */
protected Credentials getFileRequestCredentials() throws UnsupportedCallbackException, IOException {
    if (connectContext == null)
        return null;
    final CallbackHandler callbackHandler = connectContext.getCallbackHandler();
    if (callbackHandler == null)
        return null;
    final NameCallback usernameCallback = new NameCallback(USERNAME_PREFIX);
    final ObjectCallback passwordCallback = new ObjectCallback();
    callbackHandler.handle(new Callback[] { usernameCallback, passwordCallback });
    username = usernameCallback.getName();
    password = (String) passwordCallback.getObject();
    return new UsernamePasswordCredentials(username, password);
}
Also used : CallbackHandler(org.eclipse.ecf.core.security.CallbackHandler) NameCallback(org.eclipse.ecf.core.security.NameCallback) ObjectCallback(org.eclipse.ecf.core.security.ObjectCallback) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 2 with CallbackHandler

use of org.eclipse.ecf.core.security.CallbackHandler in project ecf by eclipse.

the class HttpClientRetrieveFileTransfer method getFileRequestCredentials.

/**
 * @return Credentials file request credentials
 * @throws UnsupportedCallbackException if some problem
 * @throws IOException if some problem
 * @since 5.0
 */
protected Credentials getFileRequestCredentials() throws UnsupportedCallbackException, IOException {
    if (connectContext == null)
        return null;
    final CallbackHandler callbackHandler = connectContext.getCallbackHandler();
    if (callbackHandler == null)
        return null;
    final NameCallback usernameCallback = new NameCallback(USERNAME_PREFIX);
    final ObjectCallback passwordCallback = new ObjectCallback();
    callbackHandler.handle(new Callback[] { usernameCallback, passwordCallback });
    username = usernameCallback.getName();
    password = (String) passwordCallback.getObject();
    return new UsernamePasswordCredentials(username, password);
}
Also used : CallbackHandler(org.eclipse.ecf.core.security.CallbackHandler) NameCallback(org.eclipse.ecf.core.security.NameCallback) ObjectCallback(org.eclipse.ecf.core.security.ObjectCallback) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 3 with CallbackHandler

use of org.eclipse.ecf.core.security.CallbackHandler in project ecf by eclipse.

the class ECFConnection method connect.

public synchronized Object connect(ID remote, Object data, int timeout) throws ECFException {
    if (connection != null)
        throw new ECFException("already connected");
    if (timeout > 0)
        SmackConfiguration.setPacketReplyTimeout(timeout);
    Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
    final XMPPID jabberURI = getXMPPID(remote);
    String username = jabberURI.getNodename();
    String hostname = jabberURI.getHostname();
    String hostnameOverride = null;
    // Check for the URI form of "joe@bloggs.org;talk.google.com", which
    // would at this point would have
    // - username = "joe"
    // - hostname = "blogs.org;talk.google.com"
    // - hostnameOverride = null
    // 
    // We need to turn this into:
    // - username = "joe"
    // - hostname = "bloggs.org"
    // - hostnameOverride = "talk.google.com"
    int semiColonIdx = hostname.lastIndexOf(';');
    if (semiColonIdx != -1) {
        hostnameOverride = hostname.substring(semiColonIdx + 1);
        hostname = hostname.substring(0, semiColonIdx);
    }
    if (google && hostnameOverride == null) {
        hostnameOverride = GOOGLE_TALK_HOST;
    }
    final String serviceName = hostname;
    serverPort = jabberURI.getPort();
    serverResource = jabberURI.getResourceName();
    if (serverResource == null || serverResource.equals(XMPPID.PATH_DELIMITER)) {
        serverResource = getClientIdentifier();
        jabberURI.setResourceName(serverResource);
    }
    try {
        ConnectionConfiguration config;
        if (hostnameOverride != null) {
            config = new ConnectionConfiguration(hostnameOverride, XMPP_DEFAULT_PORT, serviceName);
        } else if (serverPort == -1) {
            config = new ConnectionConfiguration(serviceName);
        } else {
            config = new ConnectionConfiguration(serviceName, serverPort);
        }
        config.setSendPresence(true);
        // authentication; handler should provide keystore password:
        if (callbackHandler instanceof javax.security.auth.callback.CallbackHandler) {
            config.setCallbackHandler((javax.security.auth.callback.CallbackHandler) callbackHandler);
        }
        connection = new XMPPConnection(config);
        connection.connect();
        SASLAuthentication.supportSASLMechanism("PLAIN", 0);
        if (google || GOOGLE_TALK_HOST.equals(hostnameOverride)) {
            username = username + "@" + serviceName;
        }
        connection.addPacketListener(packetListener, null);
        connection.addConnectionListener(connectionListener);
        // Login
        connection.login(username, (String) data, serverResource);
        waitForBindResult();
    } catch (final XMPPException e) {
        throw new ContainerConnectException("Login attempt failed", e);
    }
    return jid;
}
Also used : CallbackHandler(org.eclipse.ecf.core.security.CallbackHandler) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ECFException(org.eclipse.ecf.core.util.ECFException) ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) ContainerConnectException(org.eclipse.ecf.core.ContainerConnectException) XMPPException(org.jivesoftware.smack.XMPPException) XMPPID(org.eclipse.ecf.provider.xmpp.identity.XMPPID)

Example 4 with CallbackHandler

use of org.eclipse.ecf.core.security.CallbackHandler in project ecf by eclipse.

the class AbstractBBContainer method getCredentialsFromConnectContext.

protected IBBCredentials getCredentialsFromConnectContext(IConnectContext connectContext) throws ContainerConnectException {
    try {
        if (connectContext == null) {
            return null;
        }
        Callback[] callbacks = new Callback[2];
        callbacks[0] = new NameCallback("Username");
        callbacks[1] = new ObjectCallback();
        CallbackHandler handler = connectContext.getCallbackHandler();
        if (handler != null) {
            handler.handle(callbacks);
        }
        NameCallback nc = (NameCallback) callbacks[0];
        ObjectCallback cb = (ObjectCallback) callbacks[1];
        return new Credentials(nc.getName(), (String) cb.getObject());
    } catch (Exception e) {
        throw new ContainerConnectException("Exception in CallbackHandler.handle(<callbacks>)", e);
    }
}
Also used : CallbackHandler(org.eclipse.ecf.core.security.CallbackHandler) ObjectCallback(org.eclipse.ecf.core.security.ObjectCallback) Callback(org.eclipse.ecf.core.security.Callback) NameCallback(org.eclipse.ecf.core.security.NameCallback) NameCallback(org.eclipse.ecf.core.security.NameCallback) ContainerConnectException(org.eclipse.ecf.core.ContainerConnectException) ObjectCallback(org.eclipse.ecf.core.security.ObjectCallback) IBBCredentials(org.eclipse.ecf.bulletinboard.IBBCredentials) ContainerConnectException(org.eclipse.ecf.core.ContainerConnectException) BBException(org.eclipse.ecf.bulletinboard.BBException)

Example 5 with CallbackHandler

use of org.eclipse.ecf.core.security.CallbackHandler in project ecf by eclipse.

the class UrlConnectionRetrieveFileTransfer method setupAuthentication.

protected void setupAuthentication() throws IOException, UnsupportedCallbackException {
    if (connectContext == null)
        return;
    final CallbackHandler callbackHandler = connectContext.getCallbackHandler();
    if (callbackHandler == null)
        return;
    final NameCallback usernameCallback = new NameCallback(USERNAME_PREFIX);
    final ObjectCallback passwordCallback = new ObjectCallback();
    // Call callback with username and password callbacks
    callbackHandler.handle(new Callback[] { usernameCallback, passwordCallback });
    username = usernameCallback.getName();
    Object o = passwordCallback.getObject();
    if (!(o instanceof String))
        throw new UnsupportedCallbackException(passwordCallback, Messages.UrlConnectionRetrieveFileTransfer_UnsupportedCallbackException);
    password = (String) passwordCallback.getObject();
    // Now set authenticator to our authenticator with user and password
    Authenticator.setDefault(new UrlConnectionAuthenticator());
}
Also used : CallbackHandler(org.eclipse.ecf.core.security.CallbackHandler) NameCallback(org.eclipse.ecf.core.security.NameCallback) ObjectCallback(org.eclipse.ecf.core.security.ObjectCallback) UnsupportedCallbackException(org.eclipse.ecf.core.security.UnsupportedCallbackException)

Aggregations

CallbackHandler (org.eclipse.ecf.core.security.CallbackHandler)8 NameCallback (org.eclipse.ecf.core.security.NameCallback)6 ObjectCallback (org.eclipse.ecf.core.security.ObjectCallback)5 ContainerConnectException (org.eclipse.ecf.core.ContainerConnectException)3 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)2 Callback (org.eclipse.ecf.core.security.Callback)2 UnsupportedCallbackException (org.eclipse.ecf.core.security.UnsupportedCallbackException)2 ECFException (org.eclipse.ecf.core.util.ECFException)2 XMPPException (org.jivesoftware.smack.XMPPException)2 IOException (java.io.IOException)1 BBException (org.eclipse.ecf.bulletinboard.BBException)1 IBBCredentials (org.eclipse.ecf.bulletinboard.IBBCredentials)1 ContainerConnectedEvent (org.eclipse.ecf.core.events.ContainerConnectedEvent)1 ContainerConnectingEvent (org.eclipse.ecf.core.events.ContainerConnectingEvent)1 IDCreateException (org.eclipse.ecf.core.identity.IDCreateException)1 IConnectContext (org.eclipse.ecf.core.security.IConnectContext)1 SharedObjectAddException (org.eclipse.ecf.core.sharedobject.SharedObjectAddException)1 ECFConnection (org.eclipse.ecf.internal.provider.xmpp.smack.ECFConnection)1 ConnectionCreateException (org.eclipse.ecf.provider.comm.ConnectionCreateException)1 XMPPID (org.eclipse.ecf.provider.xmpp.identity.XMPPID)1