javax.security.sasl.RealmCallback Java Examples

The following examples show how to use javax.security.sasl.RealmCallback. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AuthCallbackHandler.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(this.userName);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(this.password.toCharArray());
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }

}
 
Example #2
Source File: DefaultCallbackHandler.java    From Bats with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example #3
Source File: Authentication.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private CallbackHandler createCallBackHandler(){
    return new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback current : callbacks) {
                if (current instanceof NameCallback) {
                    NameCallback nameCallback = (NameCallback) current;
                    nameCallback.setName(username);
                } else if (current instanceof PasswordCallback) {
                    PasswordCallback pwdCallback = (PasswordCallback) current;
                    pwdCallback.setPassword(password);
                } else if (current instanceof RealmCallback) {
                    RealmCallback realmCallback = (RealmCallback) current;
                    realmCallback.setText(realmCallback.getDefaultText());
                } else {
                    throw new UnsupportedCallbackException(current);
                }
            }
        }
    };
}
 
Example #4
Source File: KerberosHiveMetastoreAuthentication.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Callback[] callbacks)
{
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            ((NameCallback) callback).setName(username);
        }
        if (callback instanceof PasswordCallback) {
            ((PasswordCallback) callback).setPassword(password.toCharArray());
        }
        if (callback instanceof RealmCallback) {
            RealmCallback realmCallback = (RealmCallback) callback;
            realmCallback.setText(realmCallback.getDefaultText());
        }
    }
}
 
Example #5
Source File: BaseLdapSuiteAuthenticationReferralsTest.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testVerifyGoodPasswordReferral() throws Exception {
    AuthorizingCallbackHandler cbh = securityRealm.getAuthorizingCallbackHandler(AuthMechanism.PLAIN);

    NameCallback ncb = new NameCallback("Username", USER_THREE_REFERRAL);
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence(USER_THREE_PASSWORD.toCharArray()));

    cbh.handle(new Callback[] { ncb, rcb, evc });

    assertTrue("Password Verified", evc.isVerified());

    Collection<Principal> principals = Collections.emptyList();
    SubjectUserInfo userInfo = cbh.createSubjectUserInfo(principals);
    principals = userInfo.getPrincipals();
    assertEquals("Principal Count", 1, principals.size());
    RealmUser user = (RealmUser) principals.iterator().next();
    assertEquals("Expected Username", "user_three", user.getName());
}
 
Example #6
Source File: LdapAuthenticationSuiteTest.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testVerifyGoodPassword_UserTwo() throws Exception {
    /*
     * Essentially a duplicate of the previous test but we want to be sure this works as we later
     * test that this user can be excluded using an advanced filter.
     */
    AuthorizingCallbackHandler cbh = securityRealm.getAuthorizingCallbackHandler(AuthMechanism.PLAIN);

    NameCallback ncb = new NameCallback("Username", USER_TWO);
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence(USER_TWO_PASSWORD.toCharArray()));

    cbh.handle(new Callback[] { ncb, rcb, evc });

    assertTrue("Password Verified", evc.isVerified());
}
 
Example #7
Source File: SecretIdentityService.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            String defaultText = rcb.getDefaultText();
            rcb.setText(defaultText); // For now just use the realm suggested.
        } else if (current instanceof RealmChoiceCallback) {
            throw DomainManagementLogger.ROOT_LOGGER.realmNotSupported(current);
        } else if (current instanceof OptionalNameCallback) {
            // Do nothing as we don't want to set a name for local authentication.
        } else if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(userName);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password);
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example #8
Source File: Authentication.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(username);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password.toCharArray());
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(realm != null ? realm : rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example #9
Source File: RbacAdminCallbackHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(userName);
            System.out.println("set user " + userName);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password.toCharArray());
            System.out.println("set password " + password);
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example #10
Source File: Authentication.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(username);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password.toCharArray());
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example #11
Source File: Authentication.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(username);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password.toCharArray());
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example #12
Source File: SaslTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private CallbackHandler serverCallbackHandler(String username, String realm, String password) {
    return callbacks -> {
        for (Callback callback : callbacks) {
            if (callback instanceof NameCallback) {
                Assert.assertEquals(username, ((NameCallback) callback).getDefaultName());
            } else if (callback instanceof RealmCallback) {
                Assert.assertEquals(realm, ((RealmCallback) callback).getDefaultText());
            } else if (callback instanceof PasswordCallback) {
                ((PasswordCallback) callback).setPassword(password.toCharArray());
            } else if (callback instanceof AuthorizeCallback) {
                ((AuthorizeCallback) callback).setAuthorized(((AuthorizeCallback) callback).getAuthorizationID().equals(((AuthorizeCallback) callback).getAuthenticationID()));
            } else {
                throw new UnsupportedCallbackException(callback);
            }
        }
    };
}
 
