-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: various minor improvements / patches for IATP
- Loading branch information
1 parent
a3ef0f1
commit 3f9316c
Showing
6 changed files
with
153 additions
and
6 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
core/common/util/src/main/java/org/eclipse/edc/util/uri/UriUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.util.uri; | ||
|
||
import java.net.URI; | ||
|
||
public class UriUtils { | ||
/** | ||
* Compares two URIs to check if they are equal after ignoring the fragment part. | ||
* | ||
* @param u1 The first URI to compare. | ||
* @param u2 The second URI to compare. | ||
* @return {@code true} if the URIs are equal after ignoring the fragment part, {@code false} otherwise. | ||
*/ | ||
public static boolean equalsIgnoreFragment(URI u1, URI u2) { | ||
var str1 = stripFragment(u1.toString()); | ||
var str2 = stripFragment(u2.toString()); | ||
|
||
return str1.equals(str2); | ||
} | ||
|
||
/** | ||
* Removes the fragment part from a given string representation of a URI. | ||
* | ||
* @param uri The string representation of the URI. | ||
* @return The string with the fragment part removed, if it exists, otherwise the original string. | ||
*/ | ||
private static String stripFragment(String uri) { | ||
var ix = uri.indexOf("#"); | ||
return ix >= 0 ? uri.substring(0, ix) : uri; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
core/common/util/src/test/java/org/eclipse/edc/util/uri/UriUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.util.uri; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.net.URI; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class UriUtilsTest { | ||
|
||
@Test | ||
void verifyEquality() { | ||
var u1 = URI.create("https://some.url/path/foo#position1"); | ||
var u2 = URI.create("https://some.url/path/foo"); | ||
assertThat(UriUtils.equalsIgnoreFragment(u1, u2)).isTrue(); | ||
|
||
var u3 = URI.create("https://some.url/path"); | ||
assertThat(UriUtils.equalsIgnoreFragment(u1, u3)).isFalse(); | ||
assertThat(UriUtils.equalsIgnoreFragment(u2, u3)).isFalse(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
import com.apicatalog.ld.DocumentError; | ||
import com.apicatalog.ld.signature.SigningError; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.nimbusds.jose.JOSEException; | ||
import com.nimbusds.jose.jwk.Curve; | ||
|
@@ -26,6 +27,7 @@ | |
import com.nimbusds.jose.jwk.gen.ECKeyGenerator; | ||
import com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator; | ||
import jakarta.json.JsonArray; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonValue; | ||
import org.eclipse.edc.jsonld.TitaniumJsonLd; | ||
import org.eclipse.edc.jsonld.spi.JsonLdKeywords; | ||
|
@@ -242,5 +244,58 @@ void signCompactedPresentation() throws JOSEException { | |
assertThat(verificationMethod.getValueType()).describedAs("Expected a String!").isEqualTo(JsonValue.ValueType.ARRAY); | ||
assertThat(verificationMethod.asJsonArray().get(0).asJsonObject().toString()).contains(verificationMethodUrl); | ||
} | ||
|
||
@Test | ||
void signMembership() throws JsonProcessingException, JOSEException { | ||
var json = """ | ||
{ | ||
"@context": [ | ||
"https://www.w3.org/2018/credentials/v1", | ||
{ | ||
"ex": "http://org.yourdataspace.com#", | ||
"schema": "http://schema.org/", | ||
"MembershipCredential": "ex:MembershipCredential", | ||
"membership": "ex:membership", | ||
"since": "ex:since", | ||
"website": "ex:website", | ||
"contact": "ex:contact", | ||
"membershipType": "ex:membershipType" | ||
} | ||
], | ||
"id": "http://org.yourdataspace.com/credentials/1234", | ||
"type": [ | ||
"VerifiableCredential", | ||
"MembershipCredential" | ||
], | ||
"issuer": "did:web:dataspace-issuer", | ||
"issuanceDate": "2023-10-18T00:00:00Z", | ||
"credentialSubject": { | ||
"id": "did:web:connector1", | ||
"membership": { | ||
"membershipType": "FullMember", | ||
"since": "2023-05-08T00:00:00Z", | ||
"website": "www.thatsyourexample.com", | ||
"contact": "[email protected]" | ||
} | ||
} | ||
} | ||
"""; | ||
var jo = mapper.readValue(json, JsonObject.class); | ||
var pem = """ | ||
-----BEGIN EC PRIVATE KEY----- | ||
MHcCAQEEIARDUGJgKy1yzxkueIJ1k3MPUWQ/tbQWQNqW6TjyHpdcoAoGCCqGSM49 | ||
AwEHoUQDQgAE1l0Lof0a1yBc8KXhesAnoBvxZw5roYnkAXuqCYfNK3ex+hMWFuiX | ||
GUxHlzShAehR6wvwzV23bbC0tcFcVgW//A== | ||
-----END EC PRIVATE KEY----- | ||
"""; | ||
var ecKey = ECKey.parseFromPEMEncodedObjects(pem).toECKey(); | ||
var kp = createKeyPair(ecKey); | ||
var signed = issuer.signDocument(jo, kp, jws2020suite.createOptions() | ||
.purpose(URI.create("https://w3id.org/security#assertionMethod")) | ||
.verificationMethod(new JwkMethod(URI.create("did:web:dataspace-issuer#key-1"), null, null, null)) | ||
.created(Instant.now())); | ||
System.out.println(signed.getContent().toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters