Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #273: Simplified configuration for Test Server #277

Merged
merged 4 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.wultra.security.powerauth.app.testserver.controller;

import com.wultra.security.powerauth.app.testserver.errorhandling.AppConfigInvalidException;
import com.wultra.security.powerauth.app.testserver.model.request.ConfigureApplicationRequest;
import com.wultra.security.powerauth.app.testserver.service.ApplicationService;
import io.getlime.core.rest.model.base.request.ObjectRequest;
Expand Down Expand Up @@ -49,9 +50,10 @@ public ApplicationController(ApplicationService applicationService) {
* Configure an application.
* @param request Configure an application request.
* @return Configure an application response.
* @throws AppConfigInvalidException Thrown in case mobile SDK configuration is invalid.
*/
@PostMapping("config")
public Response createActivation(@RequestBody ObjectRequest<ConfigureApplicationRequest> request) {
public Response createActivation(@RequestBody ObjectRequest<ConfigureApplicationRequest> request) throws AppConfigInvalidException {
return applicationService.configureApplication(request.getRequestObject());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ public ActivationFailedException(String message) {
super(message);
}

/**
* Constructor with error message and cause.
* @param message Error message.
* @param cause Error cause.
*/
public ActivationFailedException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* PowerAuth test and related software components
* Copyright (C) 2022 Wultra s.r.o.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.wultra.security.powerauth.app.testserver.errorhandling;

import java.io.Serial;

/**
* Exception for case when application configuration is invalid.
*
* @author Roman Strobl, [email protected]
*/
public class AppConfigInvalidException extends Exception {

@Serial
private static final long serialVersionUID = -5133187370481724023L;

/**
* Default exception constructor.
*/
public AppConfigInvalidException() {
}

/**
* Constructor with error message.
* @param message Error message.
*/
public AppConfigInvalidException(String message) {
super(message);
}

/**
* Constructor with error message and cause.
* @param message Error message.
* @param cause Error cause.
*/
public AppConfigInvalidException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ public AppConfigNotFoundException(String message) {
super(message);
}

/**
* Constructor with error message and cause.
* @param message Error message.
* @param cause Error cause.
*/
public AppConfigNotFoundException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,27 @@ public class DefaultExceptionHandler {
}

/**
* Exception handler for application not found exception.
* Exception handler for application configuration not found exception.
* @param ex Exception.
* @return Response with error details.
*/
@ExceptionHandler(AppConfigNotFoundException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public @ResponseBody ErrorResponse handleApplicationNotFoundException(AppConfigNotFoundException ex) {
logger.warn("Error occurred during application lookup.", ex);
return new ErrorResponse("APPLICATION_NOT_FOUND", "Application was not found.");
public @ResponseBody ErrorResponse handleAppConfigNotFoundException(AppConfigNotFoundException ex) {
logger.warn("Error occurred during application configuration.", ex);
return new ErrorResponse("APP_CONFIG_NOT_FOUND", "Application configuration was not found.");
romanstrobl marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Exception handler for application configuration invalid exception.
* @param ex Exception.
* @return Response with error details.
*/
@ExceptionHandler(AppConfigInvalidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public @ResponseBody ErrorResponse handleAppConfigInvalidException(AppConfigInvalidException ex) {
logger.warn("Error occurred during application configuration.", ex);
return new ErrorResponse("APP_CONFIG_INVALID", "Application configuration is invalid.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ public GenericCryptographyException(String message) {
super(message);
}

/**
* Constructor with error message and cause.
* @param message Error message.
* @param cause Error cause.
*/
public GenericCryptographyException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ public RemoteExecutionException(String message) {
super(message);
}

/**
* Constructor with error message and cause.
* @param message Error message.
* @param cause Error cause.
*/
public RemoteExecutionException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,39 @@
*/
package com.wultra.security.powerauth.app.testserver.errorhandling;

import java.io.Serial;

/**
* Exception thrown when verification of signature fails.
*
* @author Petr Dvorak, [email protected]
*/
public class SignatureVerificationException extends Exception {

@Serial
private static final long serialVersionUID = 181491361337035037L;

/**
* Default exception constructor.
*/
public SignatureVerificationException() {
}

/**
* Constructor with a message.
* @param message Message.
*/
public SignatureVerificationException(String message) {
super(message);
}

/**
* Constructor with error message and cause.
* @param message Error message.
* @param cause Error cause.
*/
public SignatureVerificationException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ public class ConfigureApplicationRequest {
private String applicationKey;
private String applicationSecret;
private String masterPublicKey;
private String mobileSdkConfig;

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

import com.wultra.security.powerauth.app.testserver.database.TestConfigRepository;
import com.wultra.security.powerauth.app.testserver.database.entity.TestConfigEntity;
import com.wultra.security.powerauth.app.testserver.errorhandling.AppConfigInvalidException;
import com.wultra.security.powerauth.app.testserver.errorhandling.AppConfigNotFoundException;
import com.wultra.security.powerauth.app.testserver.model.request.ConfigureApplicationRequest;
import io.getlime.core.rest.model.base.response.Response;
import io.getlime.security.powerauth.lib.cmd.util.config.SdkConfiguration;
import io.getlime.security.powerauth.lib.cmd.util.config.SdkConfigurationSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -50,16 +53,39 @@ public ApplicationService(TestConfigRepository appConfigRepository) {
* Configure an application.
* @param request Configure an application request.
* @return Configure an application response.
* @throws AppConfigInvalidException Thrown in case mobile SDK configuration is invalid.
*/
@Transactional
public Response configureApplication(ConfigureApplicationRequest request) {
public Response configureApplication(final ConfigureApplicationRequest request) throws AppConfigInvalidException {
final String applicationId = request.getApplicationId();
final String applicationName = request.getApplicationName();
final String applicationKey = request.getApplicationKey();
final String applicationSecret = request.getApplicationSecret();
final String masterPublicKey = request.getMasterPublicKey();
final String mobileSdkConfig = request.getMobileSdkConfig();

TestConfigEntity appConfig = getOrCreateTestAppConfig(applicationId);
final TestConfigEntity appConfig = getOrCreateTestAppConfig(applicationId);

final String applicationKey;
final String applicationSecret;
final String masterPublicKey;

if (mobileSdkConfig != null) {
final SdkConfiguration config;
try {
config = SdkConfigurationSerializer.deserialize(mobileSdkConfig);
} catch (Exception ex) {
logger.warn("Invalid mobile SDK configuration: {}", ex.getMessage());
throw new AppConfigInvalidException("Invalid mobile SDK configuration", ex);
}
if (config == null) {
throw new AppConfigInvalidException("Missing mobile SDK configuration");
}
applicationKey = config.appKeyBase64();
applicationSecret = config.appSecretBase64();
masterPublicKey = config.masterPublicKeyBase64();
} else {
applicationKey = request.getApplicationKey();
applicationSecret = request.getApplicationSecret();
masterPublicKey = request.getMasterPublicKey();
}

appConfig.setApplicationName(applicationName);
appConfig.setApplicationKey(applicationKey);
Expand All @@ -71,7 +97,7 @@ public Response configureApplication(ConfigureApplicationRequest request) {
return new Response();
}

private TestConfigEntity getOrCreateTestAppConfig(String applicationId) {
private TestConfigEntity getOrCreateTestAppConfig(final String applicationId) {
TestConfigEntity appConfig;
try {
appConfig = getTestAppConfig(applicationId);
Expand Down