Example #13
Source File: SaslCallbackHandler.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof AuthorizeCallback) {
            AuthorizeCallback authorizeCallback = (AuthorizeCallback) callback;
            String authenticationId = authorizeCallback.getAuthenticationID();
            String authorizationId = authorizeCallback.getAuthorizationID();
            authorizeCallback.setAuthorized(authenticationId.equals(authorizationId));
        } else if (callback instanceof NameCallback) {
            ((NameCallback) callback).setName("glowroot");
        } else if (callback instanceof PasswordCallback) {
            ((PasswordCallback) callback).setPassword(password);
        } else if (callback instanceof RealmCallback) {
            ((RealmCallback) callback).setText("glowroot");
        }
    }
}
 
Example #14
Source File: TestHBaseSaslRpcClient.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testDigestSaslClientCallbackHandler() throws UnsupportedCallbackException {
  final Token<? extends TokenIdentifier> token = createTokenMock();
  when(token.getIdentifier()).thenReturn(Bytes.toBytes(DEFAULT_USER_NAME));
  when(token.getPassword()).thenReturn(Bytes.toBytes(DEFAULT_USER_PASSWORD));

  final NameCallback nameCallback = mock(NameCallback.class);
  final PasswordCallback passwordCallback = mock(PasswordCallback.class);
  final RealmCallback realmCallback = mock(RealmCallback.class);

  // We can provide a realmCallback, but HBase presently does nothing with it.
  Callback[] callbackArray = {nameCallback, passwordCallback, realmCallback};
  final DigestSaslClientCallbackHandler saslClCallbackHandler =
      new DigestSaslClientCallbackHandler(token);
  saslClCallbackHandler.handle(callbackArray);
  verify(nameCallback).setName(anyString());
  verify(passwordCallback).setPassword(any());
}
 
Example #15
Source File: AbstractLogin.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            nc.setName(nc.getDefaultName());
        } else if (callback instanceof PasswordCallback) {
            String errorMessage = "Could not login: the client is being asked for a password, but this " +
                    " module does not currently support obtaining a password from the user.";
            throw new UnsupportedCallbackException(callback, errorMessage);
        } else if (callback instanceof RealmCallback) {
            RealmCallback rc = (RealmCallback) callback;
            rc.setText(rc.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(callback, "Unrecognized Login callback");
        }
    }
}
 
Example #16
Source File: LdapAuthenticationSuiteTest.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testVerifyGoodPasswordUserNameWithBackSlash() throws Exception {
    AuthorizingCallbackHandler cbh = securityRealm.getAuthorizingCallbackHandler(AuthMechanism.PLAIN);
    NameCallback ncb = new NameCallback("Username", "User\\Name");
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence("user_\\password".toCharArray()));
    cbh.handle(new Callback[] { ncb, rcb, evc });
    assertTrue("Password Verified", evc.isVerified());
}
 
