Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LDAP Connector - Initial Commit #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions ldap/ldap-connector/ldap-connector-1.0.0/Readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Product: Integration tests for WSO2 ESB LDAP connector

Pre-requisites:

- Maven 3.x
- Java 1.6 or above
- org.wso2.esb.integration.integration-base is rquired. this test suite has been configred to download this automatically. however if its fail download following project and compile using mvn clean install command to update your local repository.
https://github.com/wso2-dev/esb-connectors/tree/master/integration-base

Tested Platform:

- Mac OSX 10.9.2
- WSO2 ESB 4.8.1

STEPS:

1. Make sure the ESB 4.8.1 zip file with latest patches available at "{PATH_TO_SOURCE_BUNDLE}/ldap-connector/ldap-connector-1.0.0/org.wso2.carbon.connector/repository/"

2. Integration Tests uses Embedded in-memory LDAP server in tests. So normally connector doesn't need an external LDAP server to run its tests. If you want to test the Connector with your LDAP server, do necessary changes to LDAP properties file at location "{PATH_TO_SOURCE_BUNDLE}/ldap-connector/ldap-connector-1.0.0/org.wso2.carbon.connector/src/test/resources/artifacts/ESB/connector/config".

providerUrl - URL of you LDAP server
securityPrincipal - Root user DN
securityCredentials - Root user password
ldapUserBase - User Base of the LDAP server
testUserId - Sample test user id
baseDN - Base DN of the LDAP server
ldapPort - Port which Embedded LDAP server should be started. (Default 10389)
useEmbeddedLDAP - Use embedded LDAP server or outside ldap sever. If you want to use your LDAP server to test with the Connector, make this value - false

3. Navigate to "{PATH_TO_SOURCE_BUNDLE}/ldap-connector/ldap-connector-1.0.0/org.wso2.carbon.connector/" and run the following command.
$ mvn clean install

