Skip to content

Commit

Permalink
Fix #411: Logging of steps create invalid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
petrdvorak committed Jan 29, 2024
1 parent 82c917b commit 6ddd3de
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public ResultStatusObject execute(PowerAuthStep stepId, PowerAuthVersion version
throw new PowerAuthCmdException();
}

BaseStep step = stepProvider.getStep(stepId, version);
final BaseStep step = stepProvider.getStep(stepId, version);

ResultStatusObject result = step.execute(model.toMap());
final ResultStatusObject result = step.execute(stepLogger, model.toMap());
if (result == null) {
throw new PowerAuthCmdException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,6 @@ public AbstractBaseStep(PowerAuthStep step,
*/
protected abstract ParameterizedTypeReference<R> getResponseTypeReference();

/**
* Executes this step with a given context
*
* @param context Provided context
* @return Result status object, null in case of failure.
* @throws Exception In case of any error.
*/
@Override
public ResultStatusObject execute(Map<String, Object> context) throws Exception {
StepLogger stepLogger = stepLoggerFactory.createStepLogger();
stepLogger.start();
JSONObject jsonObject = execute(stepLogger, context);
stepLogger.close();
if (jsonObject == null) {
return null;
} else {
return ResultStatusObject.fromJsonObject(jsonObject);
}
}

/**
* Execute this step with given logger and context objects.
*
Expand All @@ -155,7 +135,7 @@ public ResultStatusObject execute(Map<String, Object> context) throws Exception
* @return Result status object (with current activation status), null in case of failure.
* @throws Exception In case of a failure.
*/
public final JSONObject execute(StepLogger stepLogger, Map<String, Object> context) throws Exception {
public final ResultStatusObject execute(StepLogger stepLogger, Map<String, Object> context) throws Exception {
if (stepLogger == null) {
stepLogger = DisabledStepLogger.INSTANCE;
}
Expand Down Expand Up @@ -187,7 +167,12 @@ public final JSONObject execute(StepLogger stepLogger, Map<String, Object> conte
return null;
}

return stepContext.getModel().getResultStatusObject();
final JSONObject resultStatusObject = stepContext.getModel().getResultStatusObject();
if (resultStatusObject == null) {
return null;
} else {
return ResultStatusObject.fromJsonObject(resultStatusObject);
}
}

/**
Expand Down Expand Up @@ -231,7 +216,6 @@ public void addEncryptedRequest(StepContext<M, R> stepContext, String applicatio
* @throws Exception when an error during encryption of the request data occurred
*/
public void addEncryptedRequest(StepContext<M, R> stepContext, ClientEncryptor encryptor, byte[] data) throws Exception {
M model = stepContext.getModel();
SimpleSecurityContext securityContext = (SimpleSecurityContext) stepContext.getSecurityContext();
if (securityContext == null) {
stepContext.setSecurityContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.getlime.security.powerauth.lib.cmd.consts.PowerAuthStep;
import io.getlime.security.powerauth.lib.cmd.consts.PowerAuthVersion;
import io.getlime.security.powerauth.lib.cmd.logging.StepLogger;
import io.getlime.security.powerauth.lib.cmd.steps.pojo.ResultStatusObject;

import java.util.List;
Expand All @@ -34,11 +35,12 @@ public interface BaseStep {
/**
* Execute this step with given context objects.
*
* @param stepLogger Step logger.
* @param context Context objects.
* @return Result status object (with current activation status), null in case of failure.
* @throws Exception In case of a failure.
*/
ResultStatusObject execute(Map<String, Object> context) throws Exception;
ResultStatusObject execute(StepLogger stepLogger, Map<String, Object> context) throws Exception;

/**
* @return Corresponding PowerAuth step
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ public void setServerPublicKey(String serverPublicKey) {
*/
@JsonIgnore
public SecretKey getSignatureBiometryKeyObject() {
String signatureBiometryKey = (String) jsonObject.get("signatureBiometryKey");
final String signatureBiometryKey = (String) jsonObject.get("signatureBiometryKey");
if (signatureBiometryKey == null) {
return null;
}
return KEY_CONVERTOR.convertBytesToSharedSecretKey(Base64.getDecoder().decode(signatureBiometryKey));
}

Expand Down Expand Up @@ -290,7 +293,10 @@ public void setSignatureKnowledgeKeySalt(String signatureKnowledgeKeySalt) {
*/
@JsonIgnore
public SecretKey getSignaturePossessionKeyObject() {
String signaturePossessionKey = (String) jsonObject.get("signaturePossessionKey");
final String signaturePossessionKey = (String) jsonObject.get("signaturePossessionKey");
if (signaturePossessionKey == null) {
return null;
}
return KEY_CONVERTOR.convertBytesToSharedSecretKey(Base64.getDecoder().decode(signaturePossessionKey));
}

Expand Down Expand Up @@ -324,7 +330,10 @@ public void setSignaturePossessionKey(String signaturePossessionKey) {
*/
@JsonIgnore
public SecretKey getTransportMasterKeyObject() {
String transportMasterKey = (String) jsonObject.get("transportMasterKey");
final String transportMasterKey = (String) jsonObject.get("transportMasterKey");
if (transportMasterKey == null) {
return null;
}
return KEY_CONVERTOR.convertBytesToSharedSecretKey(Base64.getDecoder().decode(transportMasterKey));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static byte[] readFileBytes(StepLogger stepLogger,
"Empty " + fileDescription + " file",
"File not provided, assuming empty data.",
"WARNING",
filePath
null
);
return new byte[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,7 @@ public static void main(String[] args) {

stepExecutionService.execute(powerAuthStep, version, model);
}
case ACTIVATION_CREATE, ACTIVATION_PREPARE -> {
if (powerAuthStep.equals(PowerAuthStep.ACTIVATION_PREPARE)) {
System.err.println("The 'prepare' step name is deprecated, use the 'create' step name instead");
powerAuthStep = PowerAuthStep.ACTIVATION_CREATE;
}

case ACTIVATION_CREATE -> {
String customAttributesFileName = cmd.getOptionValue("C");
Map<String, Object> customAttributes =
FileUtil.readDataFromFile(stepLogger, customAttributesFileName, HashMap.class, "custom-attributes", "custom attributes");
Expand Down

0 comments on commit 6ddd3de

Please sign in to comment.