Skip to content

Commit

Permalink
feat: membership credentials api added
Browse files Browse the repository at this point in the history
  • Loading branch information
thackerronak committed May 29, 2023
1 parent 57c8803 commit b1a485a
Show file tree
Hide file tree
Showing 16 changed files with 355 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@

package org.eclipse.tractusx.managedidentitywallets.config;

import org.eclipse.tractusx.managedidentitywallets.exception.BadDataException;
import org.eclipse.tractusx.managedidentitywallets.exception.DuplicateWalletProblem;
import org.eclipse.tractusx.managedidentitywallets.exception.ForbiddenException;
import org.eclipse.tractusx.managedidentitywallets.exception.WalletNotFoundProblem;
import org.eclipse.tractusx.managedidentitywallets.exception.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand Down Expand Up @@ -99,4 +96,18 @@ ProblemDetail handleBadDataException(BadDataException e) {
return problemDetail;
}

/**
* Handle duplicate credential problem problem detail.
*
* @param e the e
* @return the problem detail
*/
@ExceptionHandler(DuplicateCredentialProblem.class)
ProblemDetail handleDuplicateCredentialProblem(DuplicateCredentialProblem e) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT, e.getMessage());
problemDetail.setTitle(e.getMessage());
problemDetail.setProperty(TIMESTAMP, System.currentTimeMillis());
return problemDetail;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers(new AntPathRequestMatcher(RestURI.API_WALLETS_IDENTIFIER, GET.name())).hasAnyRole(ApplicationConstant.ROLE_VIEW_WALLET, ApplicationConstant.ROLE_VIEW_WALLETS) //get wallet by BPN
.requestMatchers(new AntPathRequestMatcher(RestURI.API_WALLETS_IDENTIFIER_CREDENTIALS, POST.name())).hasAnyRole(ApplicationConstant.ROLE_VIEW_WALLET, ApplicationConstant.ROLE_VIEW_WALLETS) //Store credential
.requestMatchers(new AntPathRequestMatcher(RestURI.CREDENTIALS, GET.name())).hasAnyRole(ApplicationConstant.ROLE_VIEW_WALLET, ApplicationConstant.ROLE_VIEW_WALLETS) //get credentials
.requestMatchers(new AntPathRequestMatcher(RestURI.CREDENTIALS_ISSUER_MEMBERSHIP, POST.name())).hasAnyRole(ApplicationConstant.ROLE_UPDATE_WALLETS, ApplicationConstant.ROLE_UPDATE_WALLET, ApplicationConstant.ROLE_VIEW_WALLETS) //issue Membership Credential
.requestMatchers(new AntPathRequestMatcher("/error")).permitAll()
.and().oauth2ResourceServer()
.jwt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ private ApplicationConstant() {
public static final String ROLE_UPDATE_WALLET = "update_wallet";


/**
* The constant DID.
*/
public static final String DID = "did";

/**
* The constant BPN.
*/
public static final String BPN = "bpn";


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ private RestURI() {
*/
public static final String API_WALLETS_IDENTIFIER = "/api/wallets/{identifier}";

/**
* The constant API_WALLETS_IDENTIFIER_CREDENTIALS.
*/
public static final String API_WALLETS_IDENTIFIER_CREDENTIALS = "/api/wallets/{identifier}/credentials";
/**
* The constant CREDENTIALS.
*/
public static final String CREDENTIALS = "/api/credentials";
/**
* The constant CREDENTIALS_ISSUER_MEMBERSHIP.
*/
public static final String CREDENTIALS_ISSUER_MEMBERSHIP = "/api/credentials/issuer/membership";

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,54 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Credential;
import org.eclipse.tractusx.managedidentitywallets.dto.IssueMembershipCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.service.CredentialService;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* The type Credential controller.
*/
@RestController
@RequiredArgsConstructor
@Tag(name = "VerifiableCredentials")
public class CredentialController {

private final CredentialService service;

/**
* Gets credentials.
*
* @param holderIdentifier the holder identifier
* @param id the id
* @param issuerIdentifier the issuer identifier
* @param type the type
* @return the credentials
*/
@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.CREDENTIALS, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Credential>> getCredentials(@RequestParam(required = false) String holderIdentifier, @RequestParam(required = false) String id, @RequestParam(required = false) String issuerIdentifier, @RequestParam(required = false) List<String> type) {
return ResponseEntity.status(HttpStatus.OK).body(service.getCredentials(holderIdentifier, id, issuerIdentifier, type));
}

/**
* Issue membership credential response entity.
*
* @param issueMembershipCredentialRequest the issue membership credential request
* @return the response entity
*/
@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")
@PostMapping(path = RestURI.CREDENTIALS_ISSUER_MEMBERSHIP, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<VerifiableCredential> issueMembershipCredential(@Valid @RequestBody IssueMembershipCredentialRequest issueMembershipCredentialRequest) {
return ResponseEntity.status(HttpStatus.CREATED).body(service.issueMembershipCredential(issueMembershipCredentialRequest));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public ResponseEntity<Wallet> createWallet(@Valid @RequestBody CreateWalletReque
/**
* Store credential response entity.
*
* @param data the data
* @param bpn the bpn
* @param data the data
* @param identifier the identifier
* @return the response entity
*/
@Operation(summary = "Store Verifiable Credential", description = "Permission: **update_wallets** OR **update_wallet** (The BPN of wallet to extract credentials from must equal BPN of caller) \n\n Store a verifiable credential in the wallet of the given identifier")
Expand All @@ -75,7 +75,8 @@ public ResponseEntity<Map<String, String>> storeCredential(@RequestBody Map<Stri
/**
* Gets wallet by bpn.
*
* @param identifier the identifier
* @param identifier the identifier
* @param withCredentials the with credentials
* @return the wallet by bpn
*/
@Operation(summary = "Retrieve wallet by identifier", description = "Permission: **view_wallets** OR **view_wallet** (The BPN of Wallet to retrieve must equal the BPN of caller) \n\n Retrieve single wallet by identifier, with or without its credentials")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;


/**
* The type Credential.
*/
@Getter
@Setter
@Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public class Wallet extends BaseEntity {
@Transient
private List<VerifiableCredential> verifiableCredentials;

/**
* Sets did.
*
* @param did the did
*/
public void setDid(String did) {
this.did = URLDecoder.decode(did, Charset.defaultCharset());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,42 @@

import java.util.List;

/**
* The interface Credential repository.
*/
public interface CredentialRepository extends JpaRepository<Credential, Long> {
/**
* Gets by holder.
*
* @param id the id
* @return the by holder
*/
List<Credential> getByHolder(Long id);

/**
* Gets credentials by holder.
*
* @param holder the holder
* @return the credentials by holder
*/
@Query("select data from Credential where holder=:holder")
List<VerifiableCredential> getCredentialsByHolder(@Param("holder") Long holder);

/**
* Gets by holder and type.
*
* @param holderWalletId the holder wallet id
* @param type the type
* @return the by holder and type
*/
Credential getByHolderAndType(Long holderWalletId, String type);

/**
* Exists by holder and type boolean.
*
* @param holderWalletId the holder wallet id
* @param type the type
* @return the boolean
*/
boolean existsByHolderAndType(Long holderWalletId, String type);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* *******************************************************************************
* Copyright (c) 2021,2023 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 jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.*;

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

@NotBlank
@Size(min = 5, max = 255)
private String bpn;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* *******************************************************************************
* Copyright (c) 2021,2023 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.exception;

/**
* The type Duplicate wallet problem.
*/
public class DuplicateCredentialProblem extends RuntimeException {

/**
* Instantiates a new Duplicate wallet problem.
*/
public DuplicateCredentialProblem() {
}

/**
* Instantiates a new Duplicate wallet problem.
*
* @param message the message
*/
public DuplicateCredentialProblem(String message) {
super(message);
}

/**
* Instantiates a new Duplicate wallet problem.
*
* @param message the message
* @param cause the cause
*/
public DuplicateCredentialProblem(String message, Throwable cause) {
super(message, cause);
}

/**
* Instantiates a new Duplicate wallet problem.
*
* @param cause the cause
*/
public DuplicateCredentialProblem(Throwable cause) {
super(cause);
}

}
Loading

0 comments on commit b1a485a

Please sign in to comment.