Skip to content

Commit

Permalink
feat: Store credntial API, testcase mofitication based on DidDocument…
Browse files Browse the repository at this point in the history
… Java POJO
  • Loading branch information
nitin-vavdiya committed May 26, 2023
1 parent 4939ddb commit 569097b
Show file tree
Hide file tree
Showing 14 changed files with 614 additions and 24 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies {
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:${openApiVersion}"
implementation 'org.liquibase:liquibase-core'
implementation 'org.eclipse.tractusx.ssi.lib:cx-ssi-lib:0.0.4'
implementation 'org.eclipse.tractusx.ssi:cx-ssi-agent-lib:0.0.3'
runtimeOnly 'org.postgresql:postgresql'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
Expand All @@ -53,8 +54,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.json', name: 'json', version: '20230227'
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@

package org.eclipse.tractusx.managedidentitywallets.config;

import org.eclipse.tractusx.managedidentitywallets.exception.DidDocumentsNotFoundProblem;
import org.eclipse.tractusx.managedidentitywallets.exception.DuplicateWalletProblem;
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 @@ -83,4 +81,33 @@ ProblemDetail handleDidDocumentNotFoundProblem(DidDocumentsNotFoundProblem e) {
problemDetail.setProperty(TIMESTAMP, System.currentTimeMillis());
return problemDetail;
}

/**
* Handle forbidden exception problem detail.
*
* @param e the e
* @return the problem detail
*/
@ExceptionHandler(ForbiddenException.class)
ProblemDetail handleForbiddenException(ForbiddenException e) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.FORBIDDEN, e.getMessage());
problemDetail.setTitle(e.getMessage());
problemDetail.setProperty(TIMESTAMP, System.currentTimeMillis());
return problemDetail;
}

/**
* Handle bad data exception problem detail.
*
* @param e the e
* @return the problem detail
*/
@ExceptionHandler(BadDataException.class)
ProblemDetail handleBadDataException(BadDataException e) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, 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 @@ -71,8 +71,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers(new AntPathRequestMatcher("/actuator/health/**")).permitAll()
.requestMatchers(new AntPathRequestMatcher(RestURI.DID_DOCUMENTS, GET.name())).permitAll() //Get did document
.requestMatchers(new AntPathRequestMatcher(RestURI.WALLETS, POST.name())).hasRole(ApplicationConstant.ROLE_ADD_WALLETS) //Create wallet
.requestMatchers(new AntPathRequestMatcher( RestURI.WALLETS, GET.name())).hasAnyRole( ApplicationConstant.ROLE_VIEW_WALLETS) //Get all wallet
.requestMatchers(new AntPathRequestMatcher( RestURI.WALLETS_BY_BPN, GET.name())).hasAnyRole(ApplicationConstant.ROLE_VIEW_WALLET, ApplicationConstant.ROLE_VIEW_WALLETS) //get wallet by BPN
.requestMatchers(new AntPathRequestMatcher(RestURI.WALLETS, GET.name())).hasAnyRole(ApplicationConstant.ROLE_VIEW_WALLETS) //Get all wallet
.requestMatchers(new AntPathRequestMatcher(RestURI.WALLETS_BY_BPN, GET.name())).hasAnyRole(ApplicationConstant.ROLE_VIEW_WALLET, ApplicationConstant.ROLE_VIEW_WALLETS) //get wallet by BPN
.requestMatchers(new AntPathRequestMatcher(RestURI.WALLETS_BY_BPN_CREDENTIALS, POST.name())).hasAnyRole(ApplicationConstant.ROLE_VIEW_WALLET, ApplicationConstant.ROLE_VIEW_WALLETS) //Store credential
.requestMatchers(new AntPathRequestMatcher("/error")).permitAll()
.and().oauth2ResourceServer()
.jwt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ private RestURI() {
*/
public static final String WALLETS_BY_BPN = "/api/wallets/{bpn}";

public static final String WALLETS_BY_BPN_CREDENTIALS = "/api/wallets/{bpn}/credentials";


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package org.eclipse.tractusx.managedidentitywallets.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.service.DidDocumentService;
Expand All @@ -37,6 +39,7 @@
*/
@RestController
@RequiredArgsConstructor
@Tag(name = "DIDDocument")
public class DidDocumentController {
private final DidDocumentService service;

Expand All @@ -46,6 +49,7 @@ public class DidDocumentController {
* @param identifier the identifier
* @return the did document
*/
@Operation(description = "Resolve the DID document for a given DID or BPN", summary = "Resolve DID Document")
@GetMapping(path = RestURI.DID_DOCUMENTS, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<DidDocument> getDidDocument(@PathVariable(name = "identifier") String identifier) {
return ResponseEntity.status(HttpStatus.OK).body(service.getDidDocument(identifier));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package org.eclipse.tractusx.managedidentitywallets.controller;

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;
Expand All @@ -33,12 +35,14 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
* The type Wallet controller.
*/
@RestController
@RequiredArgsConstructor
@Tag(name = "Wallets")
public class WalletController {

private final WalletService service;
Expand All @@ -49,17 +53,32 @@ public class WalletController {
* @param request the request
* @return the response entity
*/
@Operation(summary = "Create Wallet", description = "Permission: **add_wallets** \n\n Create a wallet and store it")
@PostMapping(path = RestURI.WALLETS, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Wallet> createWallet(@Valid @RequestBody CreateWalletRequest request){
public ResponseEntity<Wallet> createWallet(@Valid @RequestBody CreateWalletRequest request) {
return ResponseEntity.status(HttpStatus.CREATED).body(service.createWallet(request));
}

/**
* Store credential response entity.
*
* @param data the data
* @param bpn the bpn
* @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")
@PostMapping(path = RestURI.WALLETS_BY_BPN_CREDENTIALS, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, String>> storeCredential(@RequestBody Map<String, Object> data, @PathVariable(name = "bpn") String bpn) {
return ResponseEntity.status(HttpStatus.CREATED).body(service.storeCredential(data, bpn));
}

/**
* Gets wallet by bpn.
*
* @param bpn the bpn
* @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")
@GetMapping(path = RestURI.WALLETS_BY_BPN, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Wallet> getWalletByBpn(@PathVariable(name = "bpn") String bpn) {
return ResponseEntity.status(HttpStatus.OK).body(service.getWalletByBpn(bpn));
Expand All @@ -70,8 +89,9 @@ public ResponseEntity<Wallet> getWalletByBpn(@PathVariable(name = "bpn") String
*
* @return the wallets
*/
@Operation(summary = "List of wallets", description = "Permission: **view_wallets** \n\n Retrieve list of registered wallets")
@GetMapping(path = RestURI.WALLETS, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Wallet>> getWallets() {
return ResponseEntity.status(HttpStatus.OK).body(service.getWallets());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* *******************************************************************************
* 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.dao.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;


@Getter
@Setter
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Credential extends BaseEntity {


@Id
@JsonIgnore
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition = "serial", nullable = false, unique = true)
private Long id;

@Column(nullable = false)
private Long holder;

@Column(nullable = false)
private Long issuer;

@Column(nullable = false)
private String type;

@Column(nullable = false)
private String data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* *******************************************************************************
* 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.dao.repository;

import org.eclipse.tractusx.managedidentitywallets.dao.entity.Credential;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CredentialRepository extends JpaRepository<Credential, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* *******************************************************************************
* 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 Bad data exception.
*/
public class BadDataException extends RuntimeException {

/**
* Instantiates a new Bad data exception.
*/
public BadDataException() {
}

/**
* Instantiates a new Bad data exception.
*
* @param message the message
*/
public BadDataException(String message) {
super(message);
}

/**
* Instantiates a new Bad data exception.
*
* @param message the message
* @param cause the cause
*/
public BadDataException(String message, Throwable cause) {
super(message, cause);
}

/**
* Instantiates a new Bad data exception.
*
* @param cause the cause
*/
public BadDataException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* *******************************************************************************
* 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 Forbidden exception.
*/
public class ForbiddenException extends RuntimeException {

/**
* Instantiates a new Forbidden exception.
*/
public ForbiddenException() {
}

/**
* Instantiates a new Forbidden exception.
*
* @param message the message
*/
public ForbiddenException(String message) {
super(message);
}

/**
* Instantiates a new Forbidden exception.
*
* @param message the message
* @param cause the cause
*/
public ForbiddenException(String message, Throwable cause) {
super(message, cause);
}

/**
* Instantiates a new Forbidden exception.
*
* @param cause the cause
*/
public ForbiddenException(Throwable cause) {
super(cause);
}
}
Loading

0 comments on commit 569097b

Please sign in to comment.