/* * Copyright 2004, 2005, 2006 Acegi Technology Pty Limited * * Licensed 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.springframework.security.ldap.authentication; import static org.assertj.core.api.Assertions.*; import org.junit.Before; import org.junit.Test; import org.springframework.security.authentication.encoding.LdapShaPasswordEncoder; /** * Tests {@link LdapShaPasswordEncoder}. * * @author Luke Taylor */ public class LdapShaPasswordEncoderTests { // ~ Instance fields // ================================================================================================ LdapShaPasswordEncoder sha; // ~ Methods // ======================================================================================================== @Before public void setUp() throws Exception { sha = new LdapShaPasswordEncoder(); } @Test public void invalidPasswordFails() { assertThat(sha.isPasswordValid("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=", "wrongpassword", null)).isFalse(); } @Test public void invalidSaltedPasswordFails() { assertThat(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX", "wrongpassword", null)).isFalse(); assertThat(sha.isPasswordValid("{SSHA}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd", "wrongpassword", null)).isFalse(); } @Test(expected = IllegalArgumentException.class) public void nonByteArraySaltThrowsException() { sha.encodePassword("password", "AStringNotAByteArray"); } /** * Test values generated by 'slappasswd -h {SHA} -s boabspasswurd' */ @Test public void validPasswordSucceeds() { sha.setForceLowerCasePrefix(false); assertThat(sha.isPasswordValid("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=", "boabspasswurd", null)).isTrue(); assertThat(sha.isPasswordValid("{sha}ddSFGmjXYPbZC+NXR2kCzBRjqiE=", "boabspasswurd", null)).isTrue(); sha.setForceLowerCasePrefix(true); assertThat(sha.isPasswordValid("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=", "boabspasswurd", null)).isTrue(); assertThat(sha.isPasswordValid("{sha}ddSFGmjXYPbZC+NXR2kCzBRjqiE=", "boabspasswurd", null)).isTrue(); } /** * Test values generated by 'slappasswd -s boabspasswurd' */ @Test public void validSaltedPasswordSucceeds() { sha.setForceLowerCasePrefix(false); assertThat(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX", "boabspasswurd", null)).isTrue(); assertThat(sha.isPasswordValid("{ssha}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd", "boabspasswurd", null)).isTrue(); sha.setForceLowerCasePrefix(true); assertThat(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX", "boabspasswurd", null)).isTrue(); assertThat(sha.isPasswordValid("{ssha}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd", "boabspasswurd", null)).isTrue(); } @Test // SEC-1031 public void fullLengthOfHashIsUsedInComparison() throws Exception { // Change the first hash character from '2' to '3' assertThat(sha.isPasswordValid("{SSHA}35ro4PKC8jhQZ26jVsozhX/xaP0suHgX", "boabspasswurd", null)).isFalse(); // Change the last hash character from 'X' to 'Y' assertThat(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgY", "boabspasswurd", null)).isFalse(); } @Test public void correctPrefixCaseIsUsed() { sha.setForceLowerCasePrefix(false); assertThat("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=").isEqualTo( sha.encodePassword("boabspasswurd", null)); assertThat(sha.encodePassword("somepassword", "salt".getBytes()).startsWith( "{SSHA}")); sha.setForceLowerCasePrefix(true); assertThat("{sha}ddSFGmjXYPbZC+NXR2kCzBRjqiE=").isEqualTo( sha.encodePassword("boabspasswurd", null)); assertThat(sha.encodePassword("somepassword", "salt".getBytes()).startsWith( "{ssha}")); } @Test(expected = IllegalArgumentException.class) public void invalidPrefixIsRejected() { sha.isPasswordValid("{MD9}xxxxxxxxxx", "somepassword", null); } @Test(expected = IllegalArgumentException.class) public void malformedPrefixIsRejected() { // No right brace sha.isPasswordValid("{SSHA25ro4PKC8jhQZ26jVsozhX/xaP0suHgX", "somepassword", null); } }