Example usage for javax.security.auth.callback UnsupportedCallbackException UnsupportedCallbackException

List of usage examples for javax.security.auth.callback UnsupportedCallbackException UnsupportedCallbackException

Introduction

In this page you can find the example usage for javax.security.auth.callback UnsupportedCallbackException UnsupportedCallbackException.

Prototype

public UnsupportedCallbackException(Callback callback) 

Source Link

Document

Constructs an UnsupportedCallbackException with no detail message.

Usage

From source file:org.alfresco.web.site.servlet.SSOAuthenticationFilter.java

/**
 * JAAS callback handler/*from w  ww .j a v  a 2  s  .  c  o  m*/
 * 
 * @param callbacks Callback[]
 * @exception IOException
 * @exception UnsupportedCallbackException
 */
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    if (logger.isDebugEnabled())
        logger.debug("Processing the JAAS callback list of " + callbacks.length + " items.");
    for (int i = 0; i < callbacks.length; i++) {
        // Request for user name

        if (callbacks[i] instanceof NameCallback) {
            if (logger.isDebugEnabled())
                logger.debug("Request for user name.");
            NameCallback cb = (NameCallback) callbacks[i];
            cb.setName(krbAccountName);
        }

        // Request for password
        else if (callbacks[i] instanceof PasswordCallback) {
            if (logger.isDebugEnabled())
                logger.debug("Request for password.");
            PasswordCallback cb = (PasswordCallback) callbacks[i];
            cb.setPassword(krbPassword.toCharArray());
        }

        // Request for realm

        else if (callbacks[i] instanceof RealmCallback) {
            if (logger.isDebugEnabled())
                logger.debug("Request for realm.");
            RealmCallback cb = (RealmCallback) callbacks[i];
            cb.setText(krbRealm);
        } else {
            throw new UnsupportedCallbackException(callbacks[i]);
        }
    }
}

From source file:org.apache.hadoop.io.crypto.bee.key.sasl.KeySaslClient.java

public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {

    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
            // logger.debug("set name");
            NameCallback ncb = (NameCallback) callbacks[i];
            ncb.setName(keyToken.getUser());
        } else if (callbacks[i] instanceof PasswordCallback) {
            // logger.debug("set password");
            PasswordCallback pcb = (PasswordCallback) callbacks[i];
            pcb.setPassword(new String(keyToken.getPassword()).toCharArray());
            // logger.debug("set password:" +
            // Hex.encodeHexString(keyToken.getPassword()));
        } else if (callbacks[i] instanceof RealmCallback) {
            // logger.debug("set realm");
            RealmCallback rcb = (RealmCallback) callbacks[i];
            rcb.setText(SaslUtil.KEY_REALM);
        } else {//from w  w  w . j a  v a 2 s  .c  o  m
            throw new UnsupportedCallbackException(callbacks[i]);
        }
    }
}

From source file:org.apache.servicemix.nmr.core.security.JaasAuthenticationService.java

public void authenticate(Subject subject, String domain, final String user, final Object credentials)
        throws GeneralSecurityException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Authenticating '" + user + "' with '" + credentials + "'");
    }//from  ww  w  .  ja v  a2 s  .  co m
    LoginContext loginContext = new LoginContext(domain, subject, new CallbackHandler() {
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (int i = 0; i < callbacks.length; i++) {
                if (callbacks[i] instanceof NameCallback) {
                    ((NameCallback) callbacks[i]).setName(user);
                } else if (callbacks[i] instanceof PasswordCallback && credentials instanceof String) {
                    ((PasswordCallback) callbacks[i]).setPassword(((String) credentials).toCharArray());
                } else if (callbacks[i] instanceof CertificateCallback
                        && credentials instanceof X509Certificate) {
                    ((CertificateCallback) callbacks[i]).setCertificate((X509Certificate) credentials);
                } else {
                    throw new UnsupportedCallbackException(callbacks[i]);
                }
            }
        }
    });
    loginContext.login();
}

From source file:org.eclipse.ice.core.internal.BasicAuthSecuredContext.java

/**
 * This operation checks the credentials.
 * // w  w w  . j a v  a2  s.  com
 * @param request
 *            The HTTP request
 * @param userid
 *            The userid
 * @param password
 *            The password
 * @return A Login Subject or null if authentication failed
 * @throws LoginException
 */
