Skip to content

Commit

Permalink
Refactor DID related implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
f11h committed Mar 20, 2024
1 parent 4df21d6 commit 250388f
Show file tree
Hide file tree
Showing 22 changed files with 264 additions and 172 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package tng.trustnetwork.keydistribution.clients;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import tng.trustnetwork.keydistribution.model.DidDocument;

@FeignClient(value = "universalresolver", url = "${universal.resolver}",
configuration = UniversalResolverClientConfig.class)
public interface UniversalResolverClient {

@GetMapping(value = "/{didKey}", produces = "application/json")
ResponseEntity<DidDocument> getDidDocument(@PathVariable("didKey") String didKey);
String getDidDocument(@PathVariable("didKey") String didKey);

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package tng.trustnetwork.keydistribution.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import java.time.ZonedDateTime;
import java.util.List;
Expand All @@ -31,7 +28,7 @@ public class DecentralizedIdentifierEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition = "BIGINT")
private String id;
private Long id;

@Column(name = "created_at", nullable = false)
private ZonedDateTime createdAt = ZonedDateTime.now();
Expand All @@ -42,7 +39,7 @@ public class DecentralizedIdentifierEntity {
@OneToMany(mappedBy = "parentDocument")
private List<VerificationMethodEntity> verificationMethods;

@Column(name = "raw", length = 10_000)
@Column(name = "raw", length = 10_000_000)
private String raw;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tng.trustnetwork.keydistribution.entity;

import jakarta.persistence.Column;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@DiscriminatorValue("EC")
public class EcPublicKeyJwkEntity extends PublicKeyJwkEntity {

@Column(name = "crv", length = 100)
private String crv;

@Column(name = "x", length = 100)
private String xvalue;

@Column(name = "y", length = 100)
private String yvalue;

}
Original file line number Diff line number Diff line change
@@ -1,48 +1,39 @@
package tng.trustnetwork.keydistribution.entity;

import jakarta.persistence.Column;
import jakarta.persistence.DiscriminatorColumn;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.Table;
import java.time.ZonedDateTime;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Data
@Getter
@Setter
@Entity
@Table(name = "public_key_jwk")
@AllArgsConstructor
@NoArgsConstructor
public class PublicKeyJwkEntity {
@Entity
@Table(name = "public_key_jwk")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "kty", columnDefinition = "varchar(10)")
public abstract class PublicKeyJwkEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private String id;

@Column(name = "crv", length = 100)
private String crv;

@Column(name = "kty", length = 100)
private String kty;
private Long id;

@Column(name = "x", length = 100)
private String xvalue;

@Column(name = "y", length = 100)
private String yvalue;
@Column(name = "created_at", nullable = false)
private ZonedDateTime createdAt = ZonedDateTime.now();

@Column(name = "x5c", length = 7000)
private String x5c;

@Column(name = "created_at", nullable = false)
private ZonedDateTime createdAt = ZonedDateTime.now();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tng.trustnetwork.keydistribution.entity;

import jakarta.persistence.Column;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@DiscriminatorValue("RSA")
public class RsaPublicKeyJwkEntity extends PublicKeyJwkEntity {

@Column(name = "n", length = 1000)
private String nvalue;

@Column(name = "e", length = 1000)
private String evalue;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@
@AllArgsConstructor
@NoArgsConstructor
public class VerificationMethodEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition = "BIGINT")
private String id;
private Long id;

@Column(name = "vm_id", nullable = true, length = 100)
@Column(name = "vm_id", length = 100)
private String vmId;

@Column(name = "type", nullable = true, length = 100)
@Column(name = "type", length = 100)
private String type;

@Column(name = "controller", nullable = true, length = 100)
@Column(name = "controller", length = 100)
private String controller;

@OneToOne(cascade = CascadeType.ALL)
Expand All @@ -46,7 +47,7 @@ public class VerificationMethodEntity {
@ManyToOne
@JoinColumn(name = "parent_document_id")
private DecentralizedIdentifierEntity parentDocument;

@Column(name = "created_at", nullable = false)
private ZonedDateTime createdAt = ZonedDateTime.now();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package tng.trustnetwork.keydistribution.mapper;

import java.util.List;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.SubclassExhaustiveStrategy;
import org.mapstruct.SubclassMapping;
import tng.trustnetwork.keydistribution.entity.DecentralizedIdentifierEntity;
import tng.trustnetwork.keydistribution.entity.EcPublicKeyJwkEntity;
import tng.trustnetwork.keydistribution.entity.PublicKeyJwkEntity;
import tng.trustnetwork.keydistribution.entity.RsaPublicKeyJwkEntity;
import tng.trustnetwork.keydistribution.entity.VerificationMethodEntity;
import tng.trustnetwork.keydistribution.model.DidDocument;
import tng.trustnetwork.keydistribution.model.EcPublicKeyJwk;
import tng.trustnetwork.keydistribution.model.JwkVerificationMethod;
import tng.trustnetwork.keydistribution.model.PublicKeyJwk;
import tng.trustnetwork.keydistribution.model.RsaPublicKeyJwk;
import tng.trustnetwork.keydistribution.model.StringOrObject;
import tng.trustnetwork.keydistribution.model.VerificationMethod;

@Mapper(componentModel = "spring", subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION)
public interface DidMapper {

@Mapping(target = "didId", source = "didDocument.id")
@Mapping(target = "id", ignore = true)
@Mapping(target = "verificationMethods", source = "didDocument.verificationMethod")
DecentralizedIdentifierEntity toEntity(DidDocument didDocument, String raw);

@SubclassMapping(target = RsaPublicKeyJwkEntity.class, source = RsaPublicKeyJwk.class)
@SubclassMapping(target = EcPublicKeyJwkEntity.class, source = EcPublicKeyJwk.class)
PublicKeyJwkEntity toEntity(PublicKeyJwk publicKeyJwk);

@Mapping(target = "xvalue", source = "x")
@Mapping(target = "yvalue", source = "y")
EcPublicKeyJwkEntity toEntity(EcPublicKeyJwk model);

@Mapping(target = "evalue", source = "e")
@Mapping(target = "nvalue", source = "n")
RsaPublicKeyJwkEntity toEntity(RsaPublicKeyJwk model);

@SubclassMapping(target = VerificationMethodEntity.class, source = JwkVerificationMethod.class)
VerificationMethodEntity toEntity(VerificationMethod verificationMethod);

@Mapping(target = "type", constant = "JsonWebKey2020")
@Mapping(target = "vmId", source = "verificationMethod.id")
@Mapping(target = "id", ignore = true)
VerificationMethodEntity toEntity(JwkVerificationMethod verificationMethod);

default <T> T unwrap(StringOrObject<T> wrapped) {
return wrapped.getObjectValue();
}

default String toSingleString(List<String> list) {

return String.join(",", list);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package tng.trustnetwork.keydistribution.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.repository.query.parser.Part;

@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public class EcPublicKeyJwk extends PublicKeyJwk {

private Curve crv;

private String x;
@JsonProperty("x")
private String xvalue;

private String y;
@JsonProperty("y")
private String yvalue;

public enum Curve {
@JsonEnumDefaultValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package tng.trustnetwork.keydistribution.repository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import tng.trustnetwork.keydistribution.entity.DecentralizedIdentifierEntity;

public interface DecentralizedIdentifierRepository extends JpaRepository<DecentralizedIdentifierEntity, String> {
List<DecentralizedIdentifierEntity> findAllById(String id);

List<DecentralizedIdentifierEntity> findByDidId(String id);
public interface DecentralizedIdentifierRepository extends JpaRepository<DecentralizedIdentifierEntity, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tng.trustnetwork.keydistribution.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import tng.trustnetwork.keydistribution.entity.PublicKeyJwkEntity;

public interface PublicKeyJwkRepository extends JpaRepository<PublicKeyJwkEntity, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import tng.trustnetwork.keydistribution.entity.TrustedIssuerEntity;

public interface TrustedIssuerRepository extends JpaRepository<TrustedIssuerEntity, String> {
public interface TrustedIssuerRepository extends JpaRepository<TrustedIssuerEntity, Long> {

void deleteAllByEtag(String etag);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tng.trustnetwork.keydistribution.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import tng.trustnetwork.keydistribution.entity.VerificationMethodEntity;

public interface VerificationMethodRepository extends JpaRepository<VerificationMethodEntity, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package tng.trustnetwork.keydistribution.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tng.trustnetwork.keydistribution.entity.DecentralizedIdentifierEntity;
import tng.trustnetwork.keydistribution.entity.VerificationMethodEntity;
import tng.trustnetwork.keydistribution.mapper.DidMapper;
import tng.trustnetwork.keydistribution.model.DidDocument;
import tng.trustnetwork.keydistribution.repository.DecentralizedIdentifierRepository;
import tng.trustnetwork.keydistribution.repository.PublicKeyJwkRepository;
import tng.trustnetwork.keydistribution.repository.VerificationMethodRepository;

@Slf4j
@Service
Expand All @@ -17,25 +18,33 @@ public class DecentralizedIdentifierService {

private final DecentralizedIdentifierRepository decentralizedIdentifierRepository;

public void updateDecentralizedIdentifierList(DidDocument didDocumentUnmarshal, String raw) {
// TODO Auto-generated method stub
if (null != didDocumentUnmarshal) {
DecentralizedIdentifierEntity decentralizedIdentifierEntity = new DecentralizedIdentifierEntity();
decentralizedIdentifierEntity.setDidId(didDocumentUnmarshal.getId());
private final VerificationMethodRepository verificationMethodRepository;

List<VerificationMethodEntity> verificationMethodEntities = new ArrayList<>();
private final PublicKeyJwkRepository publicKeyJwkRepository;

/*List<ResolvedKey> resolvedKeys = didDocumentUnmarshal.getVerificationMethod().getResolvedKeys();
private final DidMapper didMapper;

for (ResolvedKey resolvedKey : resolvedKeys) {
verificationMethodEntities.add(
decentralizedIdentifierMapper.resolvedKeyToVerificationMethodEntity(resolvedKey));
}
/**
* Update list of stored DID with given Document.
*
* @param didDocument Parsed DIDDocument
* @param raw RAW-JSON-Value of the DID Document. This will be stored to allow validation of LD-Proof later.
*/
public void updateDecentralizedIdentifierList(DidDocument didDocument, String raw) {

decentralizedIdentifierEntity.setVerificationMethod(verificationMethodEntities);*/
decentralizedIdentifierRepository.save(decentralizedIdentifierEntity);
DecentralizedIdentifierEntity didEntity = didMapper.toEntity(didDocument, raw);

}
decentralizedIdentifierRepository.save(didEntity);

didEntity.getVerificationMethods()
.stream()
.filter(Objects::nonNull)
.forEach(verificationMethod -> {

verificationMethod.setParentDocument(didEntity);
publicKeyJwkRepository.save(verificationMethod.getPublicKeyJwk());
verificationMethodRepository.save(verificationMethod);
});
}

}
Loading

0 comments on commit 250388f

Please sign in to comment.