use of org.apache.directory.api.ldap.model.message.BindRequestImpl in project directory-ldap-api by apache.
the class InitBindRequest method action.
/**
* {@inheritDoc}
*/
public void action(LdapMessageContainer<BindRequestDecorator> container) throws DecoderException {
// Create the BindRequest LdapMessage instance and store it in the container
BindRequest internalBindRequest = new BindRequestImpl();
internalBindRequest.setMessageId(container.getMessageId());
BindRequestDecorator bindRequest = new BindRequestDecorator(container.getLdapCodecService(), internalBindRequest);
container.setMessage(bindRequest);
// We will check that the request is not null
TLV tlv = container.getCurrentTLV();
if (tlv.getLength() == 0) {
String msg = I18n.err(I18n.ERR_04077);
LOG.error(msg);
// This will generate a PROTOCOL_ERROR
throw new DecoderException(msg);
}
}
use of org.apache.directory.api.ldap.model.message.BindRequestImpl in project directory-fortress-core by apache.
the class LdapDataProvider method bind.
/**
* Calls the PoolMgr to perform an LDAP bind for a user/password combination. This function is valid
* if and only if the user entity is a member of the USERS data set.
*
* @param connection connection to ldap server.
* @param szUserDn contains the LDAP dn to the user entry in String format.
* @param password contains the password in clear text.
* @return bindResponse contains the result of the operation.
* @throws LdapException in the event of LDAP error.
*/
protected BindResponse bind(LdapConnection connection, String szUserDn, String password) throws LdapException {
COUNTERS.incrementBind();
Dn userDn = new Dn(szUserDn);
BindRequest bindReq = new BindRequestImpl();
bindReq.setDn(userDn);
bindReq.setCredentials(password);
bindReq.addControl(PP_REQ_CTRL);
return connection.bind(bindReq);
}
use of org.apache.directory.api.ldap.model.message.BindRequestImpl in project directory-ldap-api by apache.
the class LdapNetworkConnection method bindSasl.
/**
* Process the SASL Bind. It's a dialog with the server, we will send a first BindRequest, receive
* a response and the, if this response is a challenge, continue by sending a new BindRequest with
* the requested informations.
*
* @param saslRequest The SASL request object containing all the needed parameters
* @return A {@link BindResponse} containing the result
* @throws LdapException if some error occurred
*/
public BindFuture bindSasl(SaslRequest saslRequest) throws LdapException {
// First switch to anonymous state
authenticated.set(false);
// try to connect, if we aren't already connected.
connect();
// If the session has not been establish, or is closed, we get out immediately
checkSession();
BindRequest bindRequest = createBindRequest((String) null, null, saslRequest.getSaslMechanism(), saslRequest.getControls());
// Update the messageId
int newId = messageId.incrementAndGet();
bindRequest.setMessageId(newId);
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03205_SENDING_REQUEST, bindRequest));
}
// Create a future for this Bind operation
BindFuture bindFuture = new BindFuture(this, newId);
// Store it in the future Map
addToFutureMap(newId, bindFuture);
try {
BindResponse bindResponse;
byte[] response;
ResultCodeEnum result;
// Creating a map for SASL properties
Map<String, Object> properties = new HashMap<>();
// Quality of Protection SASL property
if (saslRequest.getQualityOfProtection() != null) {
properties.put(Sasl.QOP, saslRequest.getQualityOfProtection().getValue());
}
// Security Strength SASL property
if (saslRequest.getSecurityStrength() != null) {
properties.put(Sasl.STRENGTH, saslRequest.getSecurityStrength().getValue());
}
// Mutual Authentication SASL property
if (saslRequest.isMutualAuthentication()) {
properties.put(Sasl.SERVER_AUTH, "true");
}
// Creating a SASL Client
SaslClient sc = Sasl.createSaslClient(new String[] { bindRequest.getSaslMechanism() }, saslRequest.getAuthorizationId(), "ldap", config.getLdapHost(), properties, new SaslCallbackHandler(saslRequest));
// for the requested mechanism. We then produce an Exception
if (sc == null) {
String message = "Cannot find a SASL factory for the " + bindRequest.getSaslMechanism() + " mechanism";
LOG.error(message);
throw new LdapException(message);
}
// deal with it immediately.
if (sc.hasInitialResponse()) {
byte[] challengeResponse = sc.evaluateChallenge(Strings.EMPTY_BYTES);
// Stores the challenge's response, and send it to the server
bindRequest.setCredentials(challengeResponse);
writeRequest(bindRequest);
// Get the server's response, blocking
bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
if (bindResponse == null) {
// We didn't received anything : this is an error
if (LOG.isErrorEnabled()) {
LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
}
throw new LdapException(TIME_OUT_ERROR);
}
result = bindResponse.getLdapResult().getResultCode();
} else {
// Copy the bindRequest without setting the credentials
BindRequest bindRequestCopy = new BindRequestImpl();
bindRequestCopy.setMessageId(newId);
bindRequestCopy.setName(bindRequest.getName());
bindRequestCopy.setSaslMechanism(bindRequest.getSaslMechanism());
bindRequestCopy.setSimple(bindRequest.isSimple());
bindRequestCopy.setVersion3(bindRequest.getVersion3());
bindRequestCopy.addAllControls(bindRequest.getControls().values().toArray(new Control[0]));
writeRequest(bindRequestCopy);
bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
if (bindResponse == null) {
// We didn't received anything : this is an error
if (LOG.isErrorEnabled()) {
LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
}
throw new LdapException(TIME_OUT_ERROR);
}
result = bindResponse.getLdapResult().getResultCode();
}
while (!sc.isComplete() && ((result == ResultCodeEnum.SASL_BIND_IN_PROGRESS) || (result == ResultCodeEnum.SUCCESS))) {
response = sc.evaluateChallenge(bindResponse.getServerSaslCreds());
if (result == ResultCodeEnum.SUCCESS) {
if (response != null) {
throw new LdapException("protocol error");
}
} else {
newId = messageId.incrementAndGet();
bindRequest.setMessageId(newId);
bindRequest.setCredentials(response);
addToFutureMap(newId, bindFuture);
writeRequest(bindRequest);
bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
if (bindResponse == null) {
// We didn't received anything : this is an error
if (LOG.isErrorEnabled()) {
LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
}
throw new LdapException(TIME_OUT_ERROR);
}
result = bindResponse.getLdapResult().getResultCode();
}
}
bindFuture.set(bindResponse);
return bindFuture;
} catch (LdapException e) {
throw e;
} catch (Exception e) {
LOG.error(e.getMessage());
throw new LdapException(e);
}
}
use of org.apache.directory.api.ldap.model.message.BindRequestImpl in project directory-ldap-api by apache.
the class ApiLdapModelOsgiTest method useBundleClasses.
@Override
protected void useBundleClasses() throws Exception {
// uses FastDnParser
new Dn("dc=example,dc=com");
// uses ComplexDnparser (antlr based)
new Dn("cn=a+sn=b,dc=example,dc=com");
new Value("foo");
new DefaultAttribute("cn");
new DefaultEntry();
AttributeUtils.toJndiAttribute(new DefaultAttribute("cn"));
new BindRequestImpl();
new EqualityNode<String>("cn", "foo");
new LdapUrl("ldap://ldap.example.com:10389/dc=example,dc=com?objectclass");
new ObjectClassDescriptionSchemaParser().parse("( 2.5.6.0 NAME 'top' DESC 'top of the superclass chain' ABSTRACT MUST objectClass )");
SchemaObject schemaObject = new LdapSyntax("1.2.3");
new Registries().getGlobalOidRegistry().register(schemaObject);
new Registries().getLoadedSchemas();
}
use of org.apache.directory.api.ldap.model.message.BindRequestImpl in project directory-ldap-api by apache.
the class Dsmlv2Engine method bind.
/**
* Binds to the ldap server
*
* @param messageId the message Id
* @throws LdapException If we had an issue while binding
* @throws EncoderException If we had an issue while encoding the request
* @throws DecoderException If we had an issue while decoding the request
* @throws IOException If we had an issue while transmitting the request or re ceiving the response
*/
protected void bind(int messageId) throws LdapException, EncoderException, DecoderException, IOException {
if ((connection != null) && connection.isAuthenticated()) {
return;
}
if (connection == null) {
throw new IOException(I18n.err(I18n.ERR_03101_MISSING_CONNECTION_TO));
}
BindRequest bindRequest = new BindRequestImpl();
bindRequest.setSimple(true);
bindRequest.setCredentials(Strings.getBytesUtf8(password));
bindRequest.setName(user);
bindRequest.setVersion3(true);
bindRequest.setMessageId(messageId);
BindResponse bindResponse = connection.bind(bindRequest);
if (bindResponse.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS) {
LOG.warn("Error : {}", bindResponse.getLdapResult().getDiagnosticMessage());
}
}
Aggregations