Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Registration token refactor #260

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -103,44 +99,12 @@ public DeferredResult<ResponseEntity<RegistrationToken>> generateRegistrationTok
}
StopWatch stopWatch = new StopWatch();
stopWatch.start();
String key = request.getKey();
RegistrationTokenKeyType keyType = request.getKeyType();
DeferredResult<ResponseEntity<RegistrationToken>> deferredResult = new DeferredResult<>();

switch (keyType) {
case GUID:
ResponseEntity<RegistrationToken> 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<VerificationTan> optional = tanService.getEntityByTan(key);
CovidTestIdentifierFactory covidTestIdentifierFactory = new CovidTestIdentifierFactoryImpl();

ResponseEntity<RegistrationToken> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<ResponseEntity<RegistrationToken>> generateRegistrationToken(
RegistrationTokenRequest request,
ScheduledExecutorService scheduledExecutor,
StopWatch stopWatch,
String fake,
AppSessionService appSessionService,
FakeDelayService fakeDelayService,
TanService tanService);
}
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ResponseEntity<RegistrationToken>> generateRegistrationToken(
RegistrationTokenRequest request,
ScheduledExecutorService scheduledExecutor,
StopWatch stopWatch,
String fake,
AppSessionService appSessionService,
FakeDelayService fakeDelayService,
TanService tanService) {

ResponseEntity<RegistrationToken> responseEntity =
appSessionService.generateRegistrationTokenByGuid(request.getKey(), request.getKeyDob(), fake);
stopWatch.stop();
fakeDelayService.updateFakeTokenRequestDelay(stopWatch.getTotalTimeMillis());
DeferredResult<ResponseEntity<RegistrationToken>> deferredResult = new DeferredResult<>();
deferredResult.setResult(responseEntity);
log.info("Returning the successfully generated RegistrationToken.");
return deferredResult;
}
}
Original file line number Diff line number Diff line change
@@ -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<ResponseEntity<RegistrationToken>> generateRegistrationToken(
RegistrationTokenRequest request,
ScheduledExecutorService scheduledExecutor,
StopWatch stopWatch,
String fake,
AppSessionService appSessionService,
FakeDelayService fakeDelayService,
TanService tanService) {

Optional<VerificationTan> optional = tanService.getEntityByTan(request.getKey());

ResponseEntity<RegistrationToken> 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<ResponseEntity<RegistrationToken>> 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");
}
}
Original file line number Diff line number Diff line change
@@ -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<ResponseEntity<RegistrationToken>> 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");
}
}