
io.getlime.security.powerauth.lib.cmd.steps.v3.CreateTokenStep 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 2018 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.logging.StepLogger;
import io.getlime.security.powerauth.lib.cmd.logging.StepLoggerFactory;
import io.getlime.security.powerauth.lib.cmd.header.PowerAuthHeaderFactory;
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.CreateTokenStepModel;
import io.getlime.security.powerauth.rest.api.model.entity.TokenResponsePayload;
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.stereotype.Component;
import java.util.Map;
/**
* Helper class with token creation logic.
*
* PowerAuth protocol versions:
*
* - 3.0
* - 3.1
* - 3.2
* - 3.3
*
*
* @author Lukas Lukovsky, [email protected]
* @author Roman Strobl, [email protected]
*/
@Component(value = "createTokenStepV3")
public class CreateTokenStep extends AbstractBaseStep {
private final PowerAuthHeaderFactory powerAuthHeaderFactory;
/**
* Constructor
* @param powerAuthHeaderFactory PowerAuth header factory
* @param resultStatusService Result status service
* @param stepLoggerFactory Step logger factory
*/
@Autowired
public CreateTokenStep(PowerAuthHeaderFactory powerAuthHeaderFactory,
ResultStatusService resultStatusService,
StepLoggerFactory stepLoggerFactory) {
super(PowerAuthStep.TOKEN_CREATE, PowerAuthVersion.VERSION_3, resultStatusService, stepLoggerFactory);
this.powerAuthHeaderFactory = powerAuthHeaderFactory;
}
/**
* Constructor for backward compatibility
*/
public CreateTokenStep() {
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 {
CreateTokenStepModel model = new CreateTokenStepModel();
model.fromMap(context);
RequestContext requestContext = RequestContext.builder()
.signatureHttpMethod("POST")
.signatureRequestUri("/pa/token/create")
.uri(model.getUriString() + "/pa/v3/token/create")
.build();
StepContext stepContext = buildStepContext(stepLogger, model, requestContext);
addEncryptedRequest(stepContext, model.getApplicationKey(), model.getApplicationSecret(), EncryptorId.CREATE_TOKEN, PowerAuthConst.EMPTY_JSON_BYTES, EncryptorScope.ACTIVATION_SCOPE);
powerAuthHeaderFactory.getHeaderProvider(model).addHeader(stepContext);
incrementCounter(model);
return stepContext;
}
@Override
public void processResponse(StepContext stepContext) throws Exception {
final TokenResponsePayload tokenResponsePayload = decryptResponse(stepContext, TokenResponsePayload.class);
stepContext.getStepLogger().writeItem(
getStep().id() + "-token-obtained",
"Token successfully obtained",
"Token was successfully generated and decrypted",
"OK",
Map.of("tokenId", tokenResponsePayload.getTokenId(),
"tokenSecret",tokenResponsePayload.getTokenSecret())
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy