Skip to content

Commit

Permalink
fix: make project pass lint and ci
Browse files Browse the repository at this point in the history
Signed-off-by: Josef Andersson <[email protected]>
  • Loading branch information
janderssonse committed Feb 14, 2025
1 parent 9632a4e commit 9d796bd
Show file tree
Hide file tree
Showing 22 changed files with 354 additions and 138 deletions.
2 changes: 1 addition & 1 deletion development/codequality.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ commit() {

format() {
print_header 'FORMATTING (PRETTIER)'
mvn prettier:write "${MAVEN_CLI_OPTS[@]}" -Dcode-quality -DskipTests -Dprettier.nodePath="$(which node)" -Dprettier.npmPath="$(which npm)" -s development/settings.xml
mvn prettier:write "${MAVEN_CLI_OPTS[@]}" -Dcode-quality -DskipTests -Dprettier.nodePath="$(which node)" -Dprettier.npmPath="$(which npm)" -s development/settings.xml
store_exit_code "$?" "Format" "${MISSING} ${RED}Format check failed, see logs (std out) and fix problems.${NC}\n" "${GREEN}${CHECKMARK}${CHECKMARK} Format check passed${NC}\n"
printf '\n\n'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package se.digg.wallet.datatypes.common;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -45,7 +44,9 @@ public static TokenDigestAlgorithm fromMdlName(String mdlName)
tokenDigestAlgorithm.getMdlName().equalsIgnoreCase(mdlName)
)
.findFirst()
.orElseThrow(() -> new NoSuchAlgorithmException("Unsupported mDL hash algorithm"));
.orElseThrow(
() -> new NoSuchAlgorithmException("Unsupported mDL hash algorithm")
);
}

