protected WSSecKerberosToken getKerberosTokenBuilder(RampartMessageData rmd, Token token)
throws RampartException {
RampartPolicyData rpd = rmd.getPolicyData();
KerberosConfig krbConfig = rpd.getRampartConfig().getKerberosConfig();
if (krbConfig == null || krbConfig.getProp() == null) {
throw new RampartException("noKerberosConfigDefined");
}
WSSecKerberosToken krb = new WSSecKerberosToken();
krb.setWsConfig(rmd.getConfig());
log.debug("Token inclusion: " + token.getInclusion());
RampartUtil.setKeyIdentifierType(rmd, krb, token);
String user = null;
String passwordFromConfig = null;
String clientPricipal = null;
String servicePrincipal = null;
String password = null;
String service = null;
clientPricipal = (String) rmd.getMsgContext().getProperty(
KerberosConfig.CLIENT_PRINCIPLE_NAME);
servicePrincipal = (String) rmd.getMsgContext().getProperty(
KerberosConfig.SERVICE_PRINCIPLE_NAME);
if (clientPricipal == null || servicePrincipal == null) {
// Get the user from kerberos configuration
user = krbConfig.getProp().getProperty(KerberosConfig.CLIENT_PRINCIPLE_NAME);
passwordFromConfig = krbConfig.getProp().getProperty(
KerberosConfig.CLIENT_PRINCIPLE_PASSWORD);
if (passwordFromConfig == null) {
passwordFromConfig = krbConfig.getProp().getProperty(
KerberosConfig.SERVICE_PRINCIPLE_PASSWORD);
}
// If kerberos user is not present, use user property as Alias
if (user == null) {
user = rpd.getRampartConfig().getUser();
}
if (user != null && !"".equals(user)) {
log.debug("User : " + user);
// Get the password
CallbackHandler handler = RampartUtil.getPasswordCB(rmd);
if (handler != null) {
WSPasswordCallback[] cb = { new WSPasswordCallback(user,
WSPasswordCallback.KERBEROS_TOKEN) };
try {
handler.handle(cb);
if (cb[0].getPassword() != null && !"".equals(cb[0].getPassword())) {
password = cb[0].getPassword();
log.debug("Password : " + password);
} else {
password = passwordFromConfig;
}
} catch (IOException e) {
throw new RampartException("errorInGettingPasswordForUser",
new String[] { user }, e);
} catch (UnsupportedCallbackException e) {
throw new RampartException("errorInGettingPasswordForUser",
new String[] { user }, e);
}
} else {
password = passwordFromConfig;
}
}
service = krbConfig.getProp().getProperty(KerberosConfig.SERVICE_PRINCIPLE_NAME);
} else {
user = clientPricipal;
service = servicePrincipal;
}