Skip to content

Commit

Permalink
fix: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-vavdiya committed May 23, 2024
1 parent 697ff85 commit e00620d
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.dto;
package org.eclipse.tractusx.managedidentitywallets.dto;

import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;

import java.util.LinkedHashMap;
import java.util.Map;

public class CredentialVerificationRequest extends LinkedHashMap<String, Object> {


public void setJwt(String jwt) {
put(StringPool.VC_JWT_KEY, jwt);
}
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;

import java.util.LinkedHashMap;
import java.util.Map;

public void setVc(Map<String,Object> vc) {
putAll(vc);
}
}

public class CredentialVerificationRequest extends LinkedHashMap<String, Object> {


public void setJwt(String jwt) {
put(StringPool.VC_JWT_KEY, jwt);
}

public void setVc(Map<String, Object> vc) {
putAll(vc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public void setJwt(String jwt) {
put(StringPool.VC_JWT_KEY, jwt);
}

public void setVc(Map<String,Object> vc) {
public void setVc(Map<String, Object> vc) {
putAll(vc);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.dto;
package org.eclipse.tractusx.managedidentitywallets.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
Expand All @@ -34,18 +34,18 @@
/**
* The type Issue membership credential request.
*/
@Getter
@Setter
@NoArgsConstructor
@Builder
@AllArgsConstructor
public class IssueMembershipCredentialRequest {
@Getter
@Setter
@NoArgsConstructor
@Builder
@AllArgsConstructor
public class IssueMembershipCredentialRequest {

@NotBlank(message = "Please provide BPN")
@Pattern(regexp = StringPool.BPN_NUMBER_REGEX, message = "Please provide valid BPN")
private String bpn;
@NotBlank(message = "Please provide BPN")
@Pattern(regexp = StringPool.BPN_NUMBER_REGEX, message = "Please provide valid BPN")
private String bpn;

@JsonProperty("asJwt")
private boolean asJwt;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.tractusx.managedidentitywallets.exception.WalletNotFoundProblem;
import org.eclipse.tractusx.managedidentitywallets.utils.CommonUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.Validate;
import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.springframework.stereotype.Service;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public PageImpl<CredentialsResponse> getCredentials(GetCredentialsCommand comman
for (HoldersCredential credential : filter.getContent()) {
CredentialsResponse cr = new CredentialsResponse();
if (command.isAsJwt()) {
cr.setJwt(CommonUtils.vcAsJwt(command.getIdentifier() != null ? commonService.getWalletByIdentifier(command.getIdentifier()) : holderWallet , holderWallet, credential.getData(), walletKeyService));
cr.setJwt(CommonUtils.vcAsJwt(command.getIdentifier() != null ? commonService.getWalletByIdentifier(command.getIdentifier()) : holderWallet, holderWallet, credential.getData(), walletKeyService));
} else {
cr.setVc(credential.getData());
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public CredentialsResponse issueCredential(Map<String, Object> data, String call

// Return VC
if (asJwt) {
cr.setJwt(CommonUtils.vcAsJwt(issuerWallet, commonService.getWalletByIdentifier(callerBpn), credential.getData() , walletKeyService));
cr.setJwt(CommonUtils.vcAsJwt(issuerWallet, commonService.getWalletByIdentifier(callerBpn), credential.getData(), walletKeyService));
} else {
cr.setVc(credential.getData());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public CredentialsResponse issueFrameworkCredential(IssueFrameworkCredentialRequ

// Return VC
if (asJwt) {
cr.setJwt(CommonUtils.vcAsJwt(baseWallet, holderWallet, issuersCredential.getData() , walletKeyService));
cr.setJwt(CommonUtils.vcAsJwt(baseWallet, holderWallet, issuersCredential.getData(), walletKeyService));
} else {
cr.setVc(issuersCredential.getData());
}
Expand All @@ -279,7 +279,7 @@ public CredentialsResponse issueFrameworkCredential(IssueFrameworkCredentialRequ
* @return the verifiable credential
*/
@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
public CredentialsResponse issueDismantlerCredential(IssueDismantlerCredentialRequest request, boolean asJwt, String callerBPN) {
public CredentialsResponse issueDismantlerCredential(IssueDismantlerCredentialRequest request, boolean asJwt, String callerBPN) {

//Fetch Holder Wallet
Wallet holderWallet = commonService.getWalletByIdentifier(request.getBpn());
Expand Down Expand Up @@ -320,7 +320,7 @@ public CredentialsResponse issueDismantlerCredential(IssueDismantlerCredentialRe

// Return VC
if (asJwt) {
cr.setJwt(CommonUtils.vcAsJwt(issuerWallet, holderWallet, issuersCredential.getData() , walletKeyService));
cr.setJwt(CommonUtils.vcAsJwt(issuerWallet, holderWallet, issuersCredential.getData(), walletKeyService));
} else {
cr.setVc(issuersCredential.getData());
}
Expand Down Expand Up @@ -383,7 +383,7 @@ public CredentialsResponse issueMembershipCredential(IssueMembershipCredentialRe

// Return VC
if (asJwt) {
cr.setJwt(CommonUtils.vcAsJwt(issuerWallet, holderWallet, issuersCredential.getData() , walletKeyService));
cr.setJwt(CommonUtils.vcAsJwt(issuerWallet, holderWallet, issuersCredential.getData(), walletKeyService));
} else {
cr.setVc(issuersCredential.getData());
}
Expand Down Expand Up @@ -441,7 +441,7 @@ public CredentialsResponse issueCredentialUsingBaseWallet(String holderDid, Map<

// Return VC
if (asJwt) {
cr.setJwt(CommonUtils.vcAsJwt(issuerWallet, holderWallet, issuersCredential.getData() , walletKeyService));
cr.setJwt(CommonUtils.vcAsJwt(issuerWallet, holderWallet, issuersCredential.getData(), walletKeyService));
} else {
cr.setVc(issuersCredential.getData());
}
Expand Down Expand Up @@ -518,7 +518,7 @@ public Map<String, Object> credentialsValidation(CredentialVerificationRequest v
* @return the map
*/
@SneakyThrows
public Map<String, Object> credentialsValidation(CredentialVerificationRequest verificationRequest, boolean withCredentialsValidation , boolean withCredentialExpiryDate) {
public Map<String, Object> credentialsValidation(CredentialVerificationRequest verificationRequest, boolean withCredentialsValidation, boolean withCredentialExpiryDate) {
HttpClient httpClient = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ public Map<String, Object> createPresentation(Map<String, Object> data, boolean
return buildVP(asJwt, audience, callerBpn, callerWallet, verifiableCredentials, SupportedAlgorithms.ED25519);
}

private Map<String, Object> buildVP(boolean asJwt, String audience, String callerBpn, Wallet callerWallet,
List<VerifiableCredential> verifiableCredentials, SupportedAlgorithms algorithm){
private Map<String, Object> buildVP(boolean asJwt, String audience, String callerBpn, Wallet callerWallet, List<VerifiableCredential> verifiableCredentials, SupportedAlgorithms algorithm) {
Map<String, Object> response = new HashMap<>();
if (asJwt && algorithm.equals(SupportedAlgorithms.ES256K)) {
buildVPJwtES256K(audience, callerBpn, callerWallet, verifiableCredentials, algorithm, response);
Expand All @@ -172,7 +171,7 @@ private void buildVPJsonLd(String callerBpn, List<VerifiableCredential> verifiab
response.put(StringPool.VP, verifiablePresentation);
}

@SneakyThrows({ InvalidPrivateKeyFormatException.class})
@SneakyThrows({ InvalidPrivateKeyFormatException.class })
private void buildVPJwtEdDSA(String audience, String callerBpn, Wallet callerWallet, List<VerifiableCredential> verifiableCredentials, SupportedAlgorithms algorithm, Map<String, Object> response) {
Pair<Did, Object> result = getPrivateKey(callerWallet, algorithm, audience, callerBpn);
String keyId = walletKeyService.getWalletKeyIdByWalletId(callerWallet.getId());
Expand All @@ -182,7 +181,7 @@ private void buildVPJwtEdDSA(String audience, String callerBpn, Wallet callerWal

X25519PrivateKey ed25519Key = (X25519PrivateKey) result.getRight();
X25519PrivateKey privateKey = new X25519PrivateKey(ed25519Key.asByte());
SignedJWT presentation = presentationFactory.createPresentation(result.getLeft(), verifiableCredentials, audience, privateKey , keyId);
SignedJWT presentation = presentationFactory.createPresentation(result.getLeft(), verifiableCredentials, audience, privateKey, keyId);
response.put(StringPool.VP, presentation.serialize());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected SpecificationUtil<WalletKey> getSpecificationUtil() {
/**
* Get private key by wallet identifier as bytes byte [ ].
*
* @param walletId the wallet id
* @param walletId the wallet id
* @param algorithm the algorithm
* @return the byte [ ]
*/
Expand All @@ -85,7 +85,7 @@ public byte[] getPrivateKeyByWalletIdAsBytes(long walletId, String algorithm) {
/**
* Gets private key by wallet identifier.
*
* @param walletId the wallet id
* @param walletId the wallet id
* @param algorithm the algorithm
* @return the private key by wallet identifier
*/
Expand All @@ -104,8 +104,8 @@ public Object getPrivateKeyByWalletIdAndAlgorithm(long walletId, SupportedAlgori
}
}

/**
* Gets wallet key by wallet id.
/**
* Gets wallet key by wallet id.
*
* @param walletId the wallet id
* @return the wallet key by wallet identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ public static String getIdentifierType(String identifier) {
* @param holderDid the holder did
* @return the credential
*/
public static HoldersCredential getHoldersCredential(VerifiableCredentialSubject subject, List<String> types,
DidDocument issuerDoc,
byte[] privateKeyBytes, String holderDid, List<URI> contexts, Date expiryDate, boolean selfIssued) {
public static HoldersCredential getHoldersCredential(VerifiableCredentialSubject subject, List<String> types, DidDocument issuerDoc, byte[] privateKeyBytes, String holderDid, List<URI> contexts, Date expiryDate, boolean selfIssued) {
List<String> cloneTypes = new ArrayList<>(types);

// Create VC
Expand All @@ -121,11 +119,8 @@ public static HoldersCredential getHoldersCredential(VerifiableCredentialSubject
.build();
}

@SneakyThrows({UnsupportedSignatureTypeException.class , InvalidPrivateKeyFormatException.class , SignatureGenerateFailedException.class , TransformJsonLdException.class})
private static VerifiableCredential createVerifiableCredential(DidDocument issuerDoc,
List<String> verifiableCredentialType,
VerifiableCredentialSubject verifiableCredentialSubject,
byte[] privateKey, List<URI> contexts, Date expiryDate) {
@SneakyThrows({ UnsupportedSignatureTypeException.class, InvalidPrivateKeyFormatException.class, SignatureGenerateFailedException.class, TransformJsonLdException.class })
private static VerifiableCredential createVerifiableCredential(DidDocument issuerDoc, List<String> verifiableCredentialType, VerifiableCredentialSubject verifiableCredentialSubject, byte[] privateKey, List<URI> contexts, Date expiryDate) {
// VC Builder

// if the credential does not contain the JWS proof-context add it
Expand Down Expand Up @@ -180,8 +175,9 @@ public static SecureTokenRequest getSecureTokenRequest(MultiValueMap<String, Str
Map<String, String> singleValueMap = map.toSingleValueMap();
return objectMapper.convertValue(singleValueMap, SecureTokenRequest.class);
}
@SneakyThrows({DidParseException.class})
public static String vcAsJwt(Wallet issuerWallet, Wallet holderWallet, VerifiableCredential vc , WalletKeyService walletKeyService){

@SneakyThrows({ DidParseException.class })
public static String vcAsJwt(Wallet issuerWallet, Wallet holderWallet, VerifiableCredential vc, WalletKeyService walletKeyService) {

Did issuerDid = DidParser.parse(issuerWallet.getDid());
Did holderDid = DidParser.parse(holderWallet.getDid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ public static void beforeAll() throws SQLException {
walletKeyService,
holdersCredentialRepository,
commonService,
objectMapper
);
objectMapper);
}

@BeforeEach
Expand Down Expand Up @@ -174,10 +173,10 @@ void shouldIssueCredentialAsJwt()
when(walletKey.getKeyId()).thenReturn(KEY_ID);
when(walletKey.getId()).thenReturn(42L);
when(baseWallet.getAlgorithm()).thenReturn("ED25519");
when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId() ,SupportedAlgorithms.valueOf(baseWallet.getAlgorithm())))
when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId(), SupportedAlgorithms.valueOf(baseWallet.getAlgorithm())))
.thenReturn(new X25519PrivateKey(keyPair.getPrivateKey().asStringForStoring(), true));
when(walletKeyService.getWalletKeyIdByWalletId(baseWallet.getId())).thenReturn(walletKeyId);
when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId() , "ED25519")).thenReturn(keyPair.getPrivateKey()
when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId(), "ED25519")).thenReturn(keyPair.getPrivateKey()
.asByte());
CredentialsResponse credentialsResponse = assertDoesNotThrow(
() -> issuersCredentialService.issueMembershipCredential(
Expand Down Expand Up @@ -225,9 +224,9 @@ void shouldIssueCredentialAsJwt()
when(walletKey.getKeyId()).thenReturn(KEY_ID);
when(walletKey.getId()).thenReturn(42L);
when(baseWallet.getAlgorithm()).thenReturn("ED25519");
when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId() , "ED25519")).thenReturn(keyPair.getPrivateKey()
when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId(), "ED25519")).thenReturn(keyPair.getPrivateKey()
.asByte());
when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId() ,SupportedAlgorithms.valueOf(baseWallet.getAlgorithm())))
when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId(), SupportedAlgorithms.valueOf(baseWallet.getAlgorithm())))
.thenReturn(new X25519PrivateKey(keyPair.getPrivateKey().asStringForStoring(), true));
when(walletKeyService.getWalletKeyIdByWalletId(baseWallet.getId())).thenReturn(walletKeyId);

Expand Down Expand Up @@ -266,10 +265,9 @@ void shouldIssueCredentialAsJwt() throws IOException, InvalidPrivateKeyFormatExc
when(walletKey.getKeyId()).thenReturn(KEY_ID);
when(walletKey.getId()).thenReturn(42L);
when(baseWallet.getAlgorithm()).thenReturn("ED25519");
when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId() ,SupportedAlgorithms.valueOf(baseWallet.getAlgorithm())))
when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId(), SupportedAlgorithms.valueOf(baseWallet.getAlgorithm())))
.thenReturn(new X25519PrivateKey(keyPair.getPrivateKey().asStringForStoring(), true));
when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId() , "ED25519")).thenReturn(keyPair.getPrivateKey()
.asByte());
when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId(), "ED25519")).thenReturn(keyPair.getPrivateKey().asByte());
when(walletKeyService.getWalletKeyIdByWalletId(baseWallet.getId())).thenReturn(walletKeyId);

CredentialsResponse credentialsResponse = assertDoesNotThrow(
Expand Down Expand Up @@ -301,7 +299,7 @@ void shouldIssueCredentialAsJwt() throws IOException, ParseException, InvalidPri
MockUtil.generateDid("basewallet")).build();

MockUtil.makeCreateWorkForIssuer(issuersCredentialRepository);
when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId() , "ED25519")).thenReturn(keyPair.getPrivateKey()
when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId(), "ED25519")).thenReturn(keyPair.getPrivateKey()
.asByte());
when(commonService.getWalletByIdentifier(holderWalletBpn)).thenReturn(holderWallet);
when(commonService.getWalletByIdentifier(verifiableCredential.getIssuer()
Expand All @@ -321,7 +319,7 @@ public HoldersCredential answer(InvocationOnMock invocation) throws Throwable {
when(walletKey.getKeyId()).thenReturn(KEY_ID);
when(baseWallet.getAlgorithm()).thenReturn("ED25519");
when(walletKey.getId()).thenReturn(42L);
when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId() ,SupportedAlgorithms.valueOf(baseWallet.getAlgorithm())))
when(walletKeyService.getPrivateKeyByWalletIdAndAlgorithm(baseWallet.getId(), SupportedAlgorithms.valueOf(baseWallet.getAlgorithm())))
.thenReturn(new X25519PrivateKey(keyPair.getPrivateKey().asStringForStoring(), true));
when(walletKeyService.getWalletKeyIdByWalletId(baseWallet.getId())).thenReturn(walletKeyId);

Expand All @@ -343,9 +341,8 @@ class jwtValidationTest {

@RegisterExtension
static WireMockExtension issuer = WireMockExtension.newInstance()
.options(wireMockConfig()
.dynamicPort()
// .notifier(new ConsoleNotifier(true))
.options(wireMockConfig().dynamicPort()
// .notifier(new ConsoleNotifier(true))
)
.build();

Expand Down Expand Up @@ -435,7 +432,7 @@ private void mockCommon(
when(miwSettings.authorityWalletBpn()).thenReturn(baseWalletBpn);
when(commonService.getWalletByIdentifier(baseWalletBpn)).thenReturn(baseWallet);
when(commonService.getWalletByIdentifier(holderWalletBpn)).thenReturn(holderWallet);
when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId() ,baseWallet.getAlgorithm()))
when(walletKeyService.getPrivateKeyByWalletIdAsBytes(baseWallet.getId(), baseWallet.getAlgorithm()))
.thenReturn(keyPair.getPrivateKey().asByte());
when(miwSettings.supportedFrameworkVCTypes()).thenReturn(Set.of("SustainabilityCredential"));
when(holdersCredentialRepository.save(any(HoldersCredential.class)))
Expand Down
Loading

0 comments on commit e00620d

Please sign in to comment.