Example #17
Source File: LdapAuthenticationSuiteTest.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testVerifyGoodPasswordUserNameWithSlash() throws Exception {
    AuthorizingCallbackHandler cbh = securityRealm.getAuthorizingCallbackHandler(AuthMechanism.PLAIN);
    NameCallback ncb = new NameCallback("Username", "User/Name");
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence("user_/password".toCharArray()));
    cbh.handle(new Callback[] { ncb, rcb, evc });
    assertTrue("Password Verified", evc.isVerified());
}
 
Example #18
Source File: BaseLdapSuiteAuthenticationReferralsTest.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testVerifyBadPasswordReferral() throws Exception {
    AuthorizingCallbackHandler cbh = securityRealm.getAuthorizingCallbackHandler(AuthMechanism.PLAIN);

    NameCallback ncb = new NameCallback("Username", USER_THREE_REFERRAL);
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence("BAD".toCharArray()));

    cbh.handle(new Callback[] { ncb, rcb, evc });

    assertFalse("Password Not Verified", evc.isVerified());
}
 
Example #19
Source File: LdapAuthenticationSuiteTest.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testVerifyFilteredOutUser() throws Exception {
    AuthorizingCallbackHandler cbh = getAdvancedCallbackHandler();

    NameCallback ncb = new NameCallback("Username", USER_TWO);
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence(USER_TWO_PASSWORD.toCharArray()));

    try {
        cbh.handle(new Callback[] { ncb, rcb, evc });
        fail("Expected exception not thrown");
    } catch (IOException e) {
    }
}
 
Example #20
Source File: ClientCallbackHandler.java    From jstorm with Apache License 2.0 5 votes vote down vote up
/**
 * This method is invoked by SASL for authentication challenges
 * 
 * @param callbacks a collection of challenge callbacks
 */
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback c : callbacks) {
        if (c instanceof NameCallback) {
            LOG.debug("name callback");
            NameCallback nc = (NameCallback) c;
            nc.setName(_username);
        } else if (c instanceof PasswordCallback) {
            LOG.debug("password callback");
            PasswordCallback pc = (PasswordCallback) c;
            if (_password != null) {
                pc.setPassword(_password.toCharArray());
            }
        } else if (c instanceof AuthorizeCallback) {
            LOG.debug("authorization callback");
            AuthorizeCallback ac = (AuthorizeCallback) c;
            String authid = ac.getAuthenticationID();
            String authzid = ac.getAuthorizationID();
            if (authid.equals(authzid)) {
                ac.setAuthorized(true);
            } else {
                ac.setAuthorized(false);
            }
            if (ac.isAuthorized()) {
                ac.setAuthorizedID(authzid);
            }
        } else if (c instanceof RealmCallback) {
            RealmCallback rc = (RealmCallback) c;
            ((RealmCallback) c).setText(rc.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(c);
        }
    }
}
 
