Skip to content

Commit

Permalink
feat: Testcase modification as per new API specification
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-vavdiya committed Jun 6, 2023
1 parent e56a52e commit f29edb3
Show file tree
Hide file tree
Showing 20 changed files with 363 additions and 120 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ configurations {
}

repositories {
// mavenLocal()
mavenLocal()
mavenCentral()
maven {
url = uri("https://repo.danubetech.com/repository/maven-public")
Expand Down Expand Up @@ -58,6 +58,7 @@ dependencies {
testImplementation "org.testcontainers:postgresql"
testImplementation "org.testcontainers:junit-jupiter"
testImplementation group: 'com.github.dasniko', name: 'testcontainers-keycloak', version: '2.5.0'
testImplementation group: 'org.mockito', name: 'mockito-inline', version: '5.2.0'
testImplementation group: 'org.json', name: 'json', version: '20230227'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@

import java.security.Principal;

/**
* The type Base controller.
*/
public class BaseController {

/**
* Gets bpn from token.
*
* @param principal the principal
* @return the bpn from token
*/
public String getBPNFromToken(Principal principal) {
Object principal1 = ((JwtAuthenticationToken) principal).getPrincipal();
Jwt jwt = (Jwt) principal1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public ResponseEntity<DidDocument> getDidDocument(@Parameter(description = "Did
return ResponseEntity.status(HttpStatus.OK).body(service.getDidDocument(identifier));
}

/**
* Gets did resolve.
*
* @param bpn the bpn
* @return the did resolve
*/
@Operation(description = "Resolve the DID document for a given BPN", summary = "Resolve DID Document")
@GetMapping(path = RestURI.DID_RESOLVE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<DidDocument> getDidResolve(@Parameter(description = "Did or BPN") @PathVariable(name = "bpn") String bpn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.util.Map;

/**
* The type Credential controller.
* The type Holders credential controller.
*/
@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -71,6 +71,13 @@ public ResponseEntity<List<VerifiableCredential>> getCredentials(@RequestParam(r
}


/**
* Issue credential response entity.
*
* @param data the data
* @param principal the principal
* @return the response entity
*/
@Operation(summary = "Issue Verifiable Credential", description = "Permission: **update_wallets** OR **update_wallet** (The BPN of the issuer of the Verifiable Credential must equal BPN of caller)\nIssue a verifiable credential with a given issuer DID")
@PostMapping(path = RestURI.CREDENTIALS, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)

Expand Down Expand Up @@ -98,6 +105,13 @@ public ResponseEntity<VerifiableCredential> issueCredential(@RequestBody Map<Str
return ResponseEntity.status(HttpStatus.CREATED).body(holdersCredentialService.issueCredential(data, getBPNFromToken(principal)));
}

/**
* Delete credential response entity.
*
* @param credentialId the credential id
* @param principal the principal
* @return the response entity
*/
@Operation(description = "Permission: **update_wallet** (The BPN of holderIdentifier must equal BPN of caller)\n\n Delete a verifiable credential by its ID", summary = "Delete a verifiable credential by its ID")
@DeleteMapping(path = RestURI.CREDENTIALS, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> deleteCredential(@RequestParam(name = "id") String credentialId, Principal principal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,27 @@
import java.util.List;
import java.util.Map;

/**
* The type Issuers credential controller.
*/
@RestController
@RequiredArgsConstructor
public class IssuersCredentialController extends BaseController {

private final IssuersCredentialService issuersCredentialService;


/**
* Gets credentials.
*
* @param credentialId the credential id
* @param holderIdentifier the holder identifier
* @param type the type
* @param sortColumn the sort column
* @param sortTpe the sort tpe
* @param principal the principal
* @return the credentials
*/
@Tag(name = "Verifiable Credential -Issuer")
@Operation(description = "Permission: **view_wallets** OR **view_wallet** (The BPN of holderIdentifier must equal BPN of caller)\n\n Search verifiable credentials with filter criteria", summary = "Query Verifiable Credentials")
@GetMapping(path = RestURI.ISSUERS_CREDENTIALS, produces = MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -60,6 +74,13 @@ public ResponseEntity<List<VerifiableCredential>> getCredentials(@RequestParam(r
return ResponseEntity.status(HttpStatus.OK).body(issuersCredentialService.getCredentials(credentialId, holderIdentifier, type, sortColumn, sortTpe, getBPNFromToken(principal)));
}

/**
* Issue membership credential response entity.
*
* @param issueMembershipCredentialRequest the issue membership credential request
* @param principal the principal
* @return the response entity
*/
@Tag(name = "Verifiable Credential -Issuer")

@Operation(summary = "Issue a Membership Verifiable Credential with base wallet issuer", description = "Permission: **update_wallets** OR **update_wallet** (The BPN of base wallet must equal BPN of caller)\n\n Issue a verifiable credential by base wallet")
Expand All @@ -68,6 +89,13 @@ public ResponseEntity<VerifiableCredential> issueMembershipCredential(@Valid @Re
return ResponseEntity.status(HttpStatus.CREATED).body(issuersCredentialService.issueMembershipCredential(issueMembershipCredentialRequest, getBPNFromToken(principal)));
}

/**
* Issue dismantler credential response entity.
*
* @param request the request
* @param principal the principal
* @return the response entity
*/
@Tag(name = "Verifiable Credential -Issuer")

@Operation(summary = "Issue a Dismantler Verifiable Credential with base wallet issuer", description = "Permission: **update_wallets** OR **update_wallet** (The BPN of base wallet must equal BPN of caller)\n\n Issue a verifiable credential by base wallet")
Expand All @@ -76,13 +104,26 @@ public ResponseEntity<VerifiableCredential> issueDismantlerCredential(@Valid @Re
return ResponseEntity.status(HttpStatus.CREATED).body(issuersCredentialService.issueDismantlerCredential(request, getBPNFromToken(principal)));
}

/**
* Issue framework credential response entity.
*
* @param request the request
* @param principal the principal
* @return the response entity
*/
@Tag(name = "Verifiable Credential -Issuer")
@Operation(summary = "Issue a Use Case Verifiable Credential with base wallet issuer", description = "Permission: **update_wallets** OR **update_wallet** (The BPN of base wallet must equal BPN of caller)\n\n Issue a verifiable credential by base wallet")
@PostMapping(path = RestURI.API_CREDENTIALS_ISSUER_FRAMEWORK, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<VerifiableCredential> issueFrameworkCredential(@Valid @RequestBody IssueFrameworkCredentialRequest request, Principal principal) {
return ResponseEntity.status(HttpStatus.CREATED).body(issuersCredentialService.issueFrameworkCredential(request, getBPNFromToken(principal)));
}

/**
* Credentials validation response entity.
*
* @param data the data
* @return the response entity
*/
@Tag(name = "Verifiable Credential - Validation")
@Operation(summary = "Validate Verifiable Credentials", description = "Permission: **view_wallets** OR **view_wallet** \n\n Validate Verifiable Credentials")
@PostMapping(path = RestURI.CREDENTIALS_VALIDATION, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
Expand Down Expand Up @@ -118,6 +159,13 @@ public ResponseEntity<Map<String, Object>> credentialsValidation(@RequestBody Ma
return ResponseEntity.status(HttpStatus.OK).body(issuersCredentialService.credentialsValidation(data));
}

/**
* Issue credential response entity.
*
* @param data the data
* @param principal the principal
* @return the response entity
*/
@Tag(name = "Verifiable Credential -Issuer")
@Operation(summary = "Issue Verifiable Credential", description = "Permission: **update_wallets** OR **update_wallet** (The BPN of the base wallet must equal BPN of caller)\nIssue a verifiable credential with a given issuer DID")
@PostMapping(path = RestURI.ISSUERS_CREDENTIALS, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public interface HoldersCredentialRepository extends BaseRepository<HoldersCrede
* @param type the type
* @return the by holder did and type
*/
HoldersCredential getByHolderDidAndType(String holderDid, String type);
List<HoldersCredential> getByHolderDidAndType(String holderDid, String type);

/**
* Exists by holder did and type boolean.
Expand All @@ -69,9 +69,21 @@ public interface HoldersCredentialRepository extends BaseRepository<HoldersCrede
*/
boolean existsByHolderDidAndType(String holderDid, String type);

/**
* Delete by credential id.
*
* @param credentialId the credential id
*/
@Modifying
@Query("delete from HoldersCredential where credentialId=:credentialId")
void deleteByCredentialId(@Param("credentialId") String credentialId);

/**
* Exists by holder did and credential id boolean.
*
* @param holderDid the holder did
* @param credentialId the credential id
* @return the boolean
*/
boolean existsByHolderDidAndCredentialId(String holderDid, String credentialId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,21 @@
import com.smartsensesolutions.java.commons.base.repository.BaseRepository;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.IssuersCredential;

import java.util.List;

/**
* The interface Credential repository.
*/
public interface IssuersCredentialRepository extends BaseRepository<IssuersCredential, Long> {



/**
* Gets by issuer did and holder did and type.
*
* @param issuerDid the issuer did
* @param holderDid the holder did
* @param membershipCredentialCx the membership credential cx
* @return the by issuer did and holder did and type
*/
List<IssuersCredential> getByIssuerDidAndHolderDidAndType(String issuerDid, String holderDid, String membershipCredentialCx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

package org.eclipse.tractusx.managedidentitywallets.service;

import com.google.common.collect.ImmutableMap;
import com.smartsensesolutions.java.commons.FilterRequest;
import com.smartsensesolutions.java.commons.base.repository.BaseRepository;
import com.smartsensesolutions.java.commons.base.service.BaseService;
Expand All @@ -30,21 +29,16 @@
import com.smartsensesolutions.java.commons.sort.Sort;
import com.smartsensesolutions.java.commons.sort.SortType;
import com.smartsensesolutions.java.commons.specification.SpecificationUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.HoldersCredential;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet;
import org.eclipse.tractusx.managedidentitywallets.dao.repository.HoldersCredentialRepository;
import org.eclipse.tractusx.managedidentitywallets.exception.CredentialNotFoundProblem;
import org.eclipse.tractusx.managedidentitywallets.exception.ForbiddenException;
import org.eclipse.tractusx.managedidentitywallets.utils.CommonUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.Validate;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebDocumentResolver;
import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.eclipse.tractusx.ssi.lib.proof.LinkedDataProofValidation;
import org.eclipse.tractusx.ssi.lib.resolver.DidDocumentResolverRegistry;
import org.eclipse.tractusx.ssi.lib.resolver.DidDocumentResolverRegistryImpl;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
Expand All @@ -53,55 +47,30 @@
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.net.http.HttpClient;
import java.util.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* The type Credential service.
*/
@Service
@Slf4j
@RequiredArgsConstructor
public class HoldersCredentialService extends BaseService<HoldersCredential, Long> {

/**
* The constant BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN.
*/
public static final String BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN = "Base wallet BPN is not matching with request BPN(from token)";
private final HoldersCredentialRepository holdersCredentialRepository;
private final MIWSettings miwSettings;
private final WalletService walletService;

private final SpecificationUtil<HoldersCredential> credentialSpecificationUtil;

private final WalletKeyService walletKeyService;

private final Map<String, String> supportedFrameworkVCTypes;

/**
* Instantiates a new Holders credential service.
*
* @param holdersCredentialRepository the holders credential repository
* @param miwSettings the miw settings
* @param walletService the wallet service
* @param credentialSpecificationUtil the credential specification util
* @param walletKeyService the wallet key service
*/
public HoldersCredentialService(HoldersCredentialRepository holdersCredentialRepository, MIWSettings miwSettings, WalletService walletService, SpecificationUtil<HoldersCredential> credentialSpecificationUtil, WalletKeyService walletKeyService) {
this.holdersCredentialRepository = holdersCredentialRepository;
this.miwSettings = miwSettings;
this.walletService = walletService;
this.credentialSpecificationUtil = credentialSpecificationUtil;
this.walletKeyService = walletKeyService;

Map<String, String> tmpMap = new HashMap<>();
for (String type : org.apache.commons.lang3.StringUtils.split(miwSettings.supportedFrameworkVCTypes(), ",")) {
tmpMap.put(type.split("=")[0].trim(), type.split("=")[1].trim());
}
supportedFrameworkVCTypes = ImmutableMap.copyOf(tmpMap);

}


@Override
protected BaseRepository<HoldersCredential, Long> getRepository() {
return holdersCredentialRepository;
Expand Down Expand Up @@ -165,28 +134,6 @@ public List<VerifiableCredential> getCredentials(String credentialId, String iss
return list;
}

/**
* Credentials validation map.
*
* @param data the data
* @return the map
*/
public Map<String, Object> credentialsValidation(Map<String, Object> data) {
VerifiableCredential verifiableCredential = new VerifiableCredential(data);

DidDocumentResolverRegistry didDocumentResolverRegistry = new DidDocumentResolverRegistryImpl();
didDocumentResolverRegistry.register(
new DidWebDocumentResolver(HttpClient.newHttpClient(), new DidWebParser(), miwSettings.enforceHttps()));

LinkedDataProofValidation proofValidation = LinkedDataProofValidation.newInstance(didDocumentResolverRegistry);
Boolean valid = proofValidation.checkProof(verifiableCredential);
Map<String, Object> response = new HashMap<>();
response.put("valid", valid);
response.put("vc", verifiableCredential);

return response;
}

/**
* Issue credential verifiable credential.
*
Expand Down Expand Up @@ -217,6 +164,12 @@ public VerifiableCredential issueCredential(Map<String, Object> data, String cal
return credential.getData();
}

/**
* Delete credential.
*
* @param credentialId the credential id
* @param bpnFromToken the bpn from token
*/
@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
public void deleteCredential(String credentialId, String bpnFromToken) {
//Fetch Holder Wallet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ public VerifiableCredential issueCredentialUsingBaseWallet(Map<String, Object> d
// Create Credential
HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(verifiableCredential.getCredentialSubject().get(0),
verifiableCredential.getTypes(), issuerWallet.getDidDocument(),
privateKeyBytes, issuerWallet.getDid(),
privateKeyBytes,
issuerWallet.getDid(), //TODO need to check, how we can identify holder of VC, need to m
verifiableCredential.getContext(), Date.from(verifiableCredential.getExpirationDate()), false);


Expand Down
Loading

0 comments on commit f29edb3

Please sign in to comment.