
io.getlime.security.powerauth.lib.cmd.steps.v3.TokenAndEncryptStep Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powerauth-java-cmd-lib Show documentation
Show all versions of powerauth-java-cmd-lib Show documentation
PowerAuth Command-line Utility - Java Library
The newest version!
/*
* PowerAuth Command-line utility
* Copyright 2022 Wultra s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.getlime.security.powerauth.lib.cmd.steps.v3;
import io.getlime.security.powerauth.crypto.lib.encryptor.model.EncryptorId;
import io.getlime.security.powerauth.crypto.lib.encryptor.model.EncryptorScope;
import io.getlime.security.powerauth.lib.cmd.consts.BackwardCompatibilityConst;
import io.getlime.security.powerauth.lib.cmd.consts.PowerAuthConst;
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.header.PowerAuthHeaderFactory;
import io.getlime.security.powerauth.lib.cmd.logging.StepLogger;
import io.getlime.security.powerauth.lib.cmd.logging.StepLoggerFactory;
import io.getlime.security.powerauth.lib.cmd.status.ResultStatusService;
import io.getlime.security.powerauth.lib.cmd.steps.AbstractBaseStep;
import io.getlime.security.powerauth.lib.cmd.steps.context.RequestContext;
import io.getlime.security.powerauth.lib.cmd.steps.context.StepContext;
import io.getlime.security.powerauth.lib.cmd.steps.model.TokenAndEncryptStepModel;
import io.getlime.security.powerauth.lib.cmd.util.SecurityUtil;
import io.getlime.security.powerauth.rest.api.model.response.EciesEncryptedResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* Token and encrypt step generates token authentication for request data and performs encryption using ECIES encryption in activation scope.
*
* PowerAuth protocol versions:
*
* - 3.0
* - 3.1
* - 3.2
* - 3.3
*
*
* @author Roman Strobl, [email protected]
*/
@Component
public class TokenAndEncryptStep extends AbstractBaseStep {
private final PowerAuthHeaderFactory powerAuthHeaderFactory;
/**
* Constructor.
* @param powerAuthHeaderFactory PowerAuth header factory.
* @param resultStatusService Result status service.
* @param stepLoggerFactory Step logger factory.
*/
@Autowired
public TokenAndEncryptStep(
PowerAuthHeaderFactory powerAuthHeaderFactory,
ResultStatusService resultStatusService,
StepLoggerFactory stepLoggerFactory) {
super(PowerAuthStep.TOKEN_ENCRYPT, PowerAuthVersion.VERSION_3, resultStatusService, stepLoggerFactory);
this.powerAuthHeaderFactory = powerAuthHeaderFactory;
}
/**
* Constructor for backward compatibility
*/
public TokenAndEncryptStep() {
this(
BackwardCompatibilityConst.POWER_AUTH_HEADER_FACTORY,
BackwardCompatibilityConst.RESULT_STATUS_SERVICE,
BackwardCompatibilityConst.STEP_LOGGER_FACTORY
);
}
@Override
protected ParameterizedTypeReference getResponseTypeReference() {
return PowerAuthConst.RESPONSE_TYPE_REFERENCE_V3;
}
@Override
public StepContext prepareStepContext(StepLogger stepLogger, Map context) throws Exception {
TokenAndEncryptStepModel model = new TokenAndEncryptStepModel();
model.fromMap(context);
RequestContext requestContext = RequestContext.builder()
.signatureHttpMethod(model.getHttpMethod())
.uri(model.getUriString())
.build();
StepContext stepContext =
buildStepContext(stepLogger, model, requestContext);
// Verify that HTTP method is set
if (model.getHttpMethod() == null) {
stepLogger.writeError(getStep().id() + "-error-http-method", "HTTP method not specified", "Specify HTTP method to use for sending request");
stepLogger.writeDoneFailed("token-encrypt-failed");
return null;
}
// Verify HTTP method, GET is not supported
if (HttpMethod.GET.name().equals(model.getHttpMethod().toUpperCase())) {
stepLogger.writeError(getStep().id() + "-error-http-method-invalid", "Token and Encrypt Request Failed", "Unsupported HTTP method: " + model.getHttpMethod().toUpperCase());
stepLogger.writeDoneFailed("token-encrypt-failed");
return null;
}
// Read data which needs to be encrypted
byte[] requestDataBytes = model.getData();
if (requestDataBytes == null || requestDataBytes.length == 0) {
requestDataBytes = new byte[0];
stepLogger.writeItem(
getStep().id() + "-warning-empty-data",
"Empty data",
"Data file was not found, signature will contain no data",
"WARNING",
null
);
}
stepLogger.writeItem(
getStep().id() + "-request-prepare",
"Preparing Request Data",
"Following data will be encrypted",
"OK",
requestDataBytes
);
requestContext.setRequestObject(requestDataBytes);
// Encrypt the request
addEncryptedRequest(stepContext, model.getApplicationKey(), model.getApplicationSecret(), EncryptorId.ACTIVATION_SCOPE_GENERIC, requestDataBytes, EncryptorScope.ACTIVATION_SCOPE);
powerAuthHeaderFactory.getHeaderProvider(model).addHeader(stepContext);
return stepContext;
}
@Override
public void processResponse(StepContext stepContext) throws Exception {
SecurityUtil.processEncryptedResponse(stepContext, getStep().id());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy