Skip to content

Commit

Permalink
Fix #971: Add column for signature metadata
Browse files Browse the repository at this point in the history
- Introduce abstraction for SignatureMetadata
  • Loading branch information
jandusil committed Aug 30, 2023
1 parent fe2e837 commit df53390
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.getlime.security.powerauth.app.server.database.model;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
public class PowerAuthSignatureMetadata implements SignatureMetadata<String, String> {

private String signatureDataMethod;

private String signatureDataUriId;


@Override
public String getMetadataParam1() {
return signatureDataMethod;
}

@Override
public void setMetadataParam1(String metadataParam1) {
this.signatureDataMethod = metadataParam1;
}

@Override
public String getMetadataParam2() {
return signatureDataUriId;
}

@Override
public void setMetadataParam2(String metadataParam2) {
this.signatureDataUriId = metadataParam2;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package io.getlime.security.powerauth.app.server.database.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SignatureMetadata {
public interface SignatureMetadata<T1, T2> {

private String signatureDataMethod;
T1 getMetadataParam1();

void setMetadataParam1(T1 metadataParam1);

T2 getMetadataParam2();

void setMetadataParam2(T2 metadataParam2);

private String signatureDataUriId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.getlime.security.powerauth.app.server.database.model.converter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.getlime.security.powerauth.app.server.database.model.SignatureMetadata;
import jakarta.persistence.AttributeConverter;
Expand All @@ -32,49 +33,45 @@
* Converts between SignatureMetadata object and JSON serialized
* storage in database column.
*
* @author Your Name
* @author Jan Dusil
*/
@Converter
@Component
public class SignatureMetadataConverter implements AttributeConverter<SignatureMetadata, String> {
public class SignatureMetadataConverter<T extends SignatureMetadata<?, ?>> implements AttributeConverter<T, String> {

private static final Logger logger = LoggerFactory.getLogger(SignatureMetadataConverter.class);

private final ObjectMapper objectMapper;

/**
* Converter constructor.
* @param objectMapper Object mapper.
*/
@Autowired
public SignatureMetadataConverter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

@Override
public String convertToDatabaseColumn(SignatureMetadata signatureMetadata) {
if (signatureMetadata == null) {
public String convertToDatabaseColumn(T attribute) {
if (attribute == null) {
return "{}";
}
try {
return objectMapper.writeValueAsString(signatureMetadata);
return objectMapper.writeValueAsString(attribute);
} catch (JsonProcessingException ex) {
logger.warn("Conversion failed for SignatureMetadata, error: " + ex.getMessage(), ex);
logger.warn("JSON writing error", ex);
return "{}";
}
}

@Override
public SignatureMetadata convertToEntityAttribute(String s) {
public T convertToEntityAttribute(String s) {
if (s == null || s.isEmpty()) {
return new SignatureMetadata();
return null;
}
try {
return objectMapper.readValue(s, SignatureMetadata.class);
return objectMapper.readValue(s, new TypeReference<>() {
});
} catch (JsonProcessingException ex) {
logger.warn("Conversion failed for SignatureMetadata, error: " + ex.getMessage(), ex);
return new SignatureMetadata();
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package io.getlime.security.powerauth.app.server.database.model.entity;

import io.getlime.security.powerauth.app.server.database.model.SignatureMetadata;
import io.getlime.security.powerauth.app.server.database.model.PowerAuthSignatureMetadata;
import io.getlime.security.powerauth.app.server.database.model.converter.ActivationStatusConverter;
import io.getlime.security.powerauth.app.server.database.model.converter.SignatureMetadataConverter;
import io.getlime.security.powerauth.app.server.database.model.enumeration.ActivationStatus;
Expand Down Expand Up @@ -77,7 +77,7 @@ public class SignatureEntity implements Serializable {

@Column(name = "signature_metadata")
@Convert(converter = SignatureMetadataConverter.class)
private SignatureMetadata signatureMetadata;
private PowerAuthSignatureMetadata signatureMetadata;

@Column(name = "signature_data_body", updatable = false)
private String signatureDataBody;
Expand Down Expand Up @@ -130,7 +130,7 @@ public SignatureEntity(
String signatureVersion,
String signatureType,
String signature,
SignatureMetadata signatureMetadata,
PowerAuthSignatureMetadata signatureMetadata,
String signatureDataBody,
String additionalInfo,
String note,
Expand Down Expand Up @@ -339,7 +339,7 @@ public void setSignature(String signature) {
*
* @return Signature metadata.
*/
public SignatureMetadata getSignatureMetadata() {
public PowerAuthSignatureMetadata getSignatureMetadata() {
return signatureMetadata;
}

Expand All @@ -348,7 +348,7 @@ public SignatureMetadata getSignatureMetadata() {
*
* @param signatureMetadata Metadata related to the signature.
*/
public void setSignatureMetadata(SignatureMetadata signatureMetadata) {
public void setSignatureMetadata(PowerAuthSignatureMetadata signatureMetadata) {
this.signatureMetadata = signatureMetadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import io.getlime.security.powerauth.app.server.converter.ActivationStatusConverter;
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.SignatureMetadata;
import io.getlime.security.powerauth.app.server.database.model.PowerAuthSignatureMetadata;
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 @@ -158,7 +158,7 @@ public void logSignatureAuditRecord(ActivationRecordDto activation, SignatureDat

// Audit the signature
final SignatureEntity signatureAuditRecord = new SignatureEntity();
final SignatureMetadata signatureMetadata = new SignatureMetadata(signatureData.getRequestMethod(), signatureData.getRequestUriId());
final PowerAuthSignatureMetadata signatureMetadata = new PowerAuthSignatureMetadata(signatureData.getRequestMethod(), signatureData.getRequestUriId());
signatureAuditRecord.setActivation(activationRepository.getReferenceById(activation.getActivationId()));
signatureAuditRecord.setActivationCounter(activation.getCounter());
signatureAuditRecord.setActivationCtrDataBase64(activation.getCtrDataBase64());
Expand Down

0 comments on commit df53390

Please sign in to comment.