You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by sz...@apache.org on 2010/02/06 16:52:48 UTC
svn commit: r907237 - in /directory/sandbox/szoerner: ./ src/ src/docs/
src/docs/images/ src/main/ src/main/java/ src/main/java/org/
src/main/java/org/apache/ src/main/java/org/apache/directory/
src/main/java/org/apache/directory/samples/ src/main/java...
Author: szoerner
Date: Sat Feb 6 15:52:46 2010
New Revision: 907237
URL: http://svn.apache.org/viewvc?rev=907237&view=rev
Log: (empty)
Added:
directory/sandbox/szoerner/pom.xml
directory/sandbox/szoerner/src/
directory/sandbox/szoerner/src/docs/
directory/sandbox/szoerner/src/docs/images/
directory/sandbox/szoerner/src/docs/images/passwordHashInterceptor_UML.vsd (with props)
directory/sandbox/szoerner/src/main/
directory/sandbox/szoerner/src/main/java/
directory/sandbox/szoerner/src/main/java/org/
directory/sandbox/szoerner/src/main/java/org/apache/
directory/sandbox/szoerner/src/main/java/org/apache/directory/
directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/
directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/
directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/
directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/HashTools.java
directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/Main.java
directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/PasswordHashInterceptor.java
directory/sandbox/szoerner/src/main/resources/
directory/sandbox/szoerner/src/test/
directory/sandbox/szoerner/src/test/java/
directory/sandbox/szoerner/src/test/java/org/
directory/sandbox/szoerner/src/test/java/org/apache/
directory/sandbox/szoerner/src/test/java/org/apache/directory/
directory/sandbox/szoerner/src/test/java/org/apache/directory/samples/
directory/sandbox/szoerner/src/test/java/org/apache/directory/samples/interceptor/
directory/sandbox/szoerner/src/test/java/org/apache/directory/samples/interceptor/pwdhash/
directory/sandbox/szoerner/src/test/java/org/apache/directory/samples/interceptor/pwdhash/HashToolsTest.java
directory/sandbox/szoerner/src/test/resources/
directory/sandbox/szoerner/src/test/resources/log4j.properties
Added: directory/sandbox/szoerner/pom.xml
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/pom.xml?rev=907237&view=auto
==============================================================================
--- directory/sandbox/szoerner/pom.xml (added)
+++ directory/sandbox/szoerner/pom.xml Sat Feb 6 15:52:46 2010
@@ -0,0 +1,195 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.directory.samples</groupId>
+ <artifactId>passwordHashInterceptor</artifactId>
+ <packaging>jar</packaging>
+ <version>1.5.6-SNAPSHOT</version>
+ <name>An example for an interceptor</name>
+ <url>http://maven.apache.org</url>
+
+ <build>
+ <finalName>passwordHashInterceptor</finalName>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <groupId>org.apache.maven.plugins</groupId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-bootstrap-extract</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-bootstrap-partition</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-core</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-core-avl</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-core-entry</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-core-jndi</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-core-shared</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-jdbm</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-jdbm-store</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-protocol-ldap</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-protocol-shared</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-schema-bootstrap</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-schema-registries</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-utils</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-xdbm-base</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-xdbm-search</artifactId>
+ <version>1.5.5</version>
+ </dependency>
+
+ <dependency>
+ <groupId>commons-collections</groupId>
+ <artifactId>commons-collections</artifactId>
+ <version>3.2.1</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.4</version>
+ </dependency>
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.14</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.mina</groupId>
+ <artifactId>mina-core</artifactId>
+ <version>1.1.7</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.mina</groupId>
+ <artifactId>mina-filter-ssl</artifactId>
+ <version>1.1.7</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.directory.shared</groupId>
+ <artifactId>shared-asn1</artifactId>
+ <version>0.9.15</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.shared</groupId>
+ <artifactId>shared-asn1-codec</artifactId>
+ <version>0.9.15</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.shared</groupId>
+ <artifactId>shared-ldap</artifactId>
+ <version>0.9.15</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.directory.shared</groupId>
+ <artifactId>shared-ldap-constants</artifactId>
+ <version>0.9.15</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.5.6</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>1.5.6</version>
+ </dependency>
+ <dependency>
+ <groupId>antlr</groupId>
+ <artifactId>antlr</artifactId>
+ <version>2.7.7</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.4</version>
+ </dependency> </dependencies>
+</project>
Added: directory/sandbox/szoerner/src/docs/images/passwordHashInterceptor_UML.vsd
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/src/docs/images/passwordHashInterceptor_UML.vsd?rev=907237&view=auto
==============================================================================
Binary file - no diff available.
Propchange: directory/sandbox/szoerner/src/docs/images/passwordHashInterceptor_UML.vsd
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added: directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/HashTools.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/HashTools.java?rev=907237&view=auto
==============================================================================
--- directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/HashTools.java (added)
+++ directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/HashTools.java Sat Feb 6 15:52:46 2010
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.apache.directory.samples.interceptor.pwdhash;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+import org.apache.directory.shared.ldap.util.Base64;
+
+public class HashTools {
+
+ private HashTools() {
+ }
+
+ private static final String[] hashAlgorithms = { "MD5", "SHA", "SSHA",
+ "SMD5" };
+
+ /**
+ * Applys a hash algorithm to a sequence of bytes.
+ *
+ * @param algorithm
+ * algorithm to use
+ * @param value
+ * value to be hashed
+ * @return hashed value preceeded by algorithm in curly brackets
+ *
+ * @throws NoSuchAlgorithmException
+ */
+ public static boolean isAlreadyHashed(byte[] value) {
+ String sValue = new String(value);
+ for (String alg : hashAlgorithms) {
+ if (sValue.startsWith("{" + alg + "}")) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Detects wether a value has already been hashed with a known message
+ * digest algorithm
+ *
+ * @param value
+ * value to analyse
+ * @return true, if already hashed
+ */
+ public static byte[] applyHashAlgorithm(String algorithm, byte[] value)
+ throws NoSuchAlgorithmException {
+
+ MessageDigest digest = MessageDigest.getInstance(algorithm);
+ digest.update(value);
+ byte[] digestedValue = digest.digest();
+ char[] base64encoded = Base64.encode(digestedValue);
+
+ String result = "{" + algorithm + "}" + new String(base64encoded);
+ return result.getBytes();
+ }
+}
Added: directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/Main.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/Main.java?rev=907237&view=auto
==============================================================================
--- directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/Main.java (added)
+++ directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/Main.java Sat Feb 6 15:52:46 2010
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.apache.directory.samples.interceptor.pwdhash;
+
+import java.util.List;
+
+import org.apache.directory.server.core.DefaultDirectoryService;
+import org.apache.directory.server.core.DirectoryService;
+import org.apache.directory.server.core.interceptor.Interceptor;
+import org.apache.directory.server.ldap.LdapServer;
+import org.apache.directory.server.protocol.shared.transport.TcpTransport;
+
+public class Main {
+
+ public static void main(String[] args) throws Exception {
+
+ DirectoryService directoryService = new DefaultDirectoryService();
+ directoryService.setShutdownHookEnabled(true);
+
+ LdapServer ldapServer = new LdapServer();
+ ldapServer.setDirectoryService(directoryService);
+ ldapServer.setAllowAnonymousAccess(true);
+
+ List<Interceptor> is = directoryService.getInterceptors();
+ is.add(0, new PasswordHashInterceptor());
+ directoryService.setInterceptors(is);
+
+ TcpTransport ldapTransport = new TcpTransport(10389);
+ ldapServer.setTransports(ldapTransport);
+
+ directoryService.startup();
+ ldapServer.start();
+ }
+
+}
Added: directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/PasswordHashInterceptor.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/PasswordHashInterceptor.java?rev=907237&view=auto
==============================================================================
--- directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/PasswordHashInterceptor.java (added)
+++ directory/sandbox/szoerner/src/main/java/org/apache/directory/samples/interceptor/pwdhash/PasswordHashInterceptor.java Sat Feb 6 15:52:46 2010
@@ -0,0 +1,103 @@
+package org.apache.directory.samples.interceptor.pwdhash;
+
+import static org.apache.directory.samples.interceptor.pwdhash.HashTools.applyHashAlgorithm;
+import static org.apache.directory.samples.interceptor.pwdhash.HashTools.isAlreadyHashed;
+
+import java.util.List;
+import java.util.Set;
+
+import org.apache.directory.server.core.entry.ClonedServerEntry;
+import org.apache.directory.server.core.interceptor.BaseInterceptor;
+import org.apache.directory.server.core.interceptor.NextInterceptor;
+import org.apache.directory.server.core.interceptor.context.AddOperationContext;
+import org.apache.directory.server.core.interceptor.context.ModifyOperationContext;
+import org.apache.directory.shared.ldap.entry.EntryAttribute;
+import org.apache.directory.shared.ldap.entry.Modification;
+import org.apache.directory.shared.ldap.entry.ModificationOperation;
+import org.apache.directory.shared.ldap.schema.AttributeType;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.
+ *
+ */
+
+public class PasswordHashInterceptor extends BaseInterceptor {
+
+ private String passwordAttributeName = "userPassword";
+
+ private String hashAlgorithm = "MD5";
+
+ public void setPasswordAttributeName(String passwordAttributeName) {
+ this.passwordAttributeName = passwordAttributeName;
+ }
+
+ public void setHashAlgorithm(String hashAlgorithm) {
+ this.hashAlgorithm = hashAlgorithm;
+ }
+
+ /**
+ * Intercepts the modify operation in order to replace plain password values
+ * with hashed ones.
+ */
+ @Override
+ public void modify(NextInterceptor next, ModifyOperationContext opContext)
+ throws Exception {
+
+ List<Modification> items = opContext.getModItems();
+ for (Modification modification : items) {
+ if (modification.getOperation() == ModificationOperation.ADD_ATTRIBUTE
+ || modification.getOperation() == ModificationOperation.REPLACE_ATTRIBUTE) {
+ EntryAttribute attribute = modification.getAttribute();
+ if (attribute.getId().equalsIgnoreCase(passwordAttributeName)) {
+ hashPasswordIfNeccessary(attribute);
+ }
+ }
+ }
+ super.modify(next, opContext);
+ }
+
+ @Override
+ public void add(NextInterceptor next, AddOperationContext opContext)
+ throws Exception {
+
+ ClonedServerEntry entry = opContext.getEntry();
+ Set<AttributeType> attributeTypes = entry.getAttributeTypes();
+ for (AttributeType attributeType : attributeTypes) {
+ if (attributeType.getName().equalsIgnoreCase(passwordAttributeName)) {
+ EntryAttribute attribute = entry.get(attributeType);
+ hashPasswordIfNeccessary(attribute);
+ }
+ }
+
+ super.add(next, opContext);
+ }
+
+ protected void hashPasswordIfNeccessary(EntryAttribute attribute) {
+ try {
+ byte[] password = attribute.getBytes();
+ if (!isAlreadyHashed(password)) {
+ byte[] hashed = applyHashAlgorithm(hashAlgorithm, password);
+ System.out.println("Hashed value:" + new String(hashed));
+ attribute.clear();
+ attribute.add(hashed);
+ }
+ } catch (Exception e) {
+ throw new RuntimeException("Password hash failed", e);
+ }
+ }
+}
Added: directory/sandbox/szoerner/src/test/java/org/apache/directory/samples/interceptor/pwdhash/HashToolsTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/src/test/java/org/apache/directory/samples/interceptor/pwdhash/HashToolsTest.java?rev=907237&view=auto
==============================================================================
--- directory/sandbox/szoerner/src/test/java/org/apache/directory/samples/interceptor/pwdhash/HashToolsTest.java (added)
+++ directory/sandbox/szoerner/src/test/java/org/apache/directory/samples/interceptor/pwdhash/HashToolsTest.java Sat Feb 6 15:52:46 2010
@@ -0,0 +1,31 @@
+package org.apache.directory.samples.interceptor.pwdhash;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class HashToolsTest {
+
+ @Test
+ public void applyHashAlgorithmToPasswordSecret() throws Exception {
+
+ String password = "secret";
+
+ byte[] result = HashTools
+ .applyHashAlgorithm("MD5", password.getBytes());
+ String resultString = new String(result);
+
+ String expectedResult = "{MD5}Xr4ilOzQ4PCOq3aQ0qbuaQ==";
+ Assert.assertEquals(expectedResult.length(), resultString.length());
+ Assert.assertEquals(expectedResult, resultString);
+ }
+
+ @Test
+ public void checkIfAlredyHashed() {
+
+ String hashedMD5 = "{MD5}Xr4ilOzQ4PCOq3aQ0qbuaQ==";
+ Assert.assertTrue(HashTools.isAlreadyHashed(hashedMD5.getBytes()));
+ Assert.assertFalse(HashTools.isAlreadyHashed("secret".getBytes()));
+
+ }
+
+}
Added: directory/sandbox/szoerner/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/src/test/resources/log4j.properties?rev=907237&view=auto
==============================================================================
--- directory/sandbox/szoerner/src/test/resources/log4j.properties (added)
+++ directory/sandbox/szoerner/src/test/resources/log4j.properties Sat Feb 6 15:52:46 2010
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF 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.
+log4j.rootCategory=INFO, stdout, R
+
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=[%d{HH:mm:ss}] %p [%c] - %m%n
+
+log4j.appender.R=org.apache.log4j.RollingFileAppender
+log4j.appender.R.File=apacheds-rolling.log
+
+log4j.appender.R.MaxFileSize=1024KB
+# Keep some backup files
+log4j.appender.R.MaxBackupIndex=5
+
+log4j.appender.R.layout=org.apache.log4j.PatternLayout
+log4j.appender.R.layout.ConversionPattern=[%d{HH:mm:ss}] %p [%c] - %m%n
+
+log4j.logger.org.apache.directory.samples.partition.hello=DEBUG
+