private Subject login(HttpServletRequest request, final String userid, final String password)
        throws LoginException {

    // Make sure the session is valid
    HttpSession session = request.getSession(false);
    if (session == null) {
        return null;
    }
    // Get the LoginContext that is configured for authentication
    ILoginContext context = (ILoginContext) session.getAttribute("securitycontext");
    if (context != null) {
        return context.getSubject();
    }
    // Create a login context if one does not exist. The Login context must
    // be available as a plugin to the Equinox Extension Registry and
    // provide an implementation of LoginModule.
    context = LoginContextFactory.createContext("SimpleConfig", configFile, new CallbackHandler() {
        // Provide a callback handler to check the user name and
        // password
        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (int i = 0; i < callbacks.length; i++) {
                if (callbacks[i] instanceof NameCallback) {
                    ((NameCallback) callbacks[i]).setName(userid);
                } else if (callbacks[i] instanceof PasswordCallback) {
                    ((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
                } else {
                    throw new UnsupportedCallbackException(callbacks[i]);
                }
            }
        }
    });
    // Try the login and set the context on the session if it works
    Subject result = context.getSubject();
    session.setAttribute("securitycontext", context);

    return result;
}

From source file:org.marketcetera.modules.remote.receiver.ClientLoginModuleTest.java

/**
 * test unsupported callbacks//from w  ww  .  j  av  a 2  s .c  o m
 * @throws Exception if there was failure
 */
@Test
public void unsupportedCallback() throws Exception {
    doNotHandleCallbacks = true;
    UnsupportedCallbackException uce = new UnsupportedCallbackException(
            new NameCallback(Messages.PROMPT_USERNAME.getText()));
    LoginException ex = attemptLogin(getTestUsername(), getTestPassword(), LoginException.class,
            uce.getMessage());
    assertNotNull(ex.getCause());
    assertTrue(ex.getCause() instanceof UnsupportedCallbackException);
    Callback callback = ((UnsupportedCallbackException) ex.getCause()).getCallback();
    assertNotNull(callback);
    assertTrue(callback.getClass().toString(), callback instanceof NameCallback);
    org.junit.Assert.assertEquals(Messages.PROMPT_USERNAME.getText(), ((NameCallback) callback).getPrompt());
}

From source file:org.openhab.io.net.http.SecureHttpContext.java

/**
 * <p>Authenticates the given <code>username</code> and <code>password</code>
 * with respect to the given <code>realm</code> against the configured
 * {@link LoginModule} (see login.conf in &lt;openhabhome&gt;/etc to learn
 * more about the configured {@link LoginModule})</p>
 * <p><b>Note:</b>Roles aren't supported yet!</p>
 * //from  www .j a v a 2s  .  c o m
 * @param realm the realm used by the configured {@link LoginModule}. 
 * <i>Note:</i> the given <code>realm</code> must be same name as configured
 * in <code>login.conf</code>
 * @param username
 * @param password
 * 
 * @return a {@link Subject} filled with username, password, realm, etc. or
 * <code>null</code> if the login failed
 * @throws UnsupportedCallbackException if a {@link Callback}-instance other
 * than {@link NameCallback} or {@link ObjectCallback} is going to be handled
 */
private Subject authenticate(final String realm, final String username, final String password) {
    try {
        logger.trace("going to authenticate user '{}', realm '{}'", username, realm);

        Subject subject = new Subject();

        LoginContext lContext = new LoginContext(realm, subject, new CallbackHandler() {
            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                for (int i = 0; i < callbacks.length; i++) {
                    if (callbacks[i] instanceof NameCallback) {
                        ((NameCallback) callbacks[i]).setName(username);
                    } else if (callbacks[i] instanceof ObjectCallback) {
                        ((ObjectCallback) callbacks[i]).setObject(password);
                    } else {
                        throw new UnsupportedCallbackException(callbacks[i]);
                    }
                }
            }
        });
        lContext.login();

        // TODO: TEE: implement role handling here!

        return subject;
    } catch (LoginException le) {
        logger.warn("authentication of user '" + username + "' failed", le);
        return null;
    }
}