use of javax.naming.ldap.ExtendedResponse in project ldapchai by ldapchai.
the class NmasResponseSet method write.
boolean write() throws ChaiUnavailableException, ChaiOperationException {
if (this.state != STATE.NEW) {
throw new IllegalStateException("RepsonseSet not suitable for writing (not in NEW state)");
}
// write challenge set questions to Nmas Login Config
try {
final PutLoginConfigRequest request = new PutLoginConfigRequest();
request.setObjectDN(user.getEntryDN());
final byte[] data = csToNmasXML(getChallengeSet(), this.csIdentifier).getBytes("UTF8");
request.setData(data);
request.setDataLen(data.length);
request.setTag("ChallengeResponseQuestions");
request.setMethodID(NMASChallengeResponse.METHOD_ID);
request.setMethodIDLen(NMASChallengeResponse.METHOD_ID.length * 4);
final ExtendedResponse response = user.getChaiProvider().extendedOperation(request);
if (response != null && ((PutLoginConfigResponse) response).getNmasRetCode() != 0) {
LOGGER.debug("nmas error writing question: " + ((PutLoginConfigResponse) response).getNmasRetCode());
return false;
}
} catch (UnsupportedEncodingException e) {
LOGGER.error("error while writing nmas questions: " + e.getMessage());
return false;
} catch (ChaiOperationException e) {
LOGGER.error("error while writing nmas questions: " + e.getMessage());
throw e;
} catch (ChaiValidationException e) {
LOGGER.error("error while writing nmas questions: " + e.getMessage());
throw ChaiOperationException.forErrorMessage(e.getMessage());
}
boolean success = true;
// write responses
for (final Map.Entry<Challenge, Answer> entry : crMap.entrySet()) {
final Challenge loopChallenge = entry.getKey();
try {
final byte[] data = ((NmasAnswer) entry.getValue()).getAnswerText().getBytes("UTF8");
final PutLoginSecretRequest request = new PutLoginSecretRequest();
request.setObjectDN(user.getEntryDN());
request.setData(data);
request.setDataLen(data.length);
request.setTag(loopChallenge.getChallengeText());
request.setMethodID(NMASChallengeResponse.METHOD_ID);
request.setMethodIDLen(NMASChallengeResponse.METHOD_ID.length * 4);
final ExtendedResponse response = user.getChaiProvider().extendedOperation(request);
if (response != null && ((PutLoginSecretResponse) response).getNmasRetCode() != 0) {
LOGGER.debug("nmas error writing answer: " + ((PutLoginSecretResponse) response).getNmasRetCode());
success = false;
}
} catch (Exception e) {
LOGGER.error("error while writing nmas answer: " + e.getMessage());
}
}
if (success) {
LOGGER.info("successfully wrote NMAS challenge/response set for user " + user.getEntryDN());
this.state = STATE.WRITTEN;
}
return success;
}
use of javax.naming.ldap.ExtendedResponse in project ldapchai by ldapchai.
the class InetOrgPersonImpl method setPassword.
public void setPassword(final String newPassword, final boolean enforcePasswordPolicy) throws ChaiUnavailableException, ChaiPasswordPolicyException {
final boolean useNmasSetting = this.getChaiProvider().getChaiConfiguration().getBooleanSetting(ChaiSetting.EDIRECTORY_ENABLE_NMAS);
if (!useNmasSetting) {
try {
writeStringAttribute(ATTR_PASSWORD, newPassword);
} catch (ChaiOperationException e) {
throw new ChaiPasswordPolicyException(e.getMessage(), ChaiErrors.getErrorForMessage(e.getMessage()));
}
} else {
final SetPwdRequest request = new SetPwdRequest();
request.setData(newPassword);
request.setObjectDN(this.getEntryDN());
final ExtendedResponse response;
try {
response = getChaiProvider().extendedOperation(request);
} catch (ChaiOperationException e) {
throw new ChaiPasswordPolicyException(e.getMessage(), ChaiErrors.getErrorForMessage(e.getMessage()));
}
if (response != null) {
final SetPwdResponse setResponse = (SetPwdResponse) response;
final int responseCode = setResponse.getNmasRetCode();
if (responseCode != 0) {
LOGGER.debug("error setting nmas password: " + responseCode);
final String errorString = "nmas error " + responseCode;
throw new ChaiPasswordPolicyException(errorString, ChaiErrors.getErrorForMessage(errorString));
}
}
}
}
use of javax.naming.ldap.ExtendedResponse in project directory-ldap-api by apache.
the class JavaStoredProcUtils method callStoredProcedure.
/**
* Invoke a Stored Procedure
*
* @param ctx The execution context
* @param procedureName The procedure to execute
* @param arguments The procedure's arguments
* @return The execution resut
* @throws NamingException If we have had an error whil executing the stored procedure
*/
public static Object callStoredProcedure(LdapContext ctx, String procedureName, Object[] arguments) throws NamingException {
String language = "Java";
Object responseObject;
try {
/**
* Create a new stored procedure execution request.
*/
StoredProcedureRequestImpl req = new StoredProcedureRequestImpl(0, procedureName, language);
/**
* For each argument UTF-8-encode the type name
* and Java-serialize the value
* and add them to the request as a parameter object.
*/
for (int i = 0; i < arguments.length; i++) {
byte[] type;
byte[] value;
type = arguments[i].getClass().getName().getBytes("UTF-8");
value = SerializationUtils.serialize((Serializable) arguments[i]);
req.addParameter(type, value);
}
/**
* Call the stored procedure via the extended operation
* and get back its return value.
*/
ExtendedRequest jndiReq = LdapApiServiceFactory.getSingleton().toJndi(req);
ExtendedResponse resp = ctx.extendedOperation(jndiReq);
/**
* Restore a Java object from the return value.
*/
byte[] responseStream = resp.getEncodedValue();
responseObject = SerializationUtils.deserialize(responseStream);
} catch (Exception e) {
NamingException ne = new NamingException();
ne.setRootCause(e);
throw ne;
}
return responseObject;
}
Aggregations