Example #21
Source File: SaslTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private CallbackHandler clientCallbackHandler(String username, String realm, String password) throws Exception {
    return callbacks -> {
        for (Callback callback : callbacks) {
            if (callback instanceof NameCallback) {
                ((NameCallback) callback).setName(username);
            } else if (callback instanceof RealmCallback) {
                ((RealmCallback) callback).setText(realm);
            } else if (callback instanceof PasswordCallback) {
                ((PasswordCallback) callback).setPassword(password.toCharArray());
            } else if (callback instanceof CredentialCallback && ClearPassword.ALGORITHM_CLEAR.equals(((CredentialCallback) callback).getAlgorithm())) {
                try {
                    PasswordFactory factory = PasswordFactory.getInstance(ClearPassword.ALGORITHM_CLEAR);
                    Password pass = factory.generatePassword(new ClearPasswordSpec(password.toCharArray()));

                    ((CredentialCallback) callback).setCredential(new PasswordCredential(pass));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else if (callback instanceof ChannelBindingCallback) {
                ((ChannelBindingCallback) callback).setBindingType("type");
                ((ChannelBindingCallback) callback).setBindingData(new byte[]{0x12, 0x34});
            } else {
                throw new UnsupportedCallbackException(callback);
            }
        }
    };
}
 
Example #22
Source File: PropertiesAuthenticationDigestedTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testObtainDigest() throws Exception {
    AuthorizingCallbackHandler cbh = securityRealm.getAuthorizingCallbackHandler(AuthMechanism.DIGEST);

    NameCallback ncb = new NameCallback("Username", TEST_USERNAME);
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    CredentialCallback cc = new CredentialCallback(PasswordCredential.class, ALGORITHM_DIGEST_MD5);

    cbh.handle(new Callback[] { ncb, rcb, cc });

    UsernamePasswordHashUtil uph = new UsernamePasswordHashUtil();
    byte[] expected = uph.generateHashedURP(TEST_USERNAME, TEST_REALM, TEST_PASSWORD.toCharArray());

    assertArrayEquals("Expected hash", expected, ((DigestPassword) ((PasswordCredential) cc.getCredential()).getPassword()).getDigest());
}
 
Example #23
Source File: PropertiesAuthenticationDigestedTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testVerifyGoodPassword() throws Exception {
    AuthorizingCallbackHandler cbh = securityRealm.getAuthorizingCallbackHandler(AuthMechanism.DIGEST);

    NameCallback ncb = new NameCallback("Username", TEST_USERNAME);
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence(TEST_PASSWORD.toCharArray()));

    cbh.handle(new Callback[] { ncb, rcb, evc });

    assertTrue("Password Verified", evc.isVerified());
}
 
Example #24
Source File: PropertiesAuthenticationDigestedTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testVerifyBadPassword() throws Exception {
    AuthorizingCallbackHandler cbh = securityRealm.getAuthorizingCallbackHandler(AuthMechanism.DIGEST);

    NameCallback ncb = new NameCallback("Username", TEST_USERNAME);
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence("BAD PASSWORD".toCharArray()));

    cbh.handle(new Callback[] { ncb, rcb, evc });

    assertFalse("Password Not Verified", evc.isVerified());
}
 
Example #25
Source File: SaslNettyClient.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws
    UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            nc.setName(nc.getDefaultName());
        } else {
            if (callback instanceof PasswordCallback) {
                PasswordCallback pc = (PasswordCallback) callback;
                if (password != null) {
                    pc.setPassword(this.password.toCharArray());
                }
            } else {
                if (callback instanceof RealmCallback) {
                    RealmCallback rc = (RealmCallback) callback;
                    rc.setText(rc.getDefaultText());
                } else {
                    if (callback instanceof AuthorizeCallback) {
                        AuthorizeCallback ac = (AuthorizeCallback) callback;
                        String authid = ac.getAuthenticationID();
                        String authzid = ac.getAuthorizationID();
                        if (authid.equals(authzid)) {
                            ac.setAuthorized(true);
                        } else {
                            ac.setAuthorized(false);
                        }
                        if (ac.isAuthorized()) {
                            ac.setAuthorizedID(authzid);
                        }
                    } else {
                        throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
                    }
                }
            }
        }
    }
}
 
Example #26
Source File: SaslNettyServer.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            handleNameCallback((NameCallback) callback);
        } else if (callback instanceof PasswordCallback) {
            handlePasswordCallback((PasswordCallback) callback);
        } else if (callback instanceof RealmCallback) {
            handleRealmCallback((RealmCallback) callback);
        } else if (callback instanceof AuthorizeCallback) {
            handleAuthorizeCallback((AuthorizeCallback) callback);
        }
    }
}
 
