Skip to content

Commit

Permalink
fix: get vc as jwt with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafasalfiti authored and nitin-vavdiya committed May 14, 2024
1 parent 842e437 commit 4216e0d
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.eclipse.tractusx.managedidentitywallets.command;

import java.util.List;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Builder
@Getter
@Setter
public class GetCredentialsCommand {
private String credentialId;
private String identifier;
private List<String> type;
private String sortColumn;
private String sortType;
private int pageNumber;
private int size;
private boolean asJwt;
private String callerBPN;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
import org.eclipse.tractusx.managedidentitywallets.apidocs.HoldersCredentialControllerApiDocs.GetCredentialsApiDocs;
import org.eclipse.tractusx.managedidentitywallets.apidocs.HoldersCredentialControllerApiDocs.IssueCredentialApiDoc;
import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.AsJwtParam;
import org.eclipse.tractusx.managedidentitywallets.command.GetCredentialsCommand;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.dto.CredentialsResponse;
import org.eclipse.tractusx.managedidentitywallets.service.HoldersCredentialService;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -71,7 +72,7 @@ public class HoldersCredentialController extends BaseController {
*/
@GetCredentialsApiDocs
@GetMapping(path = RestURI.CREDENTIALS, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<PageImpl<VerifiableCredential>> getCredentials(@Parameter(name = "credentialId", description = "Credential Id", examples = {@ExampleObject(name = "Credential Id", value = "did:web:localhost:BPNL000000000000#12528899-160a-48bd-ba15-f396c3959ae9")}) @RequestParam(required = false) String credentialId,
public ResponseEntity<PageImpl<CredentialsResponse>> getCredentials(@Parameter(name = "credentialId", description = "Credential Id", examples = {@ExampleObject(name = "Credential Id", value = "did:web:localhost:BPNL000000000000#12528899-160a-48bd-ba15-f396c3959ae9")}) @RequestParam(required = false) String credentialId,
@Parameter(name = "issuerIdentifier", description = "Issuer identifier(did of BPN)", examples = {@ExampleObject(name = "bpn", value = "BPNL000000000000", description = "bpn"), @ExampleObject(description = "did", name = "did", value = "did:web:localhost:BPNL000000000000")}) @RequestParam(required = false) String issuerIdentifier,
@Parameter(name = "type", description = "Type of VC", examples = {@ExampleObject(name = "SummaryCredential", value = "SummaryCredential", description = "SummaryCredential"), @ExampleObject(description = "BpnCredential", name = "BpnCredential", value = "BpnCredential")}) @RequestParam(required = false) List<String> type,
@Parameter(name = "sortColumn", description = "Sort column name",
Expand All @@ -87,9 +88,23 @@ public ResponseEntity<PageImpl<VerifiableCredential>> getCredentials(@Parameter(
@Parameter(name = "sortTpe", description = "Sort order", examples = {@ExampleObject(value = "desc", name = "Descending order"), @ExampleObject(value = "asc", name = "Ascending order")}) @RequestParam(required = false, defaultValue = "desc") String sortTpe,
@Min(0) @Max(Integer.MAX_VALUE) @Parameter(description = "Page number, Page number start with zero") @RequestParam(required = false, defaultValue = "0") int pageNumber,
@Min(0) @Max(Integer.MAX_VALUE) @Parameter(description = "Number of records per page") @RequestParam(required = false, defaultValue = Integer.MAX_VALUE + "") int size,
@AsJwtParam @RequestParam(name = StringPool.AS_JWT, defaultValue = "false") boolean asJwt,

Principal principal) {
log.debug("Received request to get credentials. BPN: {}", getBPNFromToken(principal));
return ResponseEntity.status(HttpStatus.OK).body(holdersCredentialService.getCredentials(credentialId, issuerIdentifier, type, sortColumn, sortTpe, pageNumber, size, getBPNFromToken(principal)));
final GetCredentialsCommand command;
command = GetCredentialsCommand.builder()
.credentialId(credentialId)
.identifier(issuerIdentifier)
.type(type)
.sortColumn(sortColumn)
.sortType(sortTpe)
.pageNumber(pageNumber)
.size(size)
.asJwt(asJwt)
.callerBPN(getBPNFromToken(principal))
.build();
return ResponseEntity.status(HttpStatus.OK).body(holdersCredentialService.getCredentials(command));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.IssueFrameworkCredentialApiDocs;
import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.IssueVerifiableCredentialUsingBaseWalletApiDocs;
import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.ValidateVerifiableCredentialApiDocs;
import org.eclipse.tractusx.managedidentitywallets.command.GetCredentialsCommand;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.dto.CredentialVerificationRequest;
Expand All @@ -44,7 +45,6 @@
import org.eclipse.tractusx.managedidentitywallets.dto.IssueFrameworkCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.dto.IssueMembershipCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.service.IssuersCredentialService;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -90,7 +90,7 @@ public class IssuersCredentialController extends BaseController {
*/
@GetCredentialsApiDocs
@GetMapping(path = RestURI.ISSUERS_CREDENTIALS, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<PageImpl<VerifiableCredential>> getCredentials(@Parameter(name = "credentialId", description = "Credential Id", examples = {@ExampleObject(name = "Credential Id", value = "did:web:localhost:BPNL000000000000#12528899-160a-48bd-ba15-f396c3959ae9")}) @RequestParam(required = false) String credentialId,
public ResponseEntity<PageImpl<CredentialsResponse>> getCredentials(@Parameter(name = "credentialId", description = "Credential Id", examples = {@ExampleObject(name = "Credential Id", value = "did:web:localhost:BPNL000000000000#12528899-160a-48bd-ba15-f396c3959ae9")}) @RequestParam(required = false) String credentialId,
@Parameter(name = "holderIdentifier", description = "Holder identifier(did of BPN)", examples = {@ExampleObject(name = "bpn", value = "BPNL000000000001", description = "bpn"), @ExampleObject(description = "did", name = "did", value = "did:web:localhost:BPNL000000000001")}) @RequestParam(required = false) String holderIdentifier,
@Parameter(name = "type", description = "Type of VC", examples = {@ExampleObject(name = "SummaryCredential", value = "SummaryCredential", description = "SummaryCredential"), @ExampleObject(description = "BpnCredential", name = "BpnCredential", value = "BpnCredential")}) @RequestParam(required = false) List<String> type,
@Min(0) @Max(Integer.MAX_VALUE) @Parameter(description = "Page number, Page number start with zero") @RequestParam(required = false, defaultValue = "0") int pageNumber,
Expand All @@ -103,9 +103,23 @@ public ResponseEntity<PageImpl<VerifiableCredential>> getCredentials(@Parameter(
@ExampleObject(value = "credentialId", name = "Credential id")
}
) @RequestParam(required = false, defaultValue = "createdAt") String sortColumn,
@Parameter(name = "sortTpe", description = "Sort order", examples = {@ExampleObject(value = "desc", name = "Descending order"), @ExampleObject(value = "asc", name = "Ascending order")}) @RequestParam(required = false, defaultValue = "desc") String sortTpe, Principal principal) {
@Parameter(name = "sortTpe", description = "Sort order", examples = {@ExampleObject(value = "desc", name = "Descending order"), @ExampleObject(value = "asc", name = "Ascending order")}) @RequestParam(required = false, defaultValue = "desc") String sortTpe,
@AsJwtParam @RequestParam(name = StringPool.AS_JWT, defaultValue = "false") boolean asJwt,
Principal principal) {
log.debug("Received request to get credentials. BPN: {}", getBPNFromToken(principal));
return ResponseEntity.status(HttpStatus.OK).body(issuersCredentialService.getCredentials(credentialId, holderIdentifier, type, sortColumn, sortTpe, pageNumber, size, getBPNFromToken(principal)));
final GetCredentialsCommand command;
command = GetCredentialsCommand.builder()
.credentialId(credentialId)
.identifier(holderIdentifier)
.type(type)
.sortColumn(sortColumn)
.sortType(sortTpe)
.pageNumber(pageNumber)
.size(size)
.asJwt(asJwt)
.callerBPN(getBPNFromToken(principal))
.build();
return ResponseEntity.status(HttpStatus.OK).body(issuersCredentialService.getCredentials(command));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;
import org.eclipse.tractusx.managedidentitywallets.command.GetCredentialsCommand;
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.HoldersCredential;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet;
Expand Down Expand Up @@ -96,42 +97,49 @@ protected SpecificationUtil<HoldersCredential> getSpecificationUtil() {
* @param callerBPN the caller bpn
* @return the credentials
*/
public PageImpl<VerifiableCredential> getCredentials(String credentialId, String issuerIdentifier, List<String> type, String sortColumn, String sortType, int pageNumber, int size, String callerBPN) {
public PageImpl<CredentialsResponse> getCredentials(GetCredentialsCommand command) {
FilterRequest filterRequest = new FilterRequest();
filterRequest.setPage(pageNumber);
filterRequest.setSize(size);
filterRequest.setPage(command.getPageNumber());
filterRequest.setSize(command.getSize());

//Holder must be caller of API
Wallet holderWallet = commonService.getWalletByIdentifier(callerBPN);
Wallet holderWallet = commonService.getWalletByIdentifier(command.getCallerBPN());
filterRequest.appendCriteria(StringPool.HOLDER_DID, Operator.EQUALS, holderWallet.getDid());

if (StringUtils.hasText(issuerIdentifier)) {
Wallet issuerWallet = commonService.getWalletByIdentifier(issuerIdentifier);
if (StringUtils.hasText(command.getIdentifier())) {
Wallet issuerWallet = commonService.getWalletByIdentifier(command.getIdentifier());
filterRequest.appendCriteria(StringPool.ISSUER_DID, Operator.EQUALS, issuerWallet.getDid());
}

if (StringUtils.hasText(credentialId)) {
filterRequest.appendCriteria(StringPool.CREDENTIAL_ID, Operator.EQUALS, credentialId);
if (StringUtils.hasText(command.getCredentialId())) {
filterRequest.appendCriteria(StringPool.CREDENTIAL_ID, Operator.EQUALS, command.getCredentialId());
}
FilterRequest request = new FilterRequest();
if (!CollectionUtils.isEmpty(type)) {
if (!CollectionUtils.isEmpty(command.getType())) {
request.setPage(filterRequest.getPage());
request.setSize(filterRequest.getSize());
request.setCriteriaOperator(CriteriaOperator.OR);
for (String str : type) {
for (String str : command.getType()) {
request.appendCriteria(StringPool.TYPE, Operator.CONTAIN, str);
}
}

Sort sort = new Sort();
sort.setColumn(sortColumn);
sort.setSortType(SortType.valueOf(sortType.toUpperCase()));
sort.setColumn(command.getSortColumn());
sort.setSortType(SortType.valueOf(command.getSortType().toUpperCase()));
filterRequest.setSort(sort);
Page<HoldersCredential> filter = filter(filterRequest, request, CriteriaOperator.AND);

List<VerifiableCredential> list = new ArrayList<>(filter.getContent().size());
List<CredentialsResponse> list = new ArrayList<>(filter.getContent().size());

for (HoldersCredential credential : filter.getContent()) {
list.add(credential.getData());
CredentialsResponse cr = new CredentialsResponse();
if (command.isAsJwt()) {
cr.setJwt(CommonUtils.vcAsJwt(command.getIdentifier() != null ? commonService.getWalletByIdentifier(command.getIdentifier()) : holderWallet , holderWallet, credential.getData(), walletKeyService));
} else {
cr.setVc(credential.getData());
}
list.add(cr);
}

return new PageImpl<>(list, filter.getPageable(), filter.getTotalElements());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;
import org.eclipse.tractusx.managedidentitywallets.command.GetCredentialsCommand;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
import org.eclipse.tractusx.managedidentitywallets.constant.MIWVerifiableCredentialType;
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;
Expand Down Expand Up @@ -132,42 +133,48 @@ protected SpecificationUtil<IssuersCredential> getSpecificationUtil() {
* @param callerBPN the caller bpn
* @return the credentials
*/
public PageImpl<VerifiableCredential> getCredentials(String credentialId, String holderIdentifier, List<String> type, String sortColumn, String sortType, int pageNumber, int size, String callerBPN) {
public PageImpl<CredentialsResponse> getCredentials(GetCredentialsCommand command) {
FilterRequest filterRequest = new FilterRequest();
filterRequest.setSize(size);
filterRequest.setPage(pageNumber);
filterRequest.setSize(command.getSize());
filterRequest.setPage(command.getPageNumber());

//Issuer must be caller of API
Wallet issuerWallet = commonService.getWalletByIdentifier(callerBPN);
Wallet issuerWallet = commonService.getWalletByIdentifier(command.getCallerBPN());
filterRequest.appendCriteria(StringPool.ISSUER_DID, Operator.EQUALS, issuerWallet.getDid());

if (StringUtils.hasText(holderIdentifier)) {
Wallet holderWallet = commonService.getWalletByIdentifier(holderIdentifier);
if (StringUtils.hasText(command.getIdentifier())) {
Wallet holderWallet = commonService.getWalletByIdentifier(command.getIdentifier());
filterRequest.appendCriteria(StringPool.HOLDER_DID, Operator.EQUALS, holderWallet.getDid());
}

if (StringUtils.hasText(credentialId)) {
filterRequest.appendCriteria(StringPool.CREDENTIAL_ID, Operator.EQUALS, credentialId);
if (StringUtils.hasText(command.getCredentialId())) {
filterRequest.appendCriteria(StringPool.CREDENTIAL_ID, Operator.EQUALS, command.getCredentialId());
}
FilterRequest request = new FilterRequest();
if (!CollectionUtils.isEmpty(type)) {
if (!CollectionUtils.isEmpty(command.getType())) {
request.setPage(filterRequest.getPage());
request.setSize(filterRequest.getSize());
request.setCriteriaOperator(CriteriaOperator.OR);
for (String str : type) {
for (String str : command.getType()) {
request.appendCriteria(StringPool.TYPE, Operator.CONTAIN, str);
}
}

Sort sort = new Sort();
sort.setColumn(sortColumn);
sort.setSortType(SortType.valueOf(sortType.toUpperCase()));
sort.setColumn(command.getSortColumn());
sort.setSortType(SortType.valueOf(command.getSortType().toUpperCase()));
filterRequest.setSort(sort);
Page<IssuersCredential> filter = filter(filterRequest, request, CriteriaOperator.AND);

List<VerifiableCredential> list = new ArrayList<>(filter.getContent().size());
List<CredentialsResponse> list = new ArrayList<>(filter.getContent().size());
for (IssuersCredential credential : filter.getContent()) {
list.add(credential.getData());
CredentialsResponse cr = new CredentialsResponse();
if (command.isAsJwt()) {
cr.setJwt(CommonUtils.vcAsJwt(issuerWallet, command.getIdentifier() != null ? commonService.getWalletByIdentifier(command.getIdentifier()) : issuerWallet, credential.getData(), walletKeyService));
} else {
cr.setVc(credential.getData());
}
list.add(cr);
}
return new PageImpl<>(list, filter.getPageable(), filter.getTotalElements());
}
Expand Down
Loading

0 comments on commit 4216e0d

Please sign in to comment.