11package ch .admin .bj .swiyu .didtoolbox .model ;
22
3- import ch .admin .bj .swiyu .didtoolbox .JwkUtils ;
43import com .google .gson .JsonObject ;
54import com .google .gson .JsonParser ;
65import com .google .gson .JsonSyntaxException ;
76
87import java .io .IOException ;
98import java .nio .file .Path ;
109import java .security .interfaces .ECPublicKey ;
11- import java .security .spec .InvalidKeySpecException ;
1210import java .util .Objects ;
1311
1412/**
1816 * or authorize interactions with the <a href="https://www.w3.org/TR/did-1.0/#dfn-did-subjects">DID subject</a> or associated parties.
1917 * <p>
2018 * The interface also features a several convenient static factory methods focusing on standard Java types typically used for the purpose
21- * of holding EC/P-256 public keys e.g. {@link ECPublicKey}, {@link Path} or {@link String}.
19+ * of holding public EC public keys e.g. {@link ECPublicKey}, {@link Path} or {@link String}.
20+ *
21+ * @since 1.9.0
2222 */
2323public interface VerificationMethod {
2424
25+ /**
26+ * The string representation of the
27+ * <a href="https://www.w3.org/TR/did-1.0/#dfn-verification-method">verification method</a> type behind
28+ * <a href="https://w3c-ccg.github.io/lds-jws2020/#json-web-key-2020">JsonWebKey2020</a>, which is
29+ * the type of the verification method for the signature suite {@code JsonWebSignature2020}.
30+ */
31+ String VM_TYPE_JSON_WEB_KEY_2020 = "JsonWebKey2020" ;
32+
2533 /**
2634 * Yet another static factory method of the interface.
2735 * <p>
28- * Assuming the supplied {@code publicKeyJwk} represents a proper EC/P-256 <a href="https://www.rfc-editor.org/rfc/rfc7517">JSON Web Key (JWK)</a>,
29- * a valid {@link VerificationMethod} object is returned of type <a href="https://w3c-ccg.github.io/lds-jws2020/">JsonWebKey2020</a>.
36+ * Assuming the supplied {@code publicKeyJwk} represents a proper <a href="https://www.rfc-editor.org/rfc/rfc7517">JSON Web Key (JWK)</a>
37+ * featuring <a href="https://www.rfc-editor.org/rfc/rfc7517#section-4.1">"kty" (Key Type)</a>, {@code crv}, {@code x} and {@code y} parameters,
38+ * a valid {@link VerificationMethod} implementation object is returned featuring
39+ * {@link VerificationMethod#getVerificationMaterial()} method that always returns a valid
40+ * {@link VerificationMaterial} implementation object of type {@code type}
41+ * (e.g. {@link #VM_TYPE_JSON_WEB_KEY_2020}).
3042 *
3143 * @param kid non-empty string representing a <a href="https://www.rfc-editor.org/rfc/rfc7517#section-4.5">"kid" (Key ID) Parameter</a>
44+ * @param type string representation of a <a href="https://www.w3.org/TR/did-1.0/#dfn-verification-method">verification method</a> type
45+ * (e.g. <{@link #VM_TYPE_JSON_WEB_KEY_2020})
3246 * @param publicKeyJwk string representation of a <a href="https://www.rfc-editor.org/rfc/rfc7517">JSON Web Key (JWK)</a>
3347 * @return a valid {@link VerificationMethod} implementation object, never {@code null}
3448 * @throws VerificationMethodException if the supplied {@code publicKeyJwk} does not represent a proper
35- * <a href="https://www.rfc-editor.org/rfc/rfc7517">JSON Web Key (JWK)</a>
49+ * <a href="https://www.rfc-editor.org/rfc/rfc7517">JSON Web Key (JWK)</a> as described above
3650 */
37- static VerificationMethod of (String kid , String publicKeyJwk ) throws VerificationMethodException {
51+ static VerificationMethod of (String kid , String type , String publicKeyJwk ) throws VerificationMethodException {
3852
3953 JsonObject jsonObj ;
4054 try {
@@ -60,7 +74,7 @@ public String getIdFragment() {
6074
6175 @ Override
6276 public String getType () {
63- return "JsonWebKey2020" ;
77+ return type ;
6478 }
6579
6680 @ Override
@@ -71,11 +85,6 @@ public String getPublicKeyJwk() {
7185 jsonObj .addProperty ("kid" , kid );
7286 return jsonObj .toString ();
7387 }
74-
75- @ Override
76- public String getPublicKeyMultibase () {
77- return null ;
78- }
7988 };
8089 }
8190
@@ -94,33 +103,100 @@ public int hashCode() {
94103 /**
95104 * Yet another static factory method of the interface.
96105 * <p>
97- * Assuming the supplied {@code pemPath} denotes a file featuring a proper EC/P-256 public key,
98- * a valid {@link VerificationMethod} object is returned of type <a href="https://w3c-ccg.github.io/lds-jws2020/">JsonWebKey2020</a>.
106+ * It is nothing but a variant of the {@link #of(String, String, String)}
107+ * static factory method having {@link #VM_TYPE_JSON_WEB_KEY_2020} as {@code type}.
108+ *
109+ * @see #of(String, String, String)
110+ * @see #VM_TYPE_JSON_WEB_KEY_2020
111+ */
112+ static VerificationMethod of (String kid , String publicKeyJwk ) throws VerificationMethodException {
113+ return VerificationMethod .of (kid , VM_TYPE_JSON_WEB_KEY_2020 , publicKeyJwk );
114+ }
115+
116+ /**
117+ * Yet another static factory method of the interface.
118+ * <p>
119+ * Assuming the supplied {@code ecPublicKeyPemPath} denotes a file featuring a proper (PEM-encoded) public EC key,
120+ * a valid {@link VerificationMethod} implementation object is returned featuring
121+ * {@link VerificationMethod#getVerificationMaterial()} method that always returns a valid
122+ * {@link VerificationMaterial} implementation object of type {@code type}
123+ * (e.g. {@link #VM_TYPE_JSON_WEB_KEY_2020}).
99124 *
100- * @param kid non-empty string representing a <a href="https://www.rfc-editor.org/rfc/rfc7517#section-4.5">"kid" (Key ID) Parameter</a>
101- * @param pemPath file featuring a proper EC/P-256 public key in PEM format
125+ * @param kid non-empty string representing a <a href="https://www.rfc-editor.org/rfc/rfc7517#section-4.5">"kid" (Key ID) Parameter</a>
126+ * @param type string representation of a <a href="https://www.w3.org/TR/did-1.0/#dfn-verification-method">verification method</a> type
127+ * (e.g. <{@link #VM_TYPE_JSON_WEB_KEY_2020})
128+ * @param ecPublicKeyPemPath file featuring a proper public EC key in PEM format
102129 * @return a valid {@link VerificationMethod} implementation object, never {@code null}
103- * @throws VerificationMethodException if the supplied {@code pemPath} does not feature a proper EC/P-256 public key in PEM format
130+ * @throws VerificationMethodException if the supplied {@code ecPublicKeyPemPath} does not feature a proper public EC key in PEM format
131+ * @see #of(String, String, String)
104132 */
105- static VerificationMethod of (String kid , Path pemPath ) throws VerificationMethodException {
133+ static VerificationMethod of (String kid , String type , Path ecPublicKeyPemPath ) throws VerificationMethodException {
134+
135+ VerificationMaterial vm ;
106136 try {
107- return VerificationMethod .of (kid , JwkUtils . loadECPublicJWKasJSON ( pemPath , kid ) );
108- } catch (IOException | InvalidKeySpecException exc ) {
137+ vm = VerificationMaterial .of (kid , ecPublicKeyPemPath );
138+ } catch (IOException exc ) {
109139 throw new VerificationMethodException (exc );
110140 }
141+
142+ return new VerificationMethod () {
143+ @ Override
144+ public String getIdFragment () {
145+ return kid ;
146+ }
147+
148+ @ Override
149+ public String getType () {
150+ return type ;
151+ }
152+
153+ @ Override
154+ public VerificationMaterial getVerificationMaterial () {
155+ return vm ;
156+ }
157+
158+ @ Override
159+ public boolean equals (Object obj ) {
160+ return this .defaultEquals (obj );
161+ }
162+
163+ @ Override
164+ public int hashCode () {
165+ return Objects .hash (this .getIdFragment ());
166+ }
167+ };
111168 }
112169
113170 /**
114171 * Yet another static factory method of the interface.
115172 * <p>
116- * Assuming the supplied {@code publicKeyJwk} represents a valid EC/P-256 public key,
117- * a valid {@link VerificationMethod} object is returned of type <a href="https://w3c-ccg.github.io/lds-jws2020/">JsonWebKey2020</a>.
173+ * It is nothing but a variant of the {@link #of(String, String, Path)}
174+ * static factory method having {@link #VM_TYPE_JSON_WEB_KEY_2020} as {@code type}.
175+ *
176+ * @see #of(String, String, Path)
177+ * @see #VM_TYPE_JSON_WEB_KEY_2020
178+ */
179+ static VerificationMethod of (String kid , Path pemPath ) throws VerificationMethodException {
180+ return VerificationMethod .of (kid , VM_TYPE_JSON_WEB_KEY_2020 , pemPath );
181+ }
182+
183+ /**
184+ * Yet another static factory method of the interface.
185+ * <p>
186+ * For the supplied public EC key {@code ecPublicKey},
187+ * a valid {@link VerificationMethod} implementation object is returned featuring
188+ * {@link VerificationMethod#getVerificationMaterial()} method that always returns a valid
189+ * {@link VerificationMaterial} implementation object of type {@code type}
190+ * (e.g. {@link #VM_TYPE_JSON_WEB_KEY_2020}).
118191 *
119192 * @param kid non-empty string representing a <a href="https://www.rfc-editor.org/rfc/rfc7517#section-4.5">"kid" (Key ID) Parameter</a>
120- * @param ecPublicKey valid EC/P-256 public key
193+ * @param type string representation of a <a href="https://www.w3.org/TR/did-1.0/#dfn-verification-method">verification method</a> type
194+ * (e.g. <{@link #VM_TYPE_JSON_WEB_KEY_2020})
195+ * @param ecPublicKey valid public EC public key
121196 * @return a valid {@link VerificationMethod} implementation object, never {@code null}
197+ * @see VerificationMaterial#of(String, ECPublicKey)
122198 */
123- static VerificationMethod of (String kid , ECPublicKey ecPublicKey ) {
199+ static VerificationMethod of (String kid , String type , ECPublicKey ecPublicKey ) {
124200 return new VerificationMethod () {
125201 @ Override
126202 public String getIdFragment () {
@@ -129,7 +205,7 @@ public String getIdFragment() {
129205
130206 @ Override
131207 public String getType () {
132- return "JsonWebKey2020" ;
208+ return type ;
133209 }
134210
135211 @ Override
@@ -149,6 +225,19 @@ public int hashCode() {
149225 };
150226 }
151227
228+ /**
229+ * Yet another static factory method of the interface.
230+ * <p>
231+ * It is nothing but a variant of the {@link #of(String, String, ECPublicKey)}
232+ * static factory method having {@link #VM_TYPE_JSON_WEB_KEY_2020} as {@code type}.
233+ *
234+ * @see #of(String, String, ECPublicKey)
235+ * @see #VM_TYPE_JSON_WEB_KEY_2020
236+ */
237+ static VerificationMethod of (String kid , ECPublicKey ecPublicKey ) {
238+ return VerificationMethod .of (kid , VM_TYPE_JSON_WEB_KEY_2020 , ecPublicKey );
239+ }
240+
152241 /**
153242 * As <a href="https://www.w3.org/TR/did-1.0/#dfn-verificationmethod">specified</a>
154243 * and w.r.t. <a href="https://www.rfc-editor.org/rfc/rfc3986#section-3.5">RFC3986</a>
0 commit comments