--- /dev/null
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<project name="XEMELIOS-auth-aphm" default="usage" basedir=".">
+ <import file="../../common-tasks.xml"/>
+ <description>Builds, tests, and runs the project XEMELIOS xemelios-auth-aphm</description>
+ <property file="${user.home}/.ant.properties"/>
+
+ <target name="usage">
+ <echo> Using ${ant.version} These are targets supported by this ANT build script: compile -
+ compile all .java files, except thoses in ./tmp folder. clean - deletes all compiled and
+ generated files and resources. jars - build jar files. Do not call this script directly,
+ use the one located at root of projet (in ../..) instead. </echo>
+ </target>
+
+ <target name="compile" depends="mk-output-dirs,define-compile-debug,prepare">
+ <javac srcdir="java" destdir="build/classes" debug="${compile.debug}" encoding="ISO-8859-1">
+ <classpath>
+ <pathelement path="../starter/build/classes"/>
+ <pathelement path="../mysql-persistence/build/classes"/>
+ <pathelement path="../core/build/classes"/>
+ </classpath>
+ </javac>
+ <copy todir="build/classes">
+ <fileset dir="java">
+ <include name="**/*"/>
+ <exclude name="**/*.java"/>
+ <exclude name="**/*.form"/>
+ <exclude name="**/CVS/*"/>
+ </fileset>
+ </copy>
+ </target>
+
+ <target name="jars" depends="prepare">
+ <property file="component.properties"/>
+ <copy file="manifest/manifest.txt" tofile="build/manifest.txt">
+ <filterset>
+ <filter token="version" value="${component.release}"/>
+ <filter token="name" value="${component.name}"/>
+ </filterset>
+ </copy>
+ <jar destfile="build/jars/${component.name}.jar" basedir="build/classes"
+ manifest="build/manifest.txt"/>
+ </target>
+
+ <target name="clean">
+ <delete dir="build"/>
+ </target>
+
+ <target name="mk-output-dirs">
+ <mkdir dir="build/classes"/>
+ <mkdir dir="build/jars"/>
+ </target>
+
+ <target name="config" depends="get-configuration,prepare">
+ <property name="component.dir" value="build/config/source/${component.name}/${component.release}"/>
+ <copy file="component-definition-properties.xml" tofile="build/config/source/component-definition-properties.xml"/>
+ <mkdir dir="${component.dir}"/>
+ <copy todir="${component.dir}">
+ <fileset dir="build/jars" includes="*.jar"/>
+ </copy>
+ <antcall target="generate-update"/>
+ </target>
+
+ <target name="setup">
+ <copy todir="../../build/setup/root/lib">
+ <fileset dir="build/jars" includes="*.jar"/>
+ </copy>
+ </target>
+
+</project>
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright
+ * 2008 axYus - www.axyus.com
+ * 2008 C.Marchand - christophe.marchand@axyus.com
+ * 2008 C.Bosquet - charles.bosquet@axyus.com
+ *
+ * This file is part of XEMELIOS.
+ *
+ * XEMELIOS is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XEMELIOS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XEMELIOS; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package fr.gouv.finances.cp.xemelios.auth.ldapAphm;
+
+import fr.gouv.finances.dgfip.xemelios.common.Scramble;
+import fr.gouv.finances.dgfip.xemelios.auth.AuthEngine;
+import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
+import fr.gouv.finances.dgfip.xemelios.auth.SimpleXemeliosUserImpl;
+import fr.gouv.finances.dgfip.xemelios.data.DataConfigurationException;
+import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.SQLRuntimeException;
+import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.JdbcUtils;
+import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.PStmtBinder.SimplePStmtBinderBuilder;
+import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.RowMapper.ColToIntegerRowMapper;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Hashtable;
+import javax.naming.AuthenticationException;
+import javax.naming.NamingEnumeration;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+
+/**
+ * @author CHM
+ * @author CBO
+ */
+public class LdapAuthEngineImpl implements AuthEngine {
+
+ private String baseUrl;
+ private String adminLogin;
+ private String adminPassword;
+ private String searchPath;
+ private String searchField;
+
+ public LdapAuthEngineImpl() throws DataConfigurationException, SQLException {
+ baseUrl = DataLayerManager.getImplementation().getParameterValue("auth.ldap.baseUrl");
+ adminLogin = DataLayerManager.getImplementation().getParameterValue("auth.ldap.adminLogin");
+ adminPassword = DataLayerManager.getImplementation().getParameterValue("auth.ldap.adminPassword");
+ searchPath = DataLayerManager.getImplementation().getParameterValue("auth.ldap.searchPath");
+ searchField = DataLayerManager.getImplementation().getParameterValue("auth.ldap.searchField");
+ if (adminPassword != null) {
+ // le mot de passe est crypté, il faut le décrypter
+ adminPassword = Scramble.unScramblePassword(adminPassword);
+ }
+ Connection connection = null;
+ try {
+ connection = PoolManager.getInstance().getConnection();
+ String sql = "CREATE TABLE IF NOT EXISTS APHM_USERS (LOGIN VARCHAR(24) NOT NULL, ACCESS INT)";
+ JdbcUtils.executeUpdate(connection, sql);
+ } finally {
+ PoolManager.getInstance().releaseConnection(connection);
+ }
+ }
+
+ public boolean isLoginRequired() {
+ return true;
+ }
+
+ public boolean isPasswordRequired() {
+ return true;
+ }
+
+ public XemeliosUser createUser(String login, String password) {
+ Integer authAsInteger = getAuth(login);
+ if (authAsInteger != null) {
+ Hashtable env = new Hashtable();
+ env.put(DirContext.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
+ env.put(DirContext.PROVIDER_URL, baseUrl);
+ env.put(DirContext.SECURITY_AUTHENTICATION, "simple");
+ env.put(DirContext.SECURITY_PRINCIPAL, adminLogin);
+ env.put(DirContext.SECURITY_CREDENTIALS, adminPassword);
+ DirContext ctx = null;
+ String userDN = null;
+ try {
+ // Bind admin :
+ ctx = new InitialDirContext(env);
+ //recherche login demandé dans l'arbre
+ SearchControls ctls = new SearchControls();
+ ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
+ NamingEnumeration e = ctx.search(searchPath, "(" + searchField + "=" + login.toUpperCase() + ")", ctls);
+ if (e.hasMore()) {
+ SearchResult o = (SearchResult) e.next();
+ userDN = "" + o.getAttributes().get("distinguishedName").getAll().next();
+ }
+ } catch (Exception e) {
+ } finally {
+ try {
+ if (ctx != null) {
+ ctx.close();
+ }
+ } catch (Exception e) {
+ }
+ }
+ if (userDN == null) {
+ return null; // user pas trouvé
+ } else {
+ // Bind user :
+ env = new Hashtable();
+ env.put(DirContext.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
+ env.put(DirContext.PROVIDER_URL, baseUrl);
+ env.put(DirContext.SECURITY_AUTHENTICATION, "simple");
+ env.put(DirContext.SECURITY_PRINCIPAL, userDN);
+ env.put(DirContext.SECURITY_CREDENTIALS, password);
+ try {
+ ctx = new InitialDirContext(env);
+ return new SimpleXemeliosUserImpl(login, login,
+ false, AuthEngine.Helper.rolesFromInt(authAsInteger), true, null, true, null);
+ } catch (AuthenticationException e) {
+ return null;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ } finally {
+ try {
+ if (ctx != null) {
+ ctx.close();
+ }
+ } catch (Exception e) { }
+ }
+ }
+ } else {
+ return null;
+ }
+ }
+
+ static private Integer getAuth(String login) {
+ Connection connection = null;
+ try {
+ connection = PoolManager.getInstance().getConnection();
+ String sql = "SELECT ACCESS FROM APHM_USERS WHERE LOGIN = ?";//'"+login.toLowerCase()+"'";
+ return (Integer) JdbcUtils.queryObject(connection,
+ sql,
+ new ColToIntegerRowMapper(),
+ new SimplePStmtBinderBuilder().add(login.toLowerCase()).toPStmtBinder());
+ } catch (SQLException sqle) {
+ throw new SQLRuntimeException(sqle);
+ } finally {
+ PoolManager.getInstance().releaseConnection(connection);
+ }
+ }
+
+}