-
Notifications
You must be signed in to change notification settings - Fork 14
/
RDSRegistrationStorage.java
278 lines (227 loc) · 10.6 KB
/
RDSRegistrationStorage.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package com.yubicolabs;
import com.amazon.rdsdata.client.RdsDataClient;
import com.amazonaws.services.rdsdata.AWSRDSData;
import com.amazonaws.services.rdsdata.AWSRDSDataClient;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yubico.internal.util.CollectionUtil;
import com.yubico.webauthn.AssertionResult;
import com.yubico.webauthn.CredentialRepository;
import com.yubico.webauthn.RegisteredCredential;
import com.yubico.webauthn.data.ByteArray;
import com.yubico.webauthn.data.PublicKeyCredentialDescriptor;
import com.yubicolabs.data.CredentialRegistration;
import com.yubicolabs.data.RegistrationDTO;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class RDSRegistrationStorage implements RegistrationStorage, CredentialRepository {
private static final String RESOURCE_ARN = System.getenv("DBAuroraClusterArn");
private static final String SECRET_ARN = System.getenv("DBSecretsStoreArn");
private static final String DATABASE = System.getenv("DatabaseName");
private final Clock clock = Clock.systemDefaultZone();
private final Gson gson = new GsonBuilder().create();
private final RdsDataClient client;
public RDSRegistrationStorage() {
AWSRDSData rdsData = AWSRDSDataClient.builder().build();
client = RdsDataClient.builder()
.rdsDataService(rdsData)
.database(DATABASE)
.resourceArn(RESOURCE_ARN)
.secretArn(SECRET_ARN)
.build();
}
private Collection<CredentialRegistration> getByCredentialId(ByteArray credentialId) {
String keyJsonOutput = gson.toJson(credentialId);
final String SQL = "SELECT registration FROM credentialRegistrations WHERE credentialId = :keyJsonOutput";
return client.forSql(SQL)
.withParamSets(new GetParams(keyJsonOutput))
.execute()
.mapToList(RegistrationDTO.class)
.stream()
.map(r -> gson.fromJson(r.registration, CredentialRegistration.class))
.collect(Collectors.toList());
}
private boolean updateRegistration(String username, ByteArray credentialId, CredentialRegistration reg) {
String usernameJsonOutput = gson.toJson(username);
String credentialIdJsonOutput = gson.toJson(credentialId);
String registrationJsonOutput = gson.toJson(reg);
String SQL = "UPDATE credentialRegistrations SET registration= :registrationJsonOutput WHERE username= :usernameJsonOutput AND credentialId= :credentialIdJsonOutput";
client.forSql(SQL)
.withParamSets(new UpdateParams(registrationJsonOutput, usernameJsonOutput, credentialIdJsonOutput))
.execute();
return true;
}
@Override
public void updateCredentialNickname(String username, ByteArray credentialId, String nickname) {
CredentialRegistration registration = getRegistrationByUsernameAndCredentialId(username, credentialId)
.orElseThrow(() -> new NoSuchElementException(String.format(
"Credential \"%s\" is not registered to user \"%s\"",
credentialId, username
)));
updateRegistration(
username,
credentialId,
registration
.withCredentialNickname(Optional.of(nickname))
.withLastUpdatedTime(clock.instant())
);
}
@Override
public boolean addRegistrationByUsername(String username, CredentialRegistration reg) {
String usernameJsonOutput = gson.toJson(username);
String userHandleJsonOutput = gson.toJson(reg.getUserIdentity().getId());
String credentialIdJsonOutput = gson.toJson(reg.getCredential().getCredentialId());
String registrationJsonOutput = gson.toJson(reg);
final String SQL = "INSERT INTO credentialRegistrations (username, userHandle, credentialId, registration) VALUES(:usernameJsonOutput, :userHandleJsonOutput, :credentialIdJsonOutput, :registrationJsonOutput)";
client.forSql(SQL)
.withParamSets(new AddParams(usernameJsonOutput, userHandleJsonOutput, credentialIdJsonOutput, registrationJsonOutput))
.execute();
return true;
}
@Override
public Set<PublicKeyCredentialDescriptor> getCredentialIdsForUsername(String username) {
return getRegistrationsByUsername(username).stream()
.map(registration -> PublicKeyCredentialDescriptor.builder()
.id(registration.getCredential().getCredentialId())
.build())
.collect(Collectors.toSet());
}
@Override
public Collection<CredentialRegistration> getRegistrationsByUsername(String username) {
String keyJsonOutput = gson.toJson(username);
final String SQL = "SELECT registration FROM credentialRegistrations WHERE username = :keyJsonOutput";
return client.forSql(SQL)
.withParamSets(new GetParams(keyJsonOutput))
.execute()
.mapToList(RegistrationDTO.class)
.stream()
.map(r -> gson.fromJson(r.registration, CredentialRegistration.class))
.collect(Collectors.toList());
}
@Override
public Collection<CredentialRegistration> getRegistrationsByUserHandle(ByteArray userHandle) {
String keyJsonOutput = gson.toJson(userHandle);
final String SQL = "SELECT registration FROM credentialRegistrations WHERE userHandle = :keyJsonOutput";
return client.forSql(SQL)
.withParamSets(new GetParams(keyJsonOutput))
.execute()
.mapToList(RegistrationDTO.class)
.stream()
.map(r -> gson.fromJson(r.registration, CredentialRegistration.class))
.collect(Collectors.toList());
}
@Override
public Optional<String> getUsernameForUserHandle(ByteArray userHandle) {
return getRegistrationsByUserHandle(userHandle).stream()
.findAny()
.map(CredentialRegistration::getUsername);
}
@Override
public Optional<ByteArray> getUserHandleForUsername(String username) {
return getRegistrationsByUsername(username).stream()
.findAny()
.map(reg -> reg.getUserIdentity().getId());
}
@Override
public void updateSignatureCount(AssertionResult result) {
CredentialRegistration registration = getRegistrationByUsernameAndCredentialId(result.getUsername(), result.getCredentialId())
.orElseThrow(() -> new NoSuchElementException(String.format(
"Credential \"%s\" is not registered to user \"%s\"",
result.getCredentialId(), result.getUsername()
)));
updateRegistration(
result.getUsername(),
result.getCredentialId(),
registration.withSignatureCount(result.getSignatureCount()).withLastUsedTime(clock.instant())
);
}
@Override
public Optional<CredentialRegistration> getRegistrationByUsernameAndCredentialId(String username, ByteArray id) {
return getRegistrationsByUsername(username).stream()
.filter(credReg -> id.equals(credReg.getCredential().getCredentialId()))
.findFirst();
}
@Override
public boolean removeRegistrationByUsername(String username, CredentialRegistration credentialRegistration) {
String usernameJsonOutput = gson.toJson(username);
String credentialIdJsonOutput = gson.toJson(credentialRegistration.getCredential().getCredentialId());
final String SQL = "DELETE FROM credentialRegistrations WHERE username= :usernameJsonOutput AND credentialId= :credentialIdJsonOutput";
client.forSql(SQL)
.withParamSets(new DeleteRegistrationParams(usernameJsonOutput, credentialIdJsonOutput))
.execute();
return true;
}
@Override
public boolean removeAllRegistrations(String username) {
String usernameJsonOutput = gson.toJson(username);
final String SQL = "DELETE FROM credentialRegistrations WHERE username= :usernameJsonOutput";
client.forSql(SQL)
.withParamSets(new DeleteAllParams(usernameJsonOutput))
.execute();
return true;
}
@Override
public Optional<RegisteredCredential> lookup(ByteArray credentialId, ByteArray userHandle) {
Optional<CredentialRegistration> registrationMaybe = getByCredentialId(credentialId).stream()
.findAny();
log.debug("lookup credential ID: {}, user handle: {}; result: {}", credentialId, userHandle, registrationMaybe);
return registrationMaybe.flatMap(registration ->
Optional.of(
RegisteredCredential.builder()
.credentialId(registration.getCredential().getCredentialId())
.userHandle(registration.getUserIdentity().getId())
.publicKeyCose(registration.getCredential().getPublicKeyCose())
.signatureCount(registration.getSignatureCount())
.build()
)
);
}
@Override
public Set<RegisteredCredential> lookupAll(ByteArray credentialId) {
return CollectionUtil.immutableSet(
getByCredentialId(credentialId).stream()
.map(reg -> RegisteredCredential.builder()
.credentialId(reg.getCredential().getCredentialId())
.userHandle(reg.getUserIdentity().getId())
.publicKeyCose(reg.getCredential().getPublicKeyCose())
.signatureCount(reg.getSignatureCount())
.build()
)
.collect(Collectors.toSet()));
}
@Data
private static class GetParams {
public final String keyJsonOutput;
}
@Data
private static class AddParams {
public final String usernameJsonOutput;
public final String userHandleJsonOutput;
public final String credentialIdJsonOutput;
public final String registrationJsonOutput;
}
@Data
private static class UpdateParams {
public final String registrationJsonOutput;
public final String usernameJsonOutput;
public final String credentialIdJsonOutput;
}
@Data
private static class DeleteRegistrationParams {
public final String usernameJsonOutput;
public final String credentialIdJsonOutput;
}
@Data
private static class DeleteAllParams {
public final String usernameJsonOutput;
}
}