diff --git a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/controller/ApplicationController.java b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/controller/ApplicationController.java index 5646ba2c..f86c49a2 100644 --- a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/controller/ApplicationController.java +++ b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/controller/ApplicationController.java @@ -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; @@ -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 request) { + public Response createActivation(@RequestBody ObjectRequest request) throws AppConfigInvalidException { return applicationService.configureApplication(request.getRequestObject()); } diff --git a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/ActivationFailedException.java b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/ActivationFailedException.java index 74bfc812..a2373d42 100644 --- a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/ActivationFailedException.java +++ b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/ActivationFailedException.java @@ -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); + } + } diff --git a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/AppConfigInvalidException.java b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/AppConfigInvalidException.java new file mode 100644 index 00000000..f2f43f71 --- /dev/null +++ b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/AppConfigInvalidException.java @@ -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 . + */ + +package com.wultra.security.powerauth.app.testserver.errorhandling; + +import java.io.Serial; + +/** + * Exception for case when application configuration is invalid. + * + * @author Roman Strobl, roman.strobl@wultra.com + */ +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); + } + +} \ No newline at end of file diff --git a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/AppConfigNotFoundException.java b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/AppConfigNotFoundException.java index 4bdd3c19..e0cd8329 100644 --- a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/AppConfigNotFoundException.java +++ b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/AppConfigNotFoundException.java @@ -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); + } + } diff --git a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/DefaultExceptionHandler.java b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/DefaultExceptionHandler.java index 0f62b477..ba3dbcf6 100644 --- a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/DefaultExceptionHandler.java +++ b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/DefaultExceptionHandler.java @@ -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."); + } + + /** + * 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."); } /** diff --git a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/GenericCryptographyException.java b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/GenericCryptographyException.java index 247badae..dc436261 100644 --- a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/GenericCryptographyException.java +++ b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/GenericCryptographyException.java @@ -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); + } + } diff --git a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/RemoteExecutionException.java b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/RemoteExecutionException.java index f8b5bbc0..9549aa1c 100644 --- a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/RemoteExecutionException.java +++ b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/RemoteExecutionException.java @@ -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); + } + } diff --git a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/SignatureVerificationException.java b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/SignatureVerificationException.java index 059a2261..3ef44fca 100644 --- a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/SignatureVerificationException.java +++ b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/errorhandling/SignatureVerificationException.java @@ -17,6 +17,8 @@ */ package com.wultra.security.powerauth.app.testserver.errorhandling; +import java.io.Serial; + /** * Exception thrown when verification of signature fails. * @@ -24,6 +26,15 @@ */ public class SignatureVerificationException extends Exception { + @Serial + private static final long serialVersionUID = 181491361337035037L; + + /** + * Default exception constructor. + */ + public SignatureVerificationException() { + } + /** * Constructor with a message. * @param message Message. @@ -31,4 +42,14 @@ public class SignatureVerificationException extends Exception { 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); + } + } diff --git a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/model/request/ConfigureApplicationRequest.java b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/model/request/ConfigureApplicationRequest.java index 72416357..39cf971b 100644 --- a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/model/request/ConfigureApplicationRequest.java +++ b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/model/request/ConfigureApplicationRequest.java @@ -35,5 +35,6 @@ public class ConfigureApplicationRequest { private String applicationKey; private String applicationSecret; private String masterPublicKey; + private String mobileSdkConfig; } diff --git a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/service/ApplicationService.java b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/service/ApplicationService.java index 33506953..3763934e 100644 --- a/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/service/ApplicationService.java +++ b/powerauth-test-server/src/main/java/com/wultra/security/powerauth/app/testserver/service/ApplicationService.java @@ -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; @@ -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); @@ -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);