Skip to content

Commit

Permalink
Fix #971: Add column for signature metadata
Browse files Browse the repository at this point in the history
- Incorporate code review comments
  • Loading branch information
jandusil committed Sep 6, 2023
1 parent e2cca7a commit 2406b59
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Data
public class PowerAuthSignatureMetadata implements SignatureMetadata<String, String>, Serializable {
public class PowerAuthSignatureMetadata implements SignatureMetadata<String, Object>, Serializable {

@JsonProperty("signatureDataMethod")
private String signatureDataMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@
import java.io.Serializable;

/**
* Represents a generic interface for metadata related to different types of signatures.
* The interface allows for defining various metadata attributes as type parameters T1 and T2.
* Represents an interface for metadata related to different types of signatures.
* The interface is designed to work specifically with metadata attributes of type
* String for the first parameter and Object for the second parameter. This allows
* the flexibility to capture various metadata details according to the specific
* requirements of different signature algorithms or methods.
*
* @param <T1> The type of the first metadata parameter.
* @param <T2> The type of the second metadata parameter.
* @author Jan Dusil
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = PowerAuthSignatureMetadata.class,
name = SignatureMetadataType.POWERAUTH)
})
public interface SignatureMetadata<T1, T2> extends Serializable {
public interface SignatureMetadata<String, Object> extends Serializable {

}


Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@
@Converter
@Component
@Slf4j
public class SignatureMetadataConverter implements AttributeConverter<SignatureMetadata<?, ?>, String> {
public class SignatureMetadataConverter implements AttributeConverter<SignatureMetadata<String, Object>, String> {

private final ObjectMapper objectMapper;

/**
* Default constructor that initializes a default ObjectMapper.
*/
public SignatureMetadataConverter() {
this.objectMapper = new ObjectMapper();
}

/**
* Constructor that initializes the ObjectMapper.
*
Expand All @@ -59,7 +66,7 @@ public SignatureMetadataConverter(ObjectMapper objectMapper) {
* @return The JSON string representation of the object.
*/
@Override
public String convertToDatabaseColumn(SignatureMetadata<?, ?> attribute) {
public String convertToDatabaseColumn(SignatureMetadata<String, Object> attribute) {
if (attribute == null) {
return "{}";
}
Expand All @@ -78,7 +85,7 @@ public String convertToDatabaseColumn(SignatureMetadata<?, ?> attribute) {
* @return The converted SignatureMetadata object.
*/
@Override
public SignatureMetadata<?, ?> convertToEntityAttribute(String s) {
public SignatureMetadata<String, Object> convertToEntityAttribute(String s) {
if (StringUtils.isBlank(s)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public class SignatureMetadataType {
* Represents PowerAuth signature metadata.
* This value is used for identifying the type of metadata in JSON serialization/deserialization.
*/
public static final String POWERAUTH = "PowerAuthSignatureMetadata";
public static final String POWERAUTH = "POWERAUTH";
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.getlime.security.powerauth.app.server.converter.KeyValueMapConverter;
import io.getlime.security.powerauth.app.server.converter.SignatureTypeConverter;
import io.getlime.security.powerauth.app.server.database.model.PowerAuthSignatureMetadata;
import io.getlime.security.powerauth.app.server.database.model.converter.SignatureMetadataConverter;
import io.getlime.security.powerauth.app.server.database.model.entity.ActivationRecordEntity;
import io.getlime.security.powerauth.app.server.database.model.entity.SignatureEntity;
import io.getlime.security.powerauth.app.server.database.model.enumeration.ActivationStatus;
Expand Down Expand Up @@ -58,6 +59,8 @@ public class AuditingServiceBehavior {
// Prepare converters
private final ActivationStatusConverter activationStatusConverter = new ActivationStatusConverter();
private final SignatureTypeConverter signatureTypeConverter = new SignatureTypeConverter();

private final SignatureMetadataConverter signatureMetadataConverter = new SignatureMetadataConverter();
private final KeyValueMapConverter keyValueMapConverter;

// Generic auditing capability
Expand Down Expand Up @@ -188,8 +191,7 @@ public void logSignatureAuditRecord(ActivationRecordDto activation, SignatureDat
.param("additionalInfo", additionalInfo)
.param("data", data)
.param("signature", signatureData.getSignature())
.param("signatureDataMethod", signatureData.getRequestMethod())
.param("signatureDataUriId", signatureData.getRequestUriId())
.param("signatureMetadata", signatureMetadataConverter.convertToDatabaseColumn(signatureMetadata))
.param("signatureDataBody", signatureData.getRequestBody())
.param("signatureType", signatureType.name())
.param("signatureVersion", signatureData.getSignatureVersion())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public class SignatureMetadataConverterTest {
*/
@BeforeEach
void setUp() {
ObjectMapper objectMapper = new ObjectMapper();
converter = new SignatureMetadataConverter(objectMapper);
converter = new SignatureMetadataConverter();
}

/**
Expand All @@ -59,15 +58,15 @@ void convertToDatabaseColumnTest() {
String jsonStr = converter.convertToDatabaseColumn(metadata);

assertNotNull(jsonStr);
assertEquals("{\"type\":\"PowerAuthSignatureMetadata\",\"signatureDataMethod\":\"POST\",\"signatureDataUriId\":\"123\"}", jsonStr);
assertEquals("{\"type\":\"POWERAUTH\",\"signatureDataMethod\":\"POST\",\"signatureDataUriId\":\"123\"}", jsonStr);
}

/**
* Tests the conversion of a serialized JSON string back to a SignatureMetadata object.
*/
@Test
void convertToEntityAttributeTest() {
String jsonStr = "{\"type\":\"PowerAuthSignatureMetadata\",\"signatureDataMethod\":\"POST\",\"signatureDataUriId\":\"123\"}";
String jsonStr = "{\"type\":\"POWERAUTH\",\"signatureDataMethod\":\"POST\",\"signatureDataUriId\":\"123\"}";
PowerAuthSignatureMetadata metadata = (PowerAuthSignatureMetadata) converter.convertToEntityAttribute(jsonStr);

assertNotNull(metadata);
Expand Down Expand Up @@ -95,7 +94,7 @@ void testRoundTripConversion() {
@Test
void testInvalidJsonInput() {
String invalidJson = "{\"invalidField\":\"someValue\"}";
SignatureMetadata<String, String> metadata = (PowerAuthSignatureMetadata) converter.convertToEntityAttribute(invalidJson);
SignatureMetadata<String, Object> metadata = (PowerAuthSignatureMetadata) converter.convertToEntityAttribute(invalidJson);
assertNull(metadata);
}
}

0 comments on commit 2406b59

Please sign in to comment.