Skip to content

Commit

Permalink
refactor(jackson): use mode name to match obfuscate processor
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Chan committed Nov 8, 2024
1 parent 0ee1eba commit fda0e4e
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
import com.power4j.fist.boot.common.jackson.module.DateTimeModule;
import com.power4j.fist.boot.common.jackson.module.NumberStrModule;
import com.power4j.fist.jackson.support.obfuscation.NoopStringObfuscate;
import com.power4j.fist.jackson.support.obfuscation.ObfuscateProcessorProvider;
import com.power4j.fist.jackson.support.obfuscation.ObfuscatedAnnotationIntrospector;
import com.power4j.fist.jackson.support.obfuscation.SimpleStringObfuscate;
Expand Down Expand Up @@ -73,6 +74,14 @@ public class JacksonConfig {
MODULE_MAP.put(JacksonCustomizeProperties.ModuleName.NumberToStr, new NumberStrModule());
}

@Order
@Bean
@ConditionalOnMissingBean
NoopStringObfuscate noopStringObfuscate() {
return new NoopStringObfuscate();
}

@Order
@Bean
@ConditionalOnMissingBean
SimpleStringObfuscate simpleStringObfuscate() {
Expand Down Expand Up @@ -158,9 +167,9 @@ static class ObfuscateProcessorBeanRegistry implements ObfuscateProcessorProvide
}

@Override
public Optional<StringObfuscate> getInstance(Class<? extends StringObfuscate> obfuscateClass) {
public Optional<StringObfuscate> getInstance(String mode) {
for (StringObfuscate processor : processors) {
if (processor.getClass().equals(obfuscateClass)) {
if (mode.equals(processor.modeId())) {
return Optional.of(processor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.power4j.fist.jackson.support.obfuscation.ObfuscatedStringDeserializer;
import com.power4j.fist.jackson.support.obfuscation.ObfuscatedStringSerializer;
import com.power4j.fist.jackson.support.obfuscation.SimpleStringObfuscate;
import com.power4j.fist.jackson.support.obfuscation.StringObfuscate;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -26,6 +24,10 @@
@JsonDeserialize(using = ObfuscatedStringDeserializer.class)
public @interface Obfuscation {

Class<? extends StringObfuscate> processor() default SimpleStringObfuscate.class;
/**
* Obfuscation mode, default value {@code "noop"}
* @return mode name to determine witch obfuscation processor should be used
*/
String mode() default "noop";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2024. ChenJun ([email protected] & https://github.com/John-Chan)
*
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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 com.power4j.fist.jackson.support.obfuscation;

/**
* @author CJ ([email protected])
* @since 1.0
*/
public class NoopStringObfuscate implements StringObfuscate {

public final static String MODEL_ID = "noop";

@Override
public String modeId() {
return MODEL_ID;
}

@Override
public String obfuscate(String value) throws Exception {
return value;
}

@Override
public String deobfuscate(String value) throws Exception {
return value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public interface ObfuscateProcessorProvider {

/**
* Resolve the specified obfuscate instance.
* @param obfuscateClass Class
* @param mode the mode to use
* @return empty if not found
*/
Optional<StringObfuscate> getInstance(Class<? extends StringObfuscate> obfuscateClass);
Optional<StringObfuscate> getInstance(String mode);

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public String deserialize(JsonParser p, DeserializationContext ctx) throws IOExc
if (value == null) {
return null;
}
else if (!value.startsWith(obfuscate.algorithm() + ".")) {
else if (!value.startsWith(obfuscate.modeId() + StringObfuscate.HEAD)) {
return value;
}
value = value.substring(obfuscate.algorithm().length() + 1);
value = value.substring(obfuscate.modeId().length() + 1);
try {
return obfuscate.deobfuscate(value);
}
Expand All @@ -82,11 +82,10 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctx, BeanProp
if (annotation == null) {
return StringDeserializer.instance;
}
Class<? extends StringObfuscate> obfuscate = annotation.processor();
Optional<StringObfuscate> processor = resolver.getInstance(obfuscate);
String mode = annotation.mode();
Optional<StringObfuscate> processor = resolver.getInstance(mode);
if (processor.isEmpty()) {
throw new IllegalStateException(
String.format("Obfuscation processor not registered: %s", annotation.processor().getName()));
throw new IllegalStateException(String.format("Obfuscation processor not registered: %s", mode));
}
return new ObfuscatedStringDeserializer(processor.get(), resolver);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ else if (value.isEmpty()) {
else {
String obfuscated;
try {
obfuscated = obfuscate.algorithm() + "." + obfuscate.obfuscate(value);
obfuscated = obfuscate.modeId() + StringObfuscate.HEAD + obfuscate.obfuscate(value);
}
catch (Exception e) {
throw new JsonGenerationException(e, jsonGenerator);
Expand All @@ -84,11 +84,10 @@ public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty
if (annotation == null) {
return prov.findContentValueSerializer(property.getType(), property);
}
Class<? extends StringObfuscate> obfuscate = annotation.processor();
Optional<StringObfuscate> processor = resolver.getInstance(obfuscate);
String mode = annotation.mode();
Optional<StringObfuscate> processor = resolver.getInstance(mode);
if (processor.isEmpty()) {
throw new IllegalStateException(
String.format("Obfuscation processor not registered: %s", annotation.processor().getName()));
throw new IllegalStateException(String.format("Obfuscation processor not registered: %s", mode));
}
return new ObfuscatedStringSerializer(processor.get(), resolver);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
public class SimpleStringObfuscate implements StringObfuscate {

public final static String ALGORITHM = "OBF.XOR_V1";
public final static String MODE_ID = "OBF.XOR_V1";

private final Base64.Encoder ENCODER = Base64.getEncoder();

Expand All @@ -42,8 +42,8 @@ public SimpleStringObfuscate(byte[] key) {
}

@Override
public String algorithm() {
return ALGORITHM;
public String modeId() {
return MODE_ID;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
*/
public interface StringObfuscate {

String HEAD = "$";

/**
* The algorithm id
* The mode id, {@code $} is <b> reserved</b>.
* @return String
*/
String algorithm();
String modeId();

/**
* Obfuscate string value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* @author CJ ([email protected])
Expand All @@ -32,24 +31,25 @@ public class StringObfuscateRegistry implements ObfuscateProcessorProvider {

public final static StringObfuscateRegistry INSTANCE = new StringObfuscateRegistry();

private final static Map<Class<?>, Supplier<StringObfuscate>> OBFUSCATE_MAP = new ConcurrentHashMap<>();
private final static List<StringObfuscate> INSTANCES = new CopyOnWriteArrayList<>();

static {
registerObfuscate(SimpleStringObfuscate.class, SimpleStringObfuscate::ofDefault);
registerObfuscate(new NoopStringObfuscate());
registerObfuscate(SimpleStringObfuscate.ofDefault());
}

public static Optional<StringObfuscate> getObfuscateInstance(Class<? extends StringObfuscate> obfuscate) {
return Optional.ofNullable(OBFUSCATE_MAP.get(obfuscate)).map(Supplier::get);
}

public static void registerObfuscate(Class<? extends StringObfuscate> obfuscate,
Supplier<StringObfuscate> supplier) {
OBFUSCATE_MAP.put(obfuscate, supplier);
public static void registerObfuscate(StringObfuscate instance) {
INSTANCES.add(instance);
}

@Override
public Optional<StringObfuscate> getInstance(Class<? extends StringObfuscate> obfuscateClass) {
return getObfuscateInstance(obfuscateClass);
public Optional<StringObfuscate> getInstance(String mode) {
for (StringObfuscate obfuscate : INSTANCES) {
if (mode.equals(obfuscate.modeId())) {
return Optional.of(obfuscate);
}
}
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class JacksonSimpleStringObfuscateTest {
@Data
public static class Foo {

@Obfuscation
@Obfuscation(mode = SimpleStringObfuscate.MODE_ID)
private String name;

}
Expand All @@ -59,7 +59,7 @@ public void testSerialize() throws IOException {
foo.setName("hello world");
String value = objectMapper.writeValueAsString(foo);
System.out.println("Serialized: " + value);
Assertions.assertEquals("{\"name\":\"OBF.XOR_V1.aGRub2slcWh6ZW4=\"}", value);
Assertions.assertEquals("{\"name\":\"OBF.XOR_V1$aGRub2slcWh6ZW4=\"}", value);
}

@Test
Expand All @@ -82,7 +82,7 @@ public void testSerializeEmpty() throws IOException {

@Test
public void testDeserialize() throws IOException {
String json = "{\"name\":\"OBF.XOR_V1.aGRub2slcWh6ZW4=\"}";
String json = "{\"name\":\"OBF.XOR_V1$aGRub2slcWh6ZW4=\"}";
Foo foo = objectMapper.readValue(json, Foo.class);
System.out.println("Deserialized: " + foo.getName());
Assertions.assertEquals("hello world", foo.getName());
Expand Down

0 comments on commit fda0e4e

Please sign in to comment.