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.provisioning.api.serialization;
20  
21  import com.fasterxml.jackson.core.JsonGenerator;
22  import com.fasterxml.jackson.databind.JsonSerializer;
23  import com.fasterxml.jackson.databind.SerializerProvider;
24  import java.io.IOException;
25  import java.lang.reflect.Field;
26  import java.util.Base64;
27  import org.identityconnectors.common.security.EncryptorFactory;
28  import org.identityconnectors.common.security.GuardedString;
29  import org.identityconnectors.common.security.SecurityUtil;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.util.ReflectionUtils;
33  
34  class GuardedStringSerializer extends JsonSerializer<GuardedString> {
35  
36      private static final Logger LOG = LoggerFactory.getLogger(GuardedStringSerializer.class);
37  
38      private static final String READONLY = "readOnly";
39  
40      private static final String DISPOSED = "disposed";
41  
42      private static final String ENCRYPTED_BYTES = "encryptedBytes";
43  
44      private static final String BASE64_SHA1_HASH = "base64SHA1Hash";
45  
46      private static final String LOG_ERROR_MESSAGE = "Could not get field value";
47  
48      @Override
49      public void serialize(final GuardedString source, final JsonGenerator jgen, final SerializerProvider sp)
50              throws IOException {
51  
52          jgen.writeStartObject();
53  
54          boolean readOnly = false;
55          try {
56              Field field = GuardedString.class.getDeclaredField(READONLY);
57              ReflectionUtils.makeAccessible(field);
58              readOnly = field.getBoolean(source);
59          } catch (Exception e) {
60              LOG.error(LOG_ERROR_MESSAGE, e);
61          }
62          jgen.writeBooleanField(READONLY, readOnly);
63  
64          boolean disposed = false;
65          try {
66              Field field = GuardedString.class.getDeclaredField(DISPOSED);
67              ReflectionUtils.makeAccessible(field);
68              disposed = field.getBoolean(source);
69          } catch (Exception e) {
70              LOG.error(LOG_ERROR_MESSAGE, e);
71          }
72          jgen.writeBooleanField(DISPOSED, disposed);
73  
74          byte[] encryptedBytes =
75                  EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(SecurityUtil.decrypt(source).getBytes());
76          jgen.writeStringField(ENCRYPTED_BYTES, Base64.getEncoder().encodeToString(encryptedBytes));
77  
78          String base64SHA1Hash = null;
79          try {
80              Field field = GuardedString.class.getDeclaredField(BASE64_SHA1_HASH);
81              ReflectionUtils.makeAccessible(field);
82              base64SHA1Hash = field.get(source).toString();
83          } catch (Exception e) {
84              LOG.error(LOG_ERROR_MESSAGE, e);
85          }
86          if (base64SHA1Hash != null) {
87              jgen.writeStringField(BASE64_SHA1_HASH, base64SHA1Hash);
88          }
89  
90          jgen.writeEndObject();
91      }
92  
93  }