View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.spring.security;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import org.apache.syncope.common.lib.types.CipherAlgorithm;
27  import org.apache.syncope.core.spring.ApplicationContextProvider;
28  import org.junit.jupiter.api.BeforeAll;
29  import org.junit.jupiter.api.Test;
30  
31  public class EncryptorTest {
32  
33      private static final String PASSWORD_VALUE = "password";
34  
35      private static Encryptor ENCRYPTOR;
36  
37      @BeforeAll
38      public static void setUp() {
39          ApplicationContextProvider.getBeanFactory().registerSingleton("securityProperties", new SecurityProperties());
40          ENCRYPTOR = Encryptor.getInstance();
41      }
42  
43      @Test
44      public void encoder() throws Exception {
45          for (CipherAlgorithm cipherAlgorithm : CipherAlgorithm.values()) {
46              String encPassword = ENCRYPTOR.encode(PASSWORD_VALUE, cipherAlgorithm);
47  
48              assertNotNull(encPassword);
49              assertTrue(ENCRYPTOR.verify(PASSWORD_VALUE, cipherAlgorithm, encPassword));
50              assertFalse(ENCRYPTOR.verify(PASSWORD_VALUE + "diff", cipherAlgorithm, encPassword));
51  
52              // check that same password encoded with BCRYPT or Salted versions results in different digest
53              if (cipherAlgorithm == CipherAlgorithm.BCRYPT || cipherAlgorithm.isSalted()) {
54                  String encSamePassword = ENCRYPTOR.encode(PASSWORD_VALUE, cipherAlgorithm);
55                  assertNotNull(encSamePassword);
56                  assertFalse(encSamePassword.equals(encPassword));
57                  assertTrue(ENCRYPTOR.verify(PASSWORD_VALUE, cipherAlgorithm, encSamePassword));
58              }
59          }
60      }
61  
62      @Test
63      public void decodeDefaultAESKey() throws Exception {
64          String decPassword = ENCRYPTOR.decode("9Pav+xl+UyHt02H9ZBytiA==", CipherAlgorithm.AES);
65          assertEquals(PASSWORD_VALUE, decPassword);
66      }
67  
68      @Test
69      public void smallKey() throws Exception {
70          Encryptor smallKeyEncryptor = Encryptor.getInstance("123");
71          String encPassword = smallKeyEncryptor.encode(PASSWORD_VALUE, CipherAlgorithm.AES);
72          String decPassword = smallKeyEncryptor.decode(encPassword, CipherAlgorithm.AES);
73          assertEquals(PASSWORD_VALUE, decPassword);
74      }
75  
76      @Test
77      public void saltedHash() throws Exception {
78          String encPassword = ENCRYPTOR.encode(PASSWORD_VALUE, CipherAlgorithm.SSHA256);
79          assertNotNull(encPassword);
80  
81          assertTrue(ENCRYPTOR.verify(PASSWORD_VALUE, CipherAlgorithm.SSHA256, encPassword));
82      }
83  
84      @Test
85      public void verifySaltedFromExternal() throws Exception {
86          // generated via https://github.com/peppelinux/pySSHA-slapd with command:
87          // python3 pySSHA/ssha.py -p password -enc sha256 -s 666ac543 \
88          //  | sed 's/{.*}//' | xargs echo -n | base64 -d | xxd -p | tr -d $'\n'  | xargs echo
89          String encPassword = "b098017d584647e3fa1f3e0eb437648aefa84093c15e0d3efb752a4183cfdcf3666ac543";
90          assertTrue(ENCRYPTOR.verify(PASSWORD_VALUE, CipherAlgorithm.SSHA256, encPassword));
91      }
92  }