/**
Expand All @@ -55,14 +56,16 @@ public static TokenDigestAlgorithm fromMdlName(String mdlName)
* @return the corresponding {@code TokenDigestAlgorithm} instance.
* @throws NoSuchAlgorithmException if the provided SD-JWT hash algorithm name is not supported.
*/
public static TokenDigestAlgorithm fromSdJwtName(String sdJwtName) throws NoSuchAlgorithmException {
public static TokenDigestAlgorithm fromSdJwtName(String sdJwtName)
throws NoSuchAlgorithmException {
return Arrays.stream(values())
.filter(
tokenDigestAlgorithm ->
tokenDigestAlgorithm.getSdJwtName().equalsIgnoreCase(sdJwtName)
)
.findFirst()
.orElseThrow(() -> new NoSuchAlgorithmException("Unsupported SD-JWT hash algorithm"));
.orElseThrow(
() -> new NoSuchAlgorithmException("Unsupported SD-JWT hash algorithm")
);
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
//
// SPDX-License-Identifier: EUPL-1.2

package se.digg.wallet.datatypes.common;

import java.io.Serial;
Expand All @@ -9,19 +13,18 @@
* parsing failures, or other errors encountered while processing a token.
*/
public class TokenParsingException extends Exception {

@Serial
private static final long serialVersionUID = -150091799709439631L;


/**
* Default constructor for the TokenParsingException class.
*
* This constructor creates a new instance of TokenParsingException without any
* specific error message or cause. It is typically used when there is a token
* parsing error that does not require additional context.
*/
public TokenParsingException() {
}
public TokenParsingException() {}

/**
* Constructs a new TokenParsingException with the specified detail message.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
//
// SPDX-License-Identifier: EUPL-1.2

package se.digg.wallet.datatypes.common;

import java.io.Serial;
Expand All @@ -11,6 +15,7 @@
* for specifying detailed error messages and causes.
*/
public class TokenPresentationException extends Exception {

@Serial
private static final long serialVersionUID = 942635978985209161L;

Expand All @@ -20,8 +25,7 @@ public class TokenPresentationException extends Exception {
* specific message or cause, indicating a generic error during the token
* presentation process.
*/
public TokenPresentationException() {
}
public TokenPresentationException() {}

/**
* Constructs a new TokenPresentationException with the specified detail message.
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/se/digg/wallet/datatypes/mdl/data/CBORUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import se.digg.cose.*;
import se.digg.cose.AlgorithmID;
import se.digg.cose.Attribute;
import se.digg.cose.COSEKey;
import se.digg.cose.CoseException;
import se.digg.cose.HeaderKeys;
import se.digg.cose.Sign1COSEObject;

/**
* Utility class for handling CBOR (Concise Binary Object Representation) encoding,
Expand All @@ -32,6 +37,7 @@
* data into JSON or other formats. The utility also provides a signing mechanism
* for CBOR data using a COSE-based signing process.
*/
@SuppressWarnings("PMD.CollapsibleIfStatements")
@Slf4j
public class CBORUtils {

Expand All @@ -42,8 +48,7 @@ public class CBORUtils {
*
* This class is not intended to be instantiated. It provides only static utility methods for usage.
*/
private CBORUtils() {
}
private CBORUtils() {}

/** ObjectMapper for parsing serializing objects to CBOR */
public static final ObjectMapper CBOR_MAPPER;
Expand Down Expand Up @@ -196,7 +201,14 @@ public static String cborToPrettyJson(byte[] cborBytes) throws IOException {
* @throws CoseException if an error occurs during the COSE signing process
* @throws CertificateEncodingException if an encoding error occurs with the provided certificates
*/
public static Sign1COSEObject sign(byte[] toBeSigned, COSEKey key, AlgorithmID algorithmID, String kid, List<X509Certificate> chain, boolean protectedKid) throws CoseException, CertificateEncodingException {
public static Sign1COSEObject sign(
byte[] toBeSigned,
COSEKey key,
AlgorithmID algorithmID,
String kid,
List<X509Certificate> chain,
boolean protectedKid
) throws CoseException, CertificateEncodingException {
Sign1COSEObject coseSignature = new Sign1COSEObject(false);
coseSignature.SetContent(toBeSigned);
coseSignature.addAttribute(
Expand Down Expand Up @@ -230,6 +242,4 @@ public static Sign1COSEObject sign(byte[] toBeSigned, COSEKey key, AlgorithmID a
coseSignature.sign(key);
return coseSignature;
}


}
87 changes: 62 additions & 25 deletions src/main/java/se/digg/wallet/datatypes/mdl/data/DeviceResponse.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
//
// SPDX-License-Identifier: EUPL-1.2

package se.digg.wallet.datatypes.mdl.data;

import com.fasterxml.jackson.core.JsonGenerationException;
Expand Down Expand Up @@ -37,7 +41,11 @@ public class DeviceResponse {
* @param issuerSigned the issuer-signed data associated with the device response.
* @param deviceSignature the byte array representing the device signature.
*/
public DeviceResponse(String docType, IssuerSigned issuerSigned, byte[] deviceSignature) {
public DeviceResponse(
String docType,
IssuerSigned issuerSigned,
byte[] deviceSignature
) {
this.issuerSigned = issuerSigned;
this.deviceSignature = deviceSignature;
this.docType = docType;
Expand All @@ -64,14 +72,14 @@ public DeviceResponse(String docType, IssuerSigned issuerSigned, byte[] deviceSi

/**
* A custom serializer for the {@code DeviceResponse} class that converts a {@code DeviceResponse}
* object into its CBOR representation. This class extends the {@code JsonSerializer} to provide
* object into its CBOR representation. This class extends the {@code JsonSerializer} to provide
* specific serialization logic for {@code DeviceResponse} objects.
* <p>
* The serialization process involves the creation and encoding of a CBOR object that encapsulates
* key fields from the {@code DeviceResponse} instance, including device signature, device MAC,
* The serialization process involves the creation and encoding of a CBOR object that encapsulates
* key fields from the {@code DeviceResponse} instance, including device signature, device MAC,
* namespaces, document type, issuer-signed data, and version details.
* <p>
* The serializer explicitly supports CBOR output, leveraging a {@code CBORGenerator} to output
* The serializer explicitly supports CBOR output, leveraging a {@code CBORGenerator} to output
* the serialized bytes. If a non-CBOR generator is provided, an exception is thrown.
* <p>
* Exception Handling:
Expand All @@ -89,35 +97,59 @@ public void serialize(
JsonGenerator gen,
SerializerProvider serializers
) throws IOException {

CBORObject deviceSignatureMap = CBORObject.NewMap();
if (deviceResponse.getDeviceSignature() != null) {
deviceSignatureMap.Add(CBORObject.FromString("deviceSignature"),
CBORObject.DecodeFromBytes(deviceResponse.getDeviceSignature()));
deviceSignatureMap.Add(
CBORObject.FromString("deviceSignature"),
CBORObject.DecodeFromBytes(deviceResponse.getDeviceSignature())
);
}
if (deviceResponse.getDeviceMac() != null) {
deviceSignatureMap.Add(CBORObject.FromString("deviceMac"),
CBORObject.DecodeFromBytes(deviceResponse.getDeviceMac()));
deviceSignatureMap.Add(
CBORObject.FromString("deviceMac"),
CBORObject.DecodeFromBytes(deviceResponse.getDeviceMac())
);
}

CBORObject deviceSigned = CBORObject.NewOrderedMap();
deviceSigned.Add(CBORObject.FromString("nameSpaces"),
CBORObject.FromCBORObjectAndTag(CBORObject.FromByteArray(deviceResponse.getDeviceNameSpaces().EncodeToBytes()),
EInteger.FromInt32(24)));
deviceSigned.Add(
CBORObject.FromString("nameSpaces"),
CBORObject.FromCBORObjectAndTag(
CBORObject.FromByteArray(
deviceResponse.getDeviceNameSpaces().EncodeToBytes()
),
EInteger.FromInt32(24)
)
);
deviceSigned.Add(CBORObject.FromString("deviceAuth"), deviceSignatureMap);

CBORObject docArray = CBORObject.NewArray();
CBORObject mdoc = CBORObject.NewOrderedMap();
mdoc.Add(CBORObject.FromString("docType"), CBORObject.FromString(deviceResponse.getDocType()));
mdoc.Add(CBORObject.FromString("issuerSigned"),
CBORObject.DecodeFromBytes(CBORUtils.CBOR_MAPPER.writeValueAsBytes(deviceResponse.getIssuerSigned())));
mdoc.Add(
CBORObject.FromString("docType"),
CBORObject.FromString(deviceResponse.getDocType())
);
mdoc.Add(
CBORObject.FromString("issuerSigned"),
CBORObject.DecodeFromBytes(
CBORUtils.CBOR_MAPPER.writeValueAsBytes(
deviceResponse.getIssuerSigned()
)
)
);
mdoc.Add(CBORObject.FromString("deviceSigned"), deviceSigned);
docArray.Add(mdoc);

CBORObject deviceResponseCbor = CBORObject.NewOrderedMap();
deviceResponseCbor.Add(CBORObject.FromString("version"), CBORObject.FromString(deviceResponse.getVersion()));
deviceResponseCbor.Add(
CBORObject.FromString("version"),
CBORObject.FromString(deviceResponse.getVersion())
);
deviceResponseCbor.Add(CBORObject.FromString("documents"), docArray);
deviceResponseCbor.Add(CBORObject.FromString("status"), CBORObject.FromInt32(0));
deviceResponseCbor.Add(
CBORObject.FromString("status"),
CBORObject.FromInt32(0)
);

// Generate serialized CBOR bytes
byte[] value = deviceResponseCbor.EncodeToBytes();
Expand All @@ -140,22 +172,27 @@ public void serialize(
*/
public static DeviceResponse deserialize(byte[] cborEncoded)
throws TokenParsingException {

try {
CBORObject deviceResponseObject = CBORObject.DecodeFromBytes(cborEncoded);
String version = deviceResponseObject.get("version").AsString();
int status = deviceResponseObject.get("status").AsInt32();
CBORObject documents = deviceResponseObject.get("documents");
CBORObject doc = documents.get(0);
IssuerSigned issuerSigned = IssuerSigned.deserialize(doc.get("issuerSigned").EncodeToBytes());
IssuerSigned issuerSigned = IssuerSigned.deserialize(
doc.get("issuerSigned").EncodeToBytes()
);
String docType = doc.get("docType").AsString();
CBORObject deviceSigned = doc.get("deviceSigned");
CBORObject deviceNameSpaces = CBORObject.DecodeFromBytes(deviceSigned.get("nameSpaces").Untag().GetByteString());
CBORObject deviceNameSpaces = CBORObject.DecodeFromBytes(
deviceSigned.get("nameSpaces").Untag().GetByteString()
);
CBORObject deviceAuth = deviceSigned.get("deviceAuth");
byte[] deviceSignature = deviceAuth.get("deviceSignature") != null
? deviceAuth.get("deviceSignature").EncodeToBytes() : null;
? deviceAuth.get("deviceSignature").EncodeToBytes()
: null;
byte[] deviceMac = deviceAuth.get("deviceMac") != null
? deviceAuth.get("deviceMac").EncodeToBytes() : null;
? deviceAuth.get("deviceMac").EncodeToBytes()
: null;

return new DeviceResponse(
status,
Expand All @@ -164,10 +201,10 @@ public static DeviceResponse deserialize(byte[] cborEncoded)
issuerSigned,
deviceNameSpaces,
deviceSignature,
deviceMac);
deviceMac
);
} catch (Exception e) {
throw new TokenParsingException("Failed to parse Device Response", e);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* Represents an issuer-signed item containing an instance of disclosed attribute data associated with an mDL document.
* This class is serialized and deserialized using CBOR (Concise Binary Object Representation),
* adhering to specific encoding and decoding mechanisms provided via custom serializers
* adhering to specific encoding and decoding mechanisms provided via custom serializers
* and deserializers.
* <p>
* Fields:
Expand All @@ -42,7 +42,7 @@
*
* Nested Classes:
* <ul>
* <li>`Serializer`: Custom serializer for transforming an IssuerSignedItem instance
* <li>`Serializer`: Custom serializer for transforming an IssuerSignedItem instance
* into the CBOR binary format, implementing {@link JsonSerializer}.</li>
* <li>`Deserializer`: Custom deserializer for reconstructing an IssuerSignedItem instance
* from the CBOR binary format, implementing {@link JsonDeserializer}.</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
//
// SPDX-License-Identifier: EUPL-1.2

package se.digg.wallet.datatypes.mdl.data;

import java.util.List;
Expand All @@ -13,7 +17,8 @@
* This class also provides a builder for convenient construction of its instances.
*/
@Getter
public class MdlPresentationInput extends PresentationInput<Map<String, List<String>>> {
public class MdlPresentationInput
extends PresentationInput<Map<String, List<String>>> {

/** The presentation requester client ID (See OpenID4VP) */
private String clientId;
Expand Down Expand Up @@ -86,7 +91,9 @@ public MdlPresentationInputBuilder clientId(String walletId) {
* @param mdocGeneratedNonce the string value representing the mdoc generated nonce
* @return the {@code MdlPresentationInputBuilder} instance for method chaining
*/
public MdlPresentationInputBuilder mdocGeneratedNonce(String mdocGeneratedNonce) {
public MdlPresentationInputBuilder mdocGeneratedNonce(
String mdocGeneratedNonce
) {
mdlPresentationInput.mdocGeneratedNonce = mdocGeneratedNonce;
return this;
}
Expand All @@ -108,7 +115,9 @@ public MdlPresentationInputBuilder responseUri(String responseUri) {
* @param disclosures a map where the key is a namespace, and the value is a list of attribute names being disclosed
* @return the {@code MdlPresentationInputBuilder} instance for method chaining
*/
public MdlPresentationInputBuilder disclosures(Map<String, List<String>> disclosures) {
public MdlPresentationInputBuilder disclosures(
Map<String, List<String>> disclosures
) {
mdlPresentationInput.disclosures = disclosures;
return this;
}
Expand All @@ -119,7 +128,9 @@ public MdlPresentationInputBuilder disclosures(Map<String, List<String>> disclos
* @param algorithm the {@code TokenSigningAlgorithm} specifying the signing algorithm to be used by the wallet
* @return the {@code MdlPresentationInputBuilder} instance for method chaining
*/
public MdlPresentationInputBuilder algorithm(TokenSigningAlgorithm algorithm) {
public MdlPresentationInputBuilder algorithm(
TokenSigningAlgorithm algorithm
) {
mdlPresentationInput.algorithm = algorithm;
return this;
}
Expand All @@ -133,5 +144,4 @@ public MdlPresentationInput build() {
return mdlPresentationInput;
}
}

}
Loading

0 comments on commit 9d796bd

Please sign in to comment.