NOTE :
If you are using Embedded LDAP mode in Integration Testing, please make sure that ldapPort you are assigning in config file is not used by any other application in your local machine.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/*
target/*

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<formats>
<format>zip</format>
</formats>

<includeBaseDirectory>false</includeBaseDirectory>

<fileSets>
<fileSet>
<directory>target/connector/dependencies</directory>
<outputDirectory>lib</outputDirectory>
</fileSet>
<fileSet>
<directory>target/classes</directory>
<outputDirectory></outputDirectory>
<excludes>
<exclude>**/metrics_module.xml</exclude>
<exclude>**/META-INF/*</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
product.name=WSO2 ESB
product.version=4.8.0-SNAPSHOT

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.connector.ldap;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseException;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.transport.nhttp.NhttpConstants;
import org.wso2.carbon.connector.core.AbstractConnector;
import org.wso2.carbon.connector.core.ConnectException;

public class AddEntry extends AbstractConnector {
public static final String OBJECT_CLASS = "objectClass";
public static final String ATTRIBUTES = "attributes";
public static final String DN = "dn";

@Override
public void connect(MessageContext messageContext) throws ConnectException {
String objectClass = (String)getParameter(messageContext, OBJECT_CLASS);
String attributesString = (String)getParameter(messageContext, ATTRIBUTES);
String dn = (String)getParameter(messageContext, DN);

OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace ns = factory.createOMNamespace(LDAPConstants.CONNECTOR_NAMESPACE, "ns");
OMElement result = factory.createOMElement("result", ns);
OMElement message = factory.createOMElement("message", ns);

org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext)messageContext).getAxis2MessageContext();

try {

DirContext context = LDAPUtils.getDirectoryContext(messageContext);

String classes[] = objectClass.split(",");
Attributes entry = new BasicAttributes();
Attribute obClassAttr = new BasicAttribute("objectClass");
for (int i = 0; i < classes.length; i++) {
obClassAttr.add(classes[i]);
}
entry.put(obClassAttr);
if (attributesString != null) {
String attrSet[] = attributesString.split(",");
for (int i = 0; i < attrSet.length; i++) {
String keyVals[] = attrSet[i].split("=");
Attribute newAttr = new BasicAttribute(keyVals[0]);
newAttr.add(keyVals[1]);
entry.put(newAttr);
}
}
try {
context.createSubcontext(dn, entry);
message.setText("Success");
result.addChild(message);
LDAPUtils.preparePayload(messageContext,result);
} catch (NamingException e) {
log.error("Failed to create ldap entry with dn = " + dn,e);
LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.ADD_ENTRY_ERROR, e);
throw new SynapseException(e);
}
} catch (NamingException e) {
LDAPUtils.handleErrorResponse(messageContext,LDAPConstants.ErrorConstants.INVALID_LDAP_CREDENTIALS,e);
throw new SynapseException(e);
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.connector.ldap;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseException;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.transport.nhttp.NhttpConstants;
import org.wso2.carbon.connector.core.AbstractConnector;
import org.wso2.carbon.connector.core.ConnectException;

public class Authenticate extends AbstractConnector {

@Override
public void connect(MessageContext messageContext) throws ConnectException {
String providerUrl = LDAPUtils.lookupContextParams(messageContext, LDAPConstants.PROVIDER_URL);
String dn = (String)getParameter(messageContext, "dn");
String password = (String)getParameter(messageContext, "password");

OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace ns = factory.createOMNamespace(LDAPConstants.CONNECTOR_NAMESPACE, "ns");
OMElement result = factory.createOMElement("result", ns);
OMElement message = factory.createOMElement("message", ns);

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, providerUrl);
env.put(Context.SECURITY_PRINCIPAL, dn);
env.put(Context.SECURITY_CREDENTIALS, password);

org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext)messageContext).getAxis2MessageContext();

boolean logged = false;
DirContext ctx = null;
try {
ctx = new InitialDirContext(env);
message.setText("Success");
result.addChild(message);
LDAPUtils.preparePayload(messageContext,result);
} catch (NamingException e) {
message.setText("Fail");
result.addChild(message);
LDAPUtils.preparePayload(messageContext,result);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.connector.ldap;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseException;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.transport.nhttp.NhttpConstants;
import org.wso2.carbon.connector.core.AbstractConnector;
import org.wso2.carbon.connector.core.ConnectException;

public class DeleteEntry extends AbstractConnector {
public static final String DN = "dn";

@Override
public void connect(MessageContext messageContext) throws ConnectException {
String dn = (String)getParameter(messageContext, DN);

OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace ns = factory.createOMNamespace(LDAPConstants.CONNECTOR_NAMESPACE, "ns");
OMElement result = factory.createOMElement("result", ns);
OMElement message = factory.createOMElement("message", ns);

org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext();

try {
DirContext context = LDAPUtils.getDirectoryContext(messageContext);
try {
Attributes matchingAttributes = new BasicAttributes(); //search for the existance of dn
matchingAttributes.put(new BasicAttribute("dn"));
NamingEnumeration<SearchResult> searchResult = context.search(dn, matchingAttributes);
try {
context.destroySubcontext(dn);
message.setText("Success");
result.addChild(message);
LDAPUtils.preparePayload(messageContext,result);
} catch (NamingException e) {
log.error("Failed to delete ldap entry with dn = " + dn,e);
LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.DELETE_ENTRY_ERROR, e);
throw new SynapseException(e);
}
} catch (NamingException e) {
LDAPUtils.handleErrorResponse(messageContext,LDAPConstants.ErrorConstants.ENTRY_DOESNOT_EXISTS_ERROR,e);
throw new SynapseException(e);
}
} catch (NamingException e) {
LDAPUtils.handleErrorResponse(messageContext,LDAPConstants.ErrorConstants.INVALID_LDAP_CREDENTIALS,e);
throw new SynapseException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.connector.ldap;

import org.apache.synapse.MessageContext;
import org.wso2.carbon.connector.core.AbstractConnector;
import org.wso2.carbon.connector.core.ConnectException;

public class Init extends AbstractConnector{

@Override
public void connect(MessageContext messageContext) throws ConnectException {
String providerUrl = (String)getParameter(messageContext, LDAPConstants.PROVIDER_URL);
String securityPrincipal = (String)getParameter(messageContext, LDAPConstants.SECURITY_PRINCIPAL);
String securityCredentials = (String)getParameter(messageContext, LDAPConstants.SECURITY_CREDENTIALS);
LDAPUtils.storeAdminLoginDatails(messageContext, providerUrl, securityPrincipal, securityCredentials);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.connector.ldap;

public class LDAPConstants {
public static final String PROVIDER_URL="providerUrl";
public static final String SECURITY_PRINCIPAL="securityPrincipal";
public static final String SECURITY_CREDENTIALS ="securityCredentials";
public static final String CONNECTOR_NAMESPACE = "http://org.wso2.esbconnectors.ldap";

public static final class ErrorConstants{
public static final int SEARCH_ERROR = 7000001;
public static final int INVALID_LDAP_CREDENTIALS = 7000002;
public static final int ADD_ENTRY_ERROR = 7000003;
public static final int UPDATE_ENTRY_ERROR = 7000004;
public static final int DELETE_ENTRY_ERROR = 7000005;
public static final int ENTRY_DOESNOT_EXISTS_ERROR = 7000006;

}
}
Loading