Skip to content

Commit

Permalink
fix: use correct Regex when parsing DID (#3845)
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger authored Feb 9, 2024
1 parent 8569352 commit 8bcbeeb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class DidPublicKeyResolverImpl extends AbstractPublicKeyResolver implemen
* Group 1 ("did") = the did:method:identifier portion
* Group 2 ("fragment") = the #fragment portion
*/
private static final Pattern PATTERN_DID_WITH_OPTIONAL_FRAGMENT = Pattern.compile("(?<did>did:.*:[^#]*)(?<fragment>#.*)?");
private static final Pattern PATTERN_DID_WITH_OPTIONAL_FRAGMENT = Pattern.compile("(?<did>did:.*:[^#]*)((#)(?<fragment>.*))?");
private static final String GROUP_DID = "did";
private static final String GROUP_FRAGMENT = "fragment";
private final DidResolverRegistry resolverRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

class DidPublicKeyResolverImplTest {

public static final String KEYID = "#my-key1";
public static final String KEYID = "my-key1";
private static final String DID_URL = "did:web:example.com";
private final DidResolverRegistry resolverRegistry = mock();
private final KeyParserRegistry keyParserRegistry = mock();
Expand Down Expand Up @@ -100,18 +100,18 @@ void resolve() {
var didDocument = createDidDocument();
when(resolverRegistry.resolve(DID_URL)).thenReturn(Result.success(didDocument));

var result = resolver.resolveKey(DID_URL + KEYID);
var result = resolver.resolveKey(DID_URL + "#" + KEYID);

assertThat(result).isSucceeded().isNotNull();
verify(resolverRegistry).resolve(DID_URL);
}

@Test
void resolve_withVerificationMethodUrlAsId() throws IOException, JOSEException {
var didDocument = createDidDocument(DID_URL + KEYID);
void resolve_withVerificationMethodUrlAsId() {
var didDocument = createDidDocument(DID_URL + "#" + KEYID);
when(resolverRegistry.resolve(DID_URL)).thenReturn(Result.success(didDocument));

var result = resolver.resolveKey(DID_URL + KEYID);
var result = resolver.resolveKey(DID_URL + "#" + KEYID);

assertThat(result).isSucceeded().isNotNull();
verify(resolverRegistry).resolve(DID_URL);
Expand All @@ -121,7 +121,7 @@ void resolve_withVerificationMethodUrlAsId() throws IOException, JOSEException {
void resolve_didNotFound() {
when(resolverRegistry.resolve(DID_URL)).thenReturn(Result.failure("Not found"));

var result = resolver.resolveKey(DID_URL + KEYID);
var result = resolver.resolveKey(DID_URL + "#" + KEYID);

assertThat(result).isFailed();
verify(resolverRegistry).resolve(DID_URL);
Expand All @@ -133,20 +133,20 @@ void resolve_didDoesNotContainPublicKey() {
didDocument.getVerificationMethod().clear();
when(resolverRegistry.resolve(DID_URL)).thenReturn(Result.success(didDocument));

var result = resolver.resolveKey(DID_URL + KEYID);
var result = resolver.resolveKey(DID_URL + "#" + KEYID);

assertThat(result).isFailed();
verify(resolverRegistry).resolve(DID_URL);
}

@Test
void resolve_didContainsMultipleKeysWithSameKeyId() throws JOSEException, IOException {
void resolve_didContainsMultipleKeysWithSameKeyId() {
var vm = createVerificationMethod(KEYID);
var vm1 = createVerificationMethod(KEYID);
var didDocument = createDidDocumentBuilder(KEYID).verificationMethod(List.of(vm, vm1)).build();
when(resolverRegistry.resolve(DID_URL)).thenReturn(Result.success(didDocument));

var result = resolver.resolveKey(DID_URL + KEYID);
var result = resolver.resolveKey(DID_URL + "#" + KEYID);

assertThat(result).isFailed()
.detail().contains("Every verification method must have a unique ID");
Expand All @@ -155,13 +155,13 @@ void resolve_didContainsMultipleKeysWithSameKeyId() throws JOSEException, IOExce

@Test
void resolve_didContainsMultipleKeysWithSameKeyId_withRelativeAndFullUrl() {
var vm = createVerificationMethod(DID_URL + KEYID);
var vm = createVerificationMethod(DID_URL + "#" + KEYID);
var vm1 = createVerificationMethod(KEYID);

var didDocument = createDidDocumentBuilder(KEYID).verificationMethod(List.of(vm, vm1)).build();
when(resolverRegistry.resolve(DID_URL)).thenReturn(Result.success(didDocument));

var result = resolver.resolveKey(DID_URL + KEYID);
var result = resolver.resolveKey(DID_URL + "#" + KEYID);

assertThat(result).isFailed()
.detail().contains("Every verification method must have a unique ID");
Expand Down

0 comments on commit 8bcbeeb

Please sign in to comment.