diff --git a/NOTICE b/NOTICE index 447c1f1a..274e6614 100644 --- a/NOTICE +++ b/NOTICE @@ -16,3 +16,4 @@ Michael Schulte [mschulte-tsi], T-Systems International GmbH Lars Stelzner [lstelzne-tech], T-Systems International GmbH Andreas Mandel [amandel], T-Systems International GmbH Martin Scheffler [martinschefflerTSI] T-Systems International GmbH +Damir Seit [damirseit], Nazarbayev University diff --git a/src/main/java/app/coronawarn/verification/controller/ExternalTokenController.java b/src/main/java/app/coronawarn/verification/controller/ExternalTokenController.java index 6267f5ac..98995591 100644 --- a/src/main/java/app/coronawarn/verification/controller/ExternalTokenController.java +++ b/src/main/java/app/coronawarn/verification/controller/ExternalTokenController.java @@ -20,12 +20,10 @@ package app.coronawarn.verification.controller; -import static java.util.concurrent.TimeUnit.MILLISECONDS; - -import app.coronawarn.verification.domain.VerificationTan; -import app.coronawarn.verification.exception.VerificationServerException; +import app.coronawarn.verification.covid_test_identifier_factory.COVIDTestIdentifier; +import app.coronawarn.verification.covid_test_identifier_factory.COVIDTestIdentifierFactory; +import app.coronawarn.verification.covid_test_identifier_factory.COVIDTestIdentifierFactoryImpl; import app.coronawarn.verification.model.RegistrationToken; -import app.coronawarn.verification.model.RegistrationTokenKeyType; import app.coronawarn.verification.model.RegistrationTokenRequest; import app.coronawarn.verification.service.AppSessionService; import app.coronawarn.verification.service.FakeDelayService; @@ -34,14 +32,13 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; -import java.util.Optional; + import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import javax.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Profile; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.util.StopWatch; @@ -103,44 +100,11 @@ public DeferredResult> generateRegistrationTok } StopWatch stopWatch = new StopWatch(); stopWatch.start(); - String key = request.getKey(); - RegistrationTokenKeyType keyType = request.getKeyType(); - DeferredResult> deferredResult = new DeferredResult<>(); - switch (keyType) { - case GUID: - ResponseEntity responseEntity = - appSessionService.generateRegistrationTokenByGuid(key, request.getKeyDob(), fake); - stopWatch.stop(); - fakeDelayService.updateFakeTokenRequestDelay(stopWatch.getTotalTimeMillis()); - deferredResult.setResult(responseEntity); - log.info("Returning the successfully generated RegistrationToken."); - return deferredResult; - case TELETAN: - Optional optional = tanService.getEntityByTan(key); + COVIDTestIdentifierFactory covidTestIdentifierFactory = new COVIDTestIdentifierFactoryImpl(); - ResponseEntity response = appSessionService.generateRegistrationTokenByTeleTan( - key, - fake, - optional.map(VerificationTan::getTeleTanType).orElse(null)); + COVIDTestIdentifier testIdentifier = covidTestIdentifierFactory.makeCOVIDTestIdentifier(request); - if (optional.isPresent()) { - VerificationTan teleTan = optional.get(); - teleTan.setRedeemed(true); - tanService.saveTan(teleTan); - stopWatch.stop(); - fakeDelayService.updateFakeTokenRequestDelay(stopWatch.getTotalTimeMillis()); - scheduledExecutor.schedule(() -> deferredResult.setResult(response), fakeDelayService.realDelayToken(), - MILLISECONDS); - log.info("Returning the successfully generated RegistrationToken."); - return deferredResult; - } - stopWatch.stop(); - throw new VerificationServerException(HttpStatus.BAD_REQUEST, "The teleTAN verification failed"); - default: - stopWatch.stop(); - throw new VerificationServerException(HttpStatus.BAD_REQUEST, - "Unknown registration key type for registration token"); - } + return testIdentifier.generateRegistrationToken(request, scheduledExecutor, stopWatch, fake, appSessionService, fakeDelayService, tanService); } } diff --git a/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/COVIDTestIdentifier.java b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/COVIDTestIdentifier.java new file mode 100644 index 00000000..69cd6d55 --- /dev/null +++ b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/COVIDTestIdentifier.java @@ -0,0 +1,36 @@ +/*- + * ---license-start + * Corona-Warn-App / cwa-verification + * --- + * Copyright (C) 2020 - 2022 T-Systems International GmbH and all other contributors + * --- + * 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. + * ---license-end + */ + +package app.coronawarn.verification.covid_test_identifier_factory; + +import app.coronawarn.verification.model.RegistrationToken; +import app.coronawarn.verification.model.RegistrationTokenRequest; +import app.coronawarn.verification.service.AppSessionService; +import app.coronawarn.verification.service.FakeDelayService; +import app.coronawarn.verification.service.TanService; +import org.springframework.http.ResponseEntity; +import org.springframework.util.StopWatch; +import org.springframework.web.context.request.async.DeferredResult; + +import java.util.concurrent.ScheduledExecutorService; + +public abstract class COVIDTestIdentifier { + public abstract DeferredResult> generateRegistrationToken(RegistrationTokenRequest request, ScheduledExecutorService scheduledExecutor, StopWatch stopWatch, String fake, AppSessionService appSessionService, FakeDelayService fakeDelayService, TanService tanService); +} diff --git a/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/COVIDTestIdentifierFactory.java b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/COVIDTestIdentifierFactory.java new file mode 100644 index 00000000..285dce6f --- /dev/null +++ b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/COVIDTestIdentifierFactory.java @@ -0,0 +1,29 @@ +/*- + * ---license-start + * Corona-Warn-App / cwa-verification + * --- + * Copyright (C) 2020 - 2022 T-Systems International GmbH and all other contributors + * --- + * 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. + * ---license-end + */ + +package app.coronawarn.verification.covid_test_identifier_factory; + +import app.coronawarn.verification.model.RegistrationTokenRequest; + +public interface COVIDTestIdentifierFactory { + + public COVIDTestIdentifier makeCOVIDTestIdentifier(RegistrationTokenRequest request); + +} diff --git a/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/COVIDTestIdentifierFactoryImpl.java b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/COVIDTestIdentifierFactoryImpl.java new file mode 100644 index 00000000..64a47475 --- /dev/null +++ b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/COVIDTestIdentifierFactoryImpl.java @@ -0,0 +1,37 @@ +/*- + * ---license-start + * Corona-Warn-App / cwa-verification + * --- + * Copyright (C) 2020 - 2022 T-Systems International GmbH and all other contributors + * --- + * 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. + * ---license-end + */ + +package app.coronawarn.verification.covid_test_identifier_factory; + +import app.coronawarn.verification.model.RegistrationTokenRequest; + +public class COVIDTestIdentifierFactoryImpl implements COVIDTestIdentifierFactory{ + @Override + public COVIDTestIdentifier makeCOVIDTestIdentifier(RegistrationTokenRequest request) { + switch (request.getKeyType()) { + case GUID: + return new GUIDCOVIDTestIdentifier(); + case TELETAN: + return new TeleTANCOVIDTestIdentifier(); + default: + return new UnknownCOVIDTestIdentifier(); + } + } +} diff --git a/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/GUIDCOVIDTestIdentifier.java b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/GUIDCOVIDTestIdentifier.java new file mode 100644 index 00000000..5f114181 --- /dev/null +++ b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/GUIDCOVIDTestIdentifier.java @@ -0,0 +1,52 @@ +/*- + * ---license-start + * Corona-Warn-App / cwa-verification + * --- + * Copyright (C) 2020 - 2022 T-Systems International GmbH and all other contributors + * --- + * 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. + * ---license-end + */ + +package app.coronawarn.verification.covid_test_identifier_factory; + +import app.coronawarn.verification.model.RegistrationToken; +import app.coronawarn.verification.model.RegistrationTokenRequest; +import app.coronawarn.verification.service.AppSessionService; +import app.coronawarn.verification.service.FakeDelayService; +import app.coronawarn.verification.service.TanService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.util.StopWatch; +import org.springframework.web.context.request.async.DeferredResult; + +import java.util.concurrent.ScheduledExecutorService; + +@RequiredArgsConstructor +@Slf4j +public class GUIDCOVIDTestIdentifier extends COVIDTestIdentifier { + + + @Override + public DeferredResult> generateRegistrationToken(RegistrationTokenRequest request, ScheduledExecutorService scheduledExecutor, StopWatch stopWatch, String fake, AppSessionService appSessionService, FakeDelayService fakeDelayService, TanService tanService) { + ResponseEntity responseEntity = + appSessionService.generateRegistrationTokenByGuid(request.getKey(), request.getKeyDob(), fake); + stopWatch.stop(); + fakeDelayService.updateFakeTokenRequestDelay(stopWatch.getTotalTimeMillis()); + DeferredResult> deferredResult = new DeferredResult<>(); + deferredResult.setResult(responseEntity); + log.info("Returning the successfully generated RegistrationToken."); + return deferredResult; + } +} diff --git a/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/TeleTANCOVIDTestIdentifier.java b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/TeleTANCOVIDTestIdentifier.java new file mode 100644 index 00000000..92a44fb3 --- /dev/null +++ b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/TeleTANCOVIDTestIdentifier.java @@ -0,0 +1,73 @@ +/*- + * ---license-start + * Corona-Warn-App / cwa-verification + * --- + * Copyright (C) 2020 - 2022 T-Systems International GmbH and all other contributors + * --- + * 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. + * ---license-end + */ + +package app.coronawarn.verification.covid_test_identifier_factory; + +import app.coronawarn.verification.domain.VerificationTan; +import app.coronawarn.verification.exception.VerificationServerException; +import app.coronawarn.verification.model.RegistrationToken; +import app.coronawarn.verification.model.RegistrationTokenRequest; +import app.coronawarn.verification.service.AppSessionService; +import app.coronawarn.verification.service.FakeDelayService; +import app.coronawarn.verification.service.TanService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.util.StopWatch; +import org.springframework.web.context.request.async.DeferredResult; + +import java.util.Optional; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +@RequiredArgsConstructor +@Slf4j +public class TeleTANCOVIDTestIdentifier extends COVIDTestIdentifier { + + private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(4); + + @Override + public DeferredResult> generateRegistrationToken(RegistrationTokenRequest request, ScheduledExecutorService scheduledExecutor, StopWatch stopWatch, String fake, AppSessionService appSessionService, FakeDelayService fakeDelayService, TanService tanService) { + Optional optional = tanService.getEntityByTan(request.getKey()); + + ResponseEntity response = appSessionService.generateRegistrationTokenByTeleTan( + request.getKey(), + fake, + optional.map(VerificationTan::getTeleTanType).orElse(null)); + + if (optional.isPresent()) { + VerificationTan teleTan = optional.get(); + teleTan.setRedeemed(true); + tanService.saveTan(teleTan); + stopWatch.stop(); + fakeDelayService.updateFakeTokenRequestDelay(stopWatch.getTotalTimeMillis()); + DeferredResult> deferredResult = new DeferredResult<>(); + scheduledExecutor.schedule(() -> deferredResult.setResult(response), fakeDelayService.realDelayToken(), + MILLISECONDS); + log.info("Returning the successfully generated RegistrationToken."); + return deferredResult; + } + stopWatch.stop(); + throw new VerificationServerException(HttpStatus.BAD_REQUEST, "The teleTAN verification failed"); + } +} diff --git a/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/UnknownCOVIDTestIdentifier.java b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/UnknownCOVIDTestIdentifier.java new file mode 100644 index 00000000..abfddc3a --- /dev/null +++ b/src/main/java/app/coronawarn/verification/covid_test_identifier_factory/UnknownCOVIDTestIdentifier.java @@ -0,0 +1,46 @@ +/*- + * ---license-start + * Corona-Warn-App / cwa-verification + * --- + * Copyright (C) 2020 - 2022 T-Systems International GmbH and all other contributors + * --- + * 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. + * ---license-end + */ + +package app.coronawarn.verification.covid_test_identifier_factory; + +import app.coronawarn.verification.exception.VerificationServerException; +import app.coronawarn.verification.model.RegistrationToken; +import app.coronawarn.verification.model.RegistrationTokenRequest; +import app.coronawarn.verification.service.AppSessionService; +import app.coronawarn.verification.service.FakeDelayService; +import app.coronawarn.verification.service.TanService; +import lombok.NoArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.util.StopWatch; +import org.springframework.web.context.request.async.DeferredResult; + +import java.util.concurrent.ScheduledExecutorService; + +@NoArgsConstructor +public class UnknownCOVIDTestIdentifier extends COVIDTestIdentifier { + + @Override + public DeferredResult> generateRegistrationToken(RegistrationTokenRequest request, ScheduledExecutorService scheduledExecutor, StopWatch stopWatch, String fake, AppSessionService appSessionService, FakeDelayService fakeDelayService, TanService tanService) { + stopWatch.stop(); + throw new VerificationServerException(HttpStatus.BAD_REQUEST, + "Unknown registration key type for registration token"); + } +}