Example #27
Source File: SaslNettyServer.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws
    UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            nc.setName(nc.getDefaultName());
        } else {
            if (callback instanceof PasswordCallback) {
                PasswordCallback pc = (PasswordCallback) callback;
                if (password != null) {
                    pc.setPassword(this.password.toCharArray());
                }
            } else {
                if (callback instanceof RealmCallback) {
                    RealmCallback rc = (RealmCallback) callback;
                    rc.setText(rc.getDefaultText());
                } else {
                    if (callback instanceof AuthorizeCallback) {
                        AuthorizeCallback ac = (AuthorizeCallback) callback;
                        String authid = ac.getAuthenticationID();
                        String authzid = ac.getAuthorizationID();
                        if (authid.equals(authzid)) {
                            ac.setAuthorized(true);
                        } else {
                            ac.setAuthorized(false);
                        }
                        if (ac.isAuthorized()) {
                            ac.setAuthorizedID(authzid);
                        }
                    } else {
                        throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
                    }
                }
            }
        }
    }
}
 
Example #28
Source File: ModelControllerClientFactory.java    From hawkular-agent with Apache License 2.0 5 votes vote down vote up
protected ModelControllerClient createClient(final MonitoredEndpoint endpoint) {
    final ConnectionData cnData = endpoint.getConnectionData();
    final CallbackHandler callbackHandler = new CallbackHandler() {
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback current : callbacks) {
                if (current instanceof NameCallback) {
                    NameCallback ncb = (NameCallback) current;
                    ncb.setName(cnData.getUsername());
                } else if (current instanceof PasswordCallback) {
                    PasswordCallback pcb = (PasswordCallback) current;
                    pcb.setPassword(cnData.getPassword().toCharArray());
                } else if (current instanceof RealmCallback) {
                    RealmCallback rcb = (RealmCallback) current;
                    rcb.setText(rcb.getDefaultText());
                } else {
                    throw new UnsupportedCallbackException(current);
                }
            }
        }
    };
    final URI uri = cnData.getUri();
    try {
        ModelControllerClientConfiguration config = new ModelControllerClientConfiguration.Builder()
                .setProtocol(uri.getScheme())
                .setHostName(uri.getHost())
                .setPort(uri.getPort())
                .setSslContext(endpoint.getSSLContext())
                .setHandler(callbackHandler)
                .build();

        return ModelControllerClient.Factory.create(config);
    } catch (Exception e) {
        throw new RuntimeException("Failed to create management client", e);
    }
}
 
Example #29
Source File: AbstractITest.java    From hawkular-agent with Apache License 2.0 5 votes vote down vote up
protected static ModelControllerClient newModelControllerClient(final String host, final int managementPort) {
    final CallbackHandler callbackHandler = new CallbackHandler() {
        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback current : callbacks) {
                if (current instanceof NameCallback) {
                    NameCallback ncb = (NameCallback) current;
                    log.fine("ModelControllerClient is sending a username [" + managementUser + "]");
                    ncb.setName(managementUser);
                } else if (current instanceof PasswordCallback) {
                    PasswordCallback pcb = (PasswordCallback) current;
                    log.fine("ModelControllerClient is sending a password [" + managementPasword + "]");
                    pcb.setPassword(managementPasword.toCharArray());
                } else if (current instanceof RealmCallback) {
                    RealmCallback rcb = (RealmCallback) current;
                    log.fine("ModelControllerClient is sending a realm [" + rcb.getDefaultText() + "]");
                    rcb.setText(rcb.getDefaultText());
                } else {
                    throw new UnsupportedCallbackException(current);
                }
            }
        }
    };

    try {
        InetAddress inetAddr = InetAddress.getByName(host);
        log.fine("Connecting a ModelControllerClient to [" + host + ":" + managementPort + "]");
        return ModelControllerClient.Factory.create(inetAddr, managementPort, callbackHandler);
    } catch (Exception e) {
        throw new RuntimeException("Failed to create management client", e);
    }
}
 
Example #30
Source File: LdapAuthenticationSuiteTest.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testVerifyGoodPassword_Advanced() throws Exception {
    AuthorizingCallbackHandler cbh = getAdvancedCallbackHandler();

    NameCallback ncb = new NameCallback("Username", USER_ONE);
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence(USER_ONE_PASSWORD.toCharArray()));

    cbh.handle(new Callback[] { ncb, rcb, evc });

    assertTrue("Password Verified", evc.isVerified());
}