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..65629abe 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.factory.CovidTestIdentifier; +import app.coronawarn.verification.factory.CovidTestIdentifierFactory; +import app.coronawarn.verification.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,12 @@ 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 +99,12 @@ 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/factory/CovidTestIdentifier.java b/src/main/java/app/coronawarn/verification/factory/CovidTestIdentifier.java new file mode 100644 index 00000000..e9bae0fb --- /dev/null +++ b/src/main/java/app/coronawarn/verification/factory/CovidTestIdentifier.java @@ -0,0 +1,42 @@ +/*- + * ---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.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 java.util.concurrent.ScheduledExecutorService; +import org.springframework.http.ResponseEntity; +import org.springframework.util.StopWatch; +import org.springframework.web.context.request.async.DeferredResult; + +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/factory/CovidTestIdentifierFactory.java b/src/main/java/app/coronawarn/verification/factory/CovidTestIdentifierFactory.java new file mode 100644 index 00000000..0f185136 --- /dev/null +++ b/src/main/java/app/coronawarn/verification/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.factory; + +import app.coronawarn.verification.model.RegistrationTokenRequest; + +public interface CovidTestIdentifierFactory { + + public CovidTestIdentifier makeCovidTestIdentifier(RegistrationTokenRequest request); + +} diff --git a/src/main/java/app/coronawarn/verification/factory/CovidTestIdentifierFactoryImpl.java b/src/main/java/app/coronawarn/verification/factory/CovidTestIdentifierFactoryImpl.java new file mode 100644 index 00000000..af9e2832 --- /dev/null +++ b/src/main/java/app/coronawarn/verification/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.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/factory/GuidCovidTestIdentifier.java b/src/main/java/app/coronawarn/verification/factory/GuidCovidTestIdentifier.java new file mode 100644 index 00000000..dc4fa00e --- /dev/null +++ b/src/main/java/app/coronawarn/verification/factory/GuidCovidTestIdentifier.java @@ -0,0 +1,59 @@ +/*- + * ---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.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 java.util.concurrent.ScheduledExecutorService; +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; + +@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/factory/TeleTanCovidTestIdentifier.java b/src/main/java/app/coronawarn/verification/factory/TeleTanCovidTestIdentifier.java new file mode 100644 index 00000000..def951c4 --- /dev/null +++ b/src/main/java/app/coronawarn/verification/factory/TeleTanCovidTestIdentifier.java @@ -0,0 +1,80 @@ +/*- + * ---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.factory; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +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 java.util.Optional; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +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; + +@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/factory/UnknownCovidTestIdentifier.java b/src/main/java/app/coronawarn/verification/factory/UnknownCovidTestIdentifier.java new file mode 100644 index 00000000..259d0b5d --- /dev/null +++ b/src/main/java/app/coronawarn/verification/factory/UnknownCovidTestIdentifier.java @@ -0,0 +1,53 @@ +/*- + * ---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.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 java.util.concurrent.ScheduledExecutorService; +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; + +@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"); + } +}