Search in sources :

Example 6 with RealmCallback

use of javax.security.sasl.RealmCallback in project alluxio by Alluxio.

the class PlainSaslClientCallbackHandlerTest method unsupportCallback.

/**
   * Tests that an exception is thrown in case an unsupported callback is used.
   */
@Test
public void unsupportCallback() throws Exception {
    mThrown.expect(UnsupportedCallbackException.class);
    mThrown.expectMessage(RealmCallback.class + " is unsupported.");
    Callback[] callbacks = new Callback[3];
    callbacks[0] = new NameCallback("Username:");
    callbacks[1] = new PasswordCallback("Password:", true);
    callbacks[2] = new RealmCallback("Realm:");
    String user = "alluxio-user-2";
    String password = "alluxio-user-2-password";
    CallbackHandler clientCBHandler = new PlainSaslClientCallbackHandler(user, password);
    clientCBHandler.handle(callbacks);
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) RealmCallback(javax.security.sasl.RealmCallback) Test(org.junit.Test)

Example 7 with RealmCallback

use of javax.security.sasl.RealmCallback in project apex-core by apache.

the class DefaultCallbackHandler method processCallback.

protected void processCallback(Callback callback) throws IOException, UnsupportedCallbackException {
    if (callback instanceof NameCallback) {
        NameCallback namecb = (NameCallback) callback;
        namecb.setName(context.getValue(SecurityContext.USER_NAME));
    } else if (callback instanceof PasswordCallback) {
        PasswordCallback passcb = (PasswordCallback) callback;
        passcb.setPassword(context.getValue(SecurityContext.PASSWORD));
    } else if (callback instanceof RealmCallback) {
        RealmCallback realmcb = (RealmCallback) callback;
        realmcb.setText(context.getValue(SecurityContext.REALM));
    } else if (callback instanceof TextOutputCallback) {
        TextOutputCallback textcb = (TextOutputCallback) callback;
        if (textcb.getMessageType() == TextOutputCallback.INFORMATION) {
            logger.info(textcb.getMessage());
        } else if (textcb.getMessageType() == TextOutputCallback.WARNING) {
            logger.warn(textcb.getMessage());
        } else if (textcb.getMessageType() == TextOutputCallback.ERROR) {
            logger.error(textcb.getMessage());
        } else {
            logger.debug("Auth message type {}, message {}", textcb.getMessageType(), textcb.getMessage());
        }
    } else {
        throw new UnsupportedCallbackException(callback);
    }
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RealmCallback(javax.security.sasl.RealmCallback)

Example 8 with RealmCallback

use of javax.security.sasl.RealmCallback in project apex-core by apache.

the class DefaultCallbackHandlerTest method testHandler.

@Test
public void testHandler() {
    DefaultCallbackHandler handler = new DefaultCallbackHandler();
    SecurityContext context = new SecurityContext();
    handler.setup(context);
    Callback[] callbacks = new Callback[3];
    callbacks[0] = new NameCallback("UserName:");
    callbacks[1] = new PasswordCallback("Password:", false);
    callbacks[2] = new RealmCallback("Realm:");
    try {
        handler.handle(callbacks);
        Assert.assertEquals("Username", "user1", ((NameCallback) callbacks[0]).getName());
        Assert.assertEquals("Password", "pass", new String(((PasswordCallback) callbacks[1]).getPassword()));
        Assert.assertEquals("Realm", "default", ((RealmCallback) callbacks[2]).getText());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    } catch (UnsupportedCallbackException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RealmCallback(javax.security.sasl.RealmCallback) Test(org.junit.Test)

Example 9 with RealmCallback

use of javax.security.sasl.RealmCallback in project wildfly by wildfly.

the class RealmDirectLoginModule method getUsersPassword.

/**
     * @see org.jboss.security.auth.spi.UsernamePasswordLoginModule#getUsersPassword()
     */
@Override
protected String getUsersPassword() throws LoginException {
    if (validationMode == ValidationMode.VALIDATION) {
        return null;
    }
    RealmCallback rcb = new RealmCallback("Realm", securityRealm.getName());
    NameCallback ncb = new NameCallback("User Name", getUsername());
    String password = null;
    switch(validationMode) {
        case DIGEST:
            CredentialCallback cc = new CredentialCallback(PasswordCredential.class, ALGORITHM_DIGEST_MD5);
            handle(new Callback[] { rcb, ncb, cc });
            PasswordCredential passwordCredential = (PasswordCredential) cc.getCredential();
            DigestPassword digestPassword = passwordCredential.getPassword(DigestPassword.class);
            password = ByteIterator.ofBytes(digestPassword.getDigest()).hexEncode().drainToString();
            break;
        case PASSWORD:
            PasswordCallback pcb = new PasswordCallback("Password", false);
            handle(new Callback[] { rcb, ncb, pcb });
            password = String.valueOf(pcb.getPassword());
            break;
    }
    return password;
}
Also used : DigestPassword(org.wildfly.security.password.interfaces.DigestPassword) NameCallback(javax.security.auth.callback.NameCallback) PasswordCredential(org.wildfly.security.credential.PasswordCredential) PasswordCallback(javax.security.auth.callback.PasswordCallback) CredentialCallback(org.wildfly.security.auth.callback.CredentialCallback) RealmCallback(javax.security.sasl.RealmCallback)

Example 10 with RealmCallback

use of javax.security.sasl.RealmCallback in project wildfly by wildfly.

the class RealmDirectLoginModule method validatePassword.

@Override
protected boolean validatePassword(String inputPassword, String expectedPassword) {
    if (digestCredential != null) {
        return digestCredential.verifyHA1(expectedPassword.getBytes(UTF_8));
    }
    switch(validationMode) {
        case DIGEST:
            String inputHashed = hashUtil.generateHashedHexURP(getUsername(), securityRealm.getName(), inputPassword.toCharArray());
            return expectedPassword.equals(inputHashed);
        case PASSWORD:
            return expectedPassword.equals(inputPassword);
        case VALIDATION:
            RealmCallback rcb = new RealmCallback("Realm", securityRealm.getName());
            NameCallback ncb = new NameCallback("User Name", getUsername());
            EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence(inputPassword.toCharArray()));
            try {
                handle(new Callback[] { rcb, ncb, evc });
                return evc.isVerified();
            } catch (LoginException e) {
                return false;
            }
        default:
            return false;
    }
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) PasswordGuessEvidence(org.wildfly.security.evidence.PasswordGuessEvidence) LoginException(javax.security.auth.login.LoginException) EvidenceVerifyCallback(org.wildfly.security.auth.callback.EvidenceVerifyCallback) RealmCallback(javax.security.sasl.RealmCallback)

Aggregations

RealmCallback (javax.security.sasl.RealmCallback)17 NameCallback (javax.security.auth.callback.NameCallback)14 PasswordCallback (javax.security.auth.callback.PasswordCallback)13 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)11 Callback (javax.security.auth.callback.Callback)10 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)7 IOException (java.io.IOException)3 CallbackHandler (javax.security.auth.callback.CallbackHandler)3 RealmChoiceCallback (javax.security.sasl.RealmChoiceCallback)3 Test (org.junit.Test)3 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 ConsoleHandler (java.util.logging.ConsoleHandler)1 Handler (java.util.logging.Handler)1 LoginException (javax.security.auth.login.LoginException)1 SaslException (javax.security.sasl.SaslException)1 SaslServer (javax.security.sasl.SaslServer)1