Skip to content

Commit

Permalink
fix: add asJwt to controllers and services
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 179b590 commit b604f3d
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 137 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ private StringPool() {

public static final String PRIVATE_KEY = "PRIVATE KEY";
public static final String PUBLIC_KEY = "PUBLIC KEY";
public static final String VC_JWT_KEY = "jwt";

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.tractusx.managedidentitywallets.apidocs.HoldersCredentialControllerApiDocs.GetCredentialsApiDocs;
import org.eclipse.tractusx.managedidentitywallets.apidocs.HoldersCredentialControllerApiDocs.IssueCredentialApiDoc;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
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;
Expand Down Expand Up @@ -101,8 +102,10 @@ public ResponseEntity<PageImpl<VerifiableCredential>> getCredentials(@Parameter(

@IssueCredentialApiDoc
@PostMapping(path = RestURI.CREDENTIALS, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<VerifiableCredential> issueCredential(@RequestBody Map<String, Object> data, Principal principal) {
public ResponseEntity<CredentialsResponse> issueCredential(@RequestBody Map<String, Object> data, Principal principal,
@RequestParam(name = "asJwt", defaultValue = "false") boolean asJwt
) {
log.debug("Received request to issue credential. BPN: {}", getBPNFromToken(principal));
return ResponseEntity.status(HttpStatus.CREATED).body(holdersCredentialService.issueCredential(data, getBPNFromToken(principal)));
return ResponseEntity.status(HttpStatus.CREATED).body(holdersCredentialService.issueCredential(data, getBPNFromToken(principal) , asJwt));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.IssueVerifiableCredentialUsingBaseWalletApiDocs;
import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.ValidateVerifiableCredentialApiDocs;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.dto.CredentialVerificationRequest;
import org.eclipse.tractusx.managedidentitywallets.dto.CredentialsResponse;
import org.eclipse.tractusx.managedidentitywallets.dto.IssueDismantlerCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.dto.IssueFrameworkCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.dto.IssueMembershipCredentialRequest;
Expand Down Expand Up @@ -113,7 +115,7 @@ public ResponseEntity<PageImpl<VerifiableCredential>> getCredentials(@Parameter(
*/
@IssueMembershipCredentialApiDoc
@PostMapping(path = RestURI.CREDENTIALS_ISSUER_MEMBERSHIP, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<VerifiableCredential> issueMembershipCredential(@Valid @RequestBody IssueMembershipCredentialRequest issueMembershipCredentialRequest, Principal principal) {
public ResponseEntity<CredentialsResponse> issueMembershipCredential(@Valid @RequestBody IssueMembershipCredentialRequest issueMembershipCredentialRequest, Principal principal) {
log.debug("Received request to issue membership credential. BPN: {}", getBPNFromToken(principal));
return ResponseEntity.status(HttpStatus.CREATED).body(issuersCredentialService.issueMembershipCredential(issueMembershipCredentialRequest, getBPNFromToken(principal)));
}
Expand All @@ -127,7 +129,7 @@ public ResponseEntity<VerifiableCredential> issueMembershipCredential(@Valid @Re
*/
@IssueDismantlerCredentialApiDoc
@PostMapping(path = RestURI.CREDENTIALS_ISSUER_DISMANTLER, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<VerifiableCredential> issueDismantlerCredential(@Valid @RequestBody IssueDismantlerCredentialRequest request, Principal principal) {
public ResponseEntity<CredentialsResponse> issueDismantlerCredential(@Valid @RequestBody IssueDismantlerCredentialRequest request, Principal principal) {
log.debug("Received request to issue dismantler credential. BPN: {}", getBPNFromToken(principal));
return ResponseEntity.status(HttpStatus.CREATED).body(issuersCredentialService.issueDismantlerCredential(request, getBPNFromToken(principal)));
}
Expand All @@ -141,7 +143,7 @@ public ResponseEntity<VerifiableCredential> issueDismantlerCredential(@Valid @Re
*/
@IssueFrameworkCredentialApiDocs
@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) {
public ResponseEntity<CredentialsResponse> issueFrameworkCredential(@Valid @RequestBody IssueFrameworkCredentialRequest request, Principal principal) {
log.debug("Received request to issue framework credential. BPN: {}", getBPNFromToken(principal));
return ResponseEntity.status(HttpStatus.CREATED).body(issuersCredentialService.issueFrameworkCredential(request, getBPNFromToken(principal)));
}
Expand All @@ -155,10 +157,10 @@ public ResponseEntity<VerifiableCredential> issueFrameworkCredential(@Valid @Req
*/
@PostMapping(path = RestURI.CREDENTIALS_VALIDATION, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ValidateVerifiableCredentialApiDocs
public ResponseEntity<Map<String, Object>> credentialsValidation(@RequestBody Map<String, Object> data,
public ResponseEntity<Map<String, Object>> credentialsValidation(@RequestBody CredentialVerificationRequest credentialVerificationRequest,
@Parameter(description = "Check expiry of VC") @RequestParam(name = "withCredentialExpiryDate", defaultValue = "false", required = false) boolean withCredentialExpiryDate) {
log.debug("Received request to validate verifiable credentials");
return ResponseEntity.status(HttpStatus.OK).body(issuersCredentialService.credentialsValidation(data, withCredentialExpiryDate));
return ResponseEntity.status(HttpStatus.OK).body(issuersCredentialService.credentialsValidation(credentialVerificationRequest, withCredentialExpiryDate));
}

/**
Expand All @@ -171,8 +173,10 @@ public ResponseEntity<Map<String, Object>> credentialsValidation(@RequestBody Ma
*/
@PostMapping(path = RestURI.ISSUERS_CREDENTIALS, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@IssueVerifiableCredentialUsingBaseWalletApiDocs
public ResponseEntity<VerifiableCredential> issueCredentialUsingBaseWallet(@Parameter(description = "Holder DID", examples = {@ExampleObject(description = "did", name = "did", value = "did:web:localhost:BPNL000000000000")}) @RequestParam(name = "holderDid") String holderDid, @RequestBody Map<String, Object> data, Principal principal) {
public ResponseEntity<CredentialsResponse> issueCredentialUsingBaseWallet(@Parameter(description = "Holder DID", examples = {@ExampleObject(description = "did", name = "did", value = "did:web:localhost:BPNL000000000000")}) @RequestParam(name = "holderDid") String holderDid,
@RequestBody Map<String, Object> data, Principal principal,
@RequestParam(name = "asJwt", defaultValue = "false") boolean asJwt) {
log.debug("Received request to issue verifiable credential. BPN: {}", getBPNFromToken(principal));
return ResponseEntity.status(HttpStatus.CREATED).body(issuersCredentialService.issueCredentialUsingBaseWallet(holderDid, data, getBPNFromToken(principal)));
return ResponseEntity.status(HttpStatus.CREATED).body(issuersCredentialService.issueCredentialUsingBaseWallet(holderDid, data, getBPNFromToken(principal) , asJwt));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* *******************************************************************************
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ******************************************************************************
*/

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);
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* *******************************************************************************
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.dto;

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

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

public class CredentialsResponse 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
@@ -1,6 +1,6 @@
/*
* *******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -27,6 +27,8 @@
import lombok.*;
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Set;

/**
Expand All @@ -49,4 +51,7 @@ public class IssueDismantlerCredentialRequest {

@Builder.Default
private Set<@NotBlank String> allowedVehicleBrands = Set.of();

@JsonProperty("asJwt")
private boolean asJwt;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* *******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -19,36 +19,41 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.*;


/**
* The type Issue framework credential request.
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class IssueFrameworkCredentialRequest {

@NotBlank(message = "Please provide holder identifier")
@Size(min = 5, max = 255, message = "Please provide valid identifier")
private String holderIdentifier;

@NotBlank(message = "Please provide type")
private String type;

@NotBlank(message = "Please provide contract-template")
@JsonProperty("contract-template")
private String contractTemplate;

@NotBlank(message = "Please provide contract-template")
@JsonProperty("contract-version")
private String contractVersion;
}
package org.eclipse.tractusx.managedidentitywallets.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.*;


/**
* The type Issue framework credential request.
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class IssueFrameworkCredentialRequest {

@NotBlank(message = "Please provide holder identifier")
@Size(min = 5, max = 255, message = "Please provide valid identifier")
private String holderIdentifier;

@NotBlank(message = "Please provide type")
private String type;

@NotBlank(message = "Please provide contract-template")
@JsonProperty("contract-template")
private String contractTemplate;

@NotBlank(message = "Please provide contract-template")
@JsonProperty("contract-version")
private String contractVersion;

@JsonProperty("asJwt")
private boolean asJwt;

}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* *******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -19,25 +19,31 @@
* ******************************************************************************
*/

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

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.*;
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;

/**
* The type Issue membership credential request.
*/
@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;
}
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.*;
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* The type Issue membership credential request.
*/
@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;

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


Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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.dto.CredentialsResponse;
import org.eclipse.tractusx.managedidentitywallets.exception.CredentialNotFoundProblem;
import org.eclipse.tractusx.managedidentitywallets.exception.ForbiddenException;
import org.eclipse.tractusx.managedidentitywallets.utils.CommonUtils;
Expand Down Expand Up @@ -143,7 +144,7 @@ public PageImpl<VerifiableCredential> getCredentials(String credentialId, String
* @param callerBpn the caller bpn
* @return the verifiable credential
*/
public VerifiableCredential issueCredential(Map<String, Object> data, String callerBpn) {
public CredentialsResponse issueCredential(Map<String, Object> data, String callerBpn , boolean asJwt) {
VerifiableCredential verifiableCredential = new VerifiableCredential(data);
Wallet issuerWallet = commonService.getWalletByIdentifier(verifiableCredential.getIssuer().toString());

Expand All @@ -167,9 +168,18 @@ public VerifiableCredential issueCredential(Map<String, Object> data, String cal
//Store Credential in holder table
credential = create(credential);

log.debug("VC type of {} issued to bpn ->{}", StringEscapeUtils.escapeJava(verifiableCredential.getTypes().toString()), StringEscapeUtils.escapeJava(callerBpn));
final CredentialsResponse cr = new CredentialsResponse();

// Return VC
return credential.getData();
if (asJwt) {
cr.setJwt(CommonUtils.vcAsJwt(issuerWallet, commonService.getWalletByIdentifier(callerBpn), credential.getData() , walletKeyService));
} else {
cr.setVc(credential.getData());
}

log.debug("VC type of {} issued to bpn ->{}", StringEscapeUtils.escapeJava(verifiableCredential.getTypes().toString()), StringEscapeUtils.escapeJava(callerBpn));

return cr;
}

private void isCredentialExistWithId(String holderDid, String credentialId) {
Expand Down
Loading

0 comments on commit b604f3d

Please sign in to comment.