This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
AppSessionService.java
189 lines (174 loc) · 6.54 KB
/
AppSessionService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Corona-Warn-App / cwa-verification
*
* (C) 2020, T-Systems International GmbH
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you 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.
*/
package app.coronawarn.verification.service;
import app.coronawarn.verification.domain.VerificationAppSession;
import app.coronawarn.verification.model.AppSessionSourceOfTrust;
import app.coronawarn.verification.model.RegistrationToken;
import app.coronawarn.verification.model.RegistrationTokenKeyType;
import app.coronawarn.verification.repository.VerificationAppSessionRepository;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
/**
* This class represents the VerificationAppSession service.
*/
@Slf4j
@RequiredArgsConstructor
@Component
public class AppSessionService {
/**
* The {@link VerificationAppSessionRepository}.
*/
@NonNull
private final VerificationAppSessionRepository appSessionRepository;
/**
* The {@link HashingService}.
*/
@NonNull
private final HashingService hashingService;
/**
* Creates an AppSession-Entity.
*
* @param registrationToken Token for registration
* @return appSession for registrationToken
*/
public VerificationAppSession generateAppSession(String registrationToken) {
log.info("Create the app session entity with the created registration token.");
VerificationAppSession appSession = new VerificationAppSession();
appSession.setCreatedAt(LocalDateTime.now());
appSession.setUpdatedAt(LocalDateTime.now());
appSession.setRegistrationTokenHash(hashingService.hash(registrationToken));
return appSession;
}
private String generateRegistrationToken() {
return UUID.randomUUID().toString();
}
/**
* This method generates a registration Token by a guid or a teletan.
*
* @param key the guid or teletan
* @param keyType the key type {@link RegistrationTokenKeyType}
* @return an {@link ResponseEntity}
*/
public ResponseEntity<RegistrationToken> generateRegistrationToken(String key, RegistrationTokenKeyType keyType) {
String registrationToken;
VerificationAppSession appSession;
switch (keyType) {
case GUID:
if (checkRegistrationTokenAlreadyExistsForGuid(key)) {
log.warn("The registration token already exists for the hashed guid.");
} else {
log.info("Start generating a new registration token for the given hashed guid.");
registrationToken = generateRegistrationToken();
appSession = generateAppSession(registrationToken);
appSession.setHashedGuid(key);
appSession.setSourceOfTrust(AppSessionSourceOfTrust.HASHED_GUID);
saveAppSession(appSession);
return ResponseEntity
.status(HttpStatus.CREATED)
.body(new RegistrationToken(registrationToken));
}
break;
case TELETAN:
if (checkRegistrationTokenAlreadyExistForTeleTan(key)) {
log.warn("The registration token already exists for this TeleTAN.");
} else {
log.info("Start generating a new registration token for the given tele TAN.");
registrationToken = generateRegistrationToken();
appSession = generateAppSession(registrationToken);
appSession.setTeleTanHash(hashingService.hash(key));
appSession.setSourceOfTrust(AppSessionSourceOfTrust.TELETAN);
saveAppSession(appSession);
return ResponseEntity
.status(HttpStatus.CREATED)
.body(new RegistrationToken(registrationToken));
}
break;
default:
break;
}
return ResponseEntity.badRequest().build();
}
/**
* Persists the specified entity of {@link VerificationAppSession}
* instances.
*
* @param appSession the verification app session entity
*/
public void saveAppSession(VerificationAppSession appSession) {
log.info("Start saveAppSession.");
appSessionRepository.save(appSession);
}
/**
* Get existing VerificationAppSession for Reg Token from
* {@link VerificationAppSessionRepository}.
*
* @param registrationToken the registrationToken
* @return Optional VerificationAppSession
*/
public Optional<VerificationAppSession> getAppSessionByToken(String registrationToken) {
log.info("Start getAppSessionByToken.");
return appSessionRepository.findByRegistrationTokenHash(hashingService.hash(registrationToken));
}
/**
* Check for existing hashed GUID Token in the
* {@link VerificationAppSessionRepository}.
*
* @param hashedGuid the hashed guid
* @return flag for existing guid
*/
public boolean checkRegistrationTokenAlreadyExistsForGuid(String hashedGuid) {
log.info("Start checkRegistrationTokenAlreadyExistsForGuid.");
VerificationAppSession appSession = new VerificationAppSession();
appSession.setHashedGuid(hashedGuid);
return appSessionRepository.exists(Example.of(appSession, ExampleMatcher.matchingAll()));
}
/**
* Check for existing hashed TeleTAN in the
* {@link VerificationAppSessionRepository}.
*
* @param teleTan the teleTAN
* @return flag for existing teleTAN
*/
public boolean checkRegistrationTokenAlreadyExistForTeleTan(String teleTan) {
log.info("Start checkTeleTanAlreadyExistForTeleTan.");
VerificationAppSession appSession = new VerificationAppSession();
appSession.setTeleTanHash(hashingService.hash(teleTan));
return appSessionRepository.exists(Example.of(appSession, ExampleMatcher.matchingAll()));
}
/**
* Verifies the hashed guid.
*
* @param hashedGuid the hashed Guid
* @return flag for verification
*/
public boolean verifyHashedGuid(String hashedGuid) {
return hashingService.isHashValid(hashedGuid);
}
}