Skip to content

Commit 515900a

Browse files
committed
Add method getUsernameForUserHandle to UsernameRepository
1 parent f9a8d73 commit 515900a

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

webauthn-server-core/src/main/java/com/yubico/webauthn/CredentialRepositoryV1ToV2Adapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ public boolean credentialIdExists(ByteArray credentialId) {
3535
public Optional<ByteArray> getUserHandleForUsername(String username) {
3636
return inner.getUserHandleForUsername(username);
3737
}
38+
39+
@Override
40+
public Optional<String> getUsernameForUserHandle(ByteArray userHandle) {
41+
return inner.getUsernameForUserHandle(userHandle);
42+
}
3843
}

webauthn-server-core/src/main/java/com/yubico/webauthn/FinishAssertionSteps.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ final class FinishAssertionSteps<C extends CredentialRecord> {
6060
private final Optional<ByteArray> callerTokenBindingId;
6161
private final Set<String> origins;
6262
private final String rpId;
63-
private final Optional<CredentialRepository> credentialRepository;
6463
private final CredentialRepositoryV2<C> credentialRepositoryV2;
6564
private final Optional<UsernameRepository> usernameRepository;
6665
private final boolean allowOriginPort;
@@ -79,7 +78,6 @@ static FinishAssertionSteps<RegisteredCredential> fromV1(
7978
options.getCallerTokenBindingId(),
8079
rp.getOrigins(),
8180
rp.getIdentity().getId(),
82-
Optional.of(credRepo),
8381
credRepoV2,
8482
Optional.of(credRepoV2),
8583
rp.isAllowOriginPort(),
@@ -95,7 +93,6 @@ static FinishAssertionSteps<RegisteredCredential> fromV1(
9593
options.getCallerTokenBindingId(),
9694
rp.getOrigins(),
9795
rp.getIdentity().getId(),
98-
Optional.empty(),
9996
rp.getCredentialRepository(),
10097
Optional.ofNullable(rp.getUsernameRepository()),
10198
rp.isAllowOriginPort(),
@@ -105,7 +102,7 @@ static FinishAssertionSteps<RegisteredCredential> fromV1(
105102
}
106103

107104
private Optional<String> getUsernameForUserHandle(final ByteArray userHandle) {
108-
return credentialRepository.flatMap(credRepo -> credRepo.getUsernameForUserHandle(userHandle));
105+
return usernameRepository.flatMap(unameRepo -> unameRepo.getUsernameForUserHandle(userHandle));
109106
}
110107

111108
public Step5 begin() {
@@ -262,7 +259,7 @@ public void validate() {
262259
finalUserHandle.get(),
263260
response.getId());
264261

265-
if (credentialRepository.isPresent()) {
262+
if (usernameRepository.isPresent()) {
266263
assertTrue(
267264
finalUsername.isPresent(),
268265
"Unknown username for user handle: %s",

webauthn-server-core/src/main/java/com/yubico/webauthn/UsernameRepository.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,20 @@
3535
public interface UsernameRepository {
3636

3737
/**
38-
* Get the user handle corresponding to the given username.
38+
* Get the user handle corresponding to the given username - the inverse of {@link
39+
* #getUsernameForUserHandle(ByteArray)}.
3940
*
4041
* <p>Used to look up the user handle based on the username, for authentication ceremonies where
4142
* the username is already given.
4243
*/
4344
Optional<ByteArray> getUserHandleForUsername(String username);
45+
46+
/**
47+
* Get the username corresponding to the given user handle - the inverse of {@link
48+
* #getUserHandleForUsername(String)}.
49+
*
50+
* <p>Used to look up the username based on the user handle, for username-less authentication
51+
* ceremonies.
52+
*/
53+
Optional<String> getUsernameForUserHandle(ByteArray userHandle);
4454
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,17 @@ public Optional<ByteArray> getUserHandleForUsername(String username) {
105105
.map(reg -> reg.getUserIdentity().getId());
106106
}
107107

108-
////////////////////////////////////////////////////////////////////////////////
109-
// The following methods are specific to this demo application.
110-
////////////////////////////////////////////////////////////////////////////////
111-
108+
@Override
112109
public Optional<String> getUsernameForUserHandle(ByteArray userHandle) {
113110
return getRegistrationsByUserHandle(userHandle).stream()
114111
.findAny()
115112
.map(CredentialRegistration::getUsername);
116113
}
117114

115+
////////////////////////////////////////////////////////////////////////////////
116+
// The following methods are specific to this demo application.
117+
////////////////////////////////////////////////////////////////////////////////
118+
118119
public boolean addRegistrationByUsername(String username, CredentialRegistration reg) {
119120
try {
120121
return storage.get(username, HashSet::new).add(reg);

0 commit comments

Comments
 (0)