Skip to content

Commit c6aed4c

Browse files
committed
Delete interface RegistrationStorage and highlight CredentialRepository methods in demo
1 parent f00b469 commit c6aed4c

File tree

4 files changed

+76
-126
lines changed

4 files changed

+76
-126
lines changed

webauthn-server-demo/src/main/java/demo/webauthn/InMemoryRegistrationStorage.java

Lines changed: 73 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,16 @@
4444
import org.slf4j.Logger;
4545
import org.slf4j.LoggerFactory;
4646

47-
public class InMemoryRegistrationStorage implements RegistrationStorage, CredentialRepository {
47+
public class InMemoryRegistrationStorage implements CredentialRepository {
4848

4949
private final Cache<String, Set<CredentialRegistration>> storage =
5050
CacheBuilder.newBuilder().maximumSize(1000).expireAfterAccess(1, TimeUnit.DAYS).build();
5151

5252
private Logger logger = LoggerFactory.getLogger(InMemoryRegistrationStorage.class);
5353

54-
@Override
55-
public boolean addRegistrationByUsername(String username, CredentialRegistration reg) {
56-
try {
57-
return storage.get(username, HashSet::new).add(reg);
58-
} catch (ExecutionException e) {
59-
logger.error("Failed to add registration", e);
60-
throw new RuntimeException(e);
61-
}
62-
}
54+
////////////////////////////////////////////////////////////////////////////////
55+
// The following methods are required by the CredentialRepository interface.
56+
////////////////////////////////////////////////////////////////////////////////
6357

6458
@Override
6559
public Set<PublicKeyCredentialDescriptor> getCredentialIdsForUsername(String username) {
@@ -74,6 +68,73 @@ public Set<PublicKeyCredentialDescriptor> getCredentialIdsForUsername(String use
7468
}
7569

7670
@Override
71+
public Optional<String> getUsernameForUserHandle(ByteArray userHandle) {
72+
return getRegistrationsByUserHandle(userHandle).stream()
73+
.findAny()
74+
.map(CredentialRegistration::getUsername);
75+
}
76+
77+
@Override
78+
public Optional<ByteArray> getUserHandleForUsername(String username) {
79+
return getRegistrationsByUsername(username).stream()
80+
.findAny()
81+
.map(reg -> reg.getUserIdentity().getId());
82+
}
83+
84+
@Override
85+
public Optional<RegisteredCredential> lookup(ByteArray credentialId, ByteArray userHandle) {
86+
Optional<CredentialRegistration> registrationMaybe =
87+
storage.asMap().values().stream()
88+
.flatMap(Collection::stream)
89+
.filter(credReg -> credentialId.equals(credReg.getCredential().getCredentialId()))
90+
.findAny();
91+
92+
logger.debug(
93+
"lookup credential ID: {}, user handle: {}; result: {}",
94+
credentialId,
95+
userHandle,
96+
registrationMaybe);
97+
return registrationMaybe.flatMap(
98+
registration ->
99+
Optional.of(
100+
RegisteredCredential.builder()
101+
.credentialId(registration.getCredential().getCredentialId())
102+
.userHandle(registration.getUserIdentity().getId())
103+
.publicKeyCose(registration.getCredential().getPublicKeyCose())
104+
.signatureCount(registration.getCredential().getSignatureCount())
105+
.build()));
106+
}
107+
108+
@Override
109+
public Set<RegisteredCredential> lookupAll(ByteArray credentialId) {
110+
return CollectionUtil.immutableSet(
111+
storage.asMap().values().stream()
112+
.flatMap(Collection::stream)
113+
.filter(reg -> reg.getCredential().getCredentialId().equals(credentialId))
114+
.map(
115+
reg ->
116+
RegisteredCredential.builder()
117+
.credentialId(reg.getCredential().getCredentialId())
118+
.userHandle(reg.getUserIdentity().getId())
119+
.publicKeyCose(reg.getCredential().getPublicKeyCose())
120+
.signatureCount(reg.getCredential().getSignatureCount())
121+
.build())
122+
.collect(Collectors.toSet()));
123+
}
124+
125+
////////////////////////////////////////////////////////////////////////////////
126+
// The following methods are specific to this demo application.
127+
////////////////////////////////////////////////////////////////////////////////
128+
129+
public boolean addRegistrationByUsername(String username, CredentialRegistration reg) {
130+
try {
131+
return storage.get(username, HashSet::new).add(reg);
132+
} catch (ExecutionException e) {
133+
logger.error("Failed to add registration", e);
134+
throw new RuntimeException(e);
135+
}
136+
}
137+
77138
public Collection<CredentialRegistration> getRegistrationsByUsername(String username) {
78139
try {
79140
return storage.get(username, HashSet::new);
@@ -83,7 +144,6 @@ public Collection<CredentialRegistration> getRegistrationsByUsername(String user
83144
}
84145
}
85146

86-
@Override
87147
public Collection<CredentialRegistration> getRegistrationsByUserHandle(ByteArray userHandle) {
88148
return storage.asMap().values().stream()
89149
.flatMap(Collection::stream)
@@ -93,21 +153,6 @@ public Collection<CredentialRegistration> getRegistrationsByUserHandle(ByteArray
93153
.collect(Collectors.toList());
94154
}
95155

96-
@Override
97-
public Optional<String> getUsernameForUserHandle(ByteArray userHandle) {
98-
return getRegistrationsByUserHandle(userHandle).stream()
99-
.findAny()
100-
.map(CredentialRegistration::getUsername);
101-
}
102-
103-
@Override
104-
public Optional<ByteArray> getUserHandleForUsername(String username) {
105-
return getRegistrationsByUsername(username).stream()
106-
.findAny()
107-
.map(reg -> reg.getUserIdentity().getId());
108-
}
109-
110-
@Override
111156
public void updateSignatureCount(AssertionResult result) {
112157
CredentialRegistration registration =
113158
getRegistrationByUsernameAndCredentialId(result.getUsername(), result.getCredentialId())
@@ -127,7 +172,6 @@ public void updateSignatureCount(AssertionResult result) {
127172
.build()));
128173
}
129174

130-
@Override
131175
public Optional<CredentialRegistration> getRegistrationByUsernameAndCredentialId(
132176
String username, ByteArray id) {
133177
try {
@@ -140,7 +184,6 @@ public Optional<CredentialRegistration> getRegistrationByUsernameAndCredentialId
140184
}
141185
}
142186

143-
@Override
144187
public boolean removeRegistrationByUsername(
145188
String username, CredentialRegistration credentialRegistration) {
146189
try {
@@ -151,50 +194,12 @@ public boolean removeRegistrationByUsername(
151194
}
152195
}
153196

154-
@Override
155197
public boolean removeAllRegistrations(String username) {
156198
storage.invalidate(username);
157199
return true;
158200
}
159201

160-
@Override
161-
public Optional<RegisteredCredential> lookup(ByteArray credentialId, ByteArray userHandle) {
162-
Optional<CredentialRegistration> registrationMaybe =
163-
storage.asMap().values().stream()
164-
.flatMap(Collection::stream)
165-
.filter(credReg -> credentialId.equals(credReg.getCredential().getCredentialId()))
166-
.findAny();
167-
168-
logger.debug(
169-
"lookup credential ID: {}, user handle: {}; result: {}",
170-
credentialId,
171-
userHandle,
172-
registrationMaybe);
173-
return registrationMaybe.flatMap(
174-
registration ->
175-
Optional.of(
176-
RegisteredCredential.builder()
177-
.credentialId(registration.getCredential().getCredentialId())
178-
.userHandle(registration.getUserIdentity().getId())
179-
.publicKeyCose(registration.getCredential().getPublicKeyCose())
180-
.signatureCount(registration.getCredential().getSignatureCount())
181-
.build()));
182-
}
183-
184-
@Override
185-
public Set<RegisteredCredential> lookupAll(ByteArray credentialId) {
186-
return CollectionUtil.immutableSet(
187-
storage.asMap().values().stream()
188-
.flatMap(Collection::stream)
189-
.filter(reg -> reg.getCredential().getCredentialId().equals(credentialId))
190-
.map(
191-
reg ->
192-
RegisteredCredential.builder()
193-
.credentialId(reg.getCredential().getCredentialId())
194-
.userHandle(reg.getUserIdentity().getId())
195-
.publicKeyCose(reg.getCredential().getPublicKeyCose())
196-
.signatureCount(reg.getCredential().getSignatureCount())
197-
.build())
198-
.collect(Collectors.toSet()));
202+
public boolean userExists(String username) {
203+
return !getRegistrationsByUsername(username).isEmpty();
199204
}
200205
}

webauthn-server-demo/src/main/java/demo/webauthn/RegistrationStorage.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

webauthn-server-demo/src/main/java/demo/webauthn/WebAuthnServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public class WebAuthnServer {
113113

114114
private final Cache<ByteArray, AssertionRequestWrapper> assertRequestStorage;
115115
private final Cache<ByteArray, RegistrationRequest> registerRequestStorage;
116-
private final RegistrationStorage userStorage;
116+
private final InMemoryRegistrationStorage userStorage;
117117
private final SessionManager sessions = new SessionManager();
118118

119119
private final TrustResolver trustResolver =
@@ -144,7 +144,7 @@ public WebAuthnServer() throws InvalidAppIdException, CertificateException {
144144
}
145145

146146
public WebAuthnServer(
147-
RegistrationStorage userStorage,
147+
InMemoryRegistrationStorage userStorage,
148148
Cache<ByteArray, RegistrationRequest> registerRequestStorage,
149149
Cache<ByteArray, AssertionRequestWrapper> assertRequestStorage,
150150
RelyingPartyIdentity rpIdentity,

webauthn-server-demo/src/test/scala/demo/webauthn/WebAuthnServerSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class WebAuthnServerSpec
330330
testData: RegistrationTestData,
331331
origins: java.util.Set[String] = origins,
332332
) = {
333-
val userStorage: RegistrationStorage = makeUserStorage(testData)
333+
val userStorage: InMemoryRegistrationStorage = makeUserStorage(testData)
334334

335335
new WebAuthnServer(
336336
userStorage,

0 commit comments

Comments
 (0)