Skip to content

Commit

Permalink
Add expired batch check for wallet lockup endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
slaurenz committed Feb 25, 2022
1 parent 1263ab4 commit fb17239
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down Expand Up @@ -75,8 +77,9 @@ public class HashesEntity {
/**
* ID of the Batch.
*/
@Column(name = "batch_id", length = 36)
private String batchId;
@OneToOne()
@JoinColumn(name = "batch_id")
private BatchListEntity batch;

/**
* Update status of the hash value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package eu.europa.ec.dgc.revocationdistribution.repository;

import eu.europa.ec.dgc.revocationdistribution.entity.HashesEntity;
import java.time.ZonedDateTime;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
Expand All @@ -34,10 +35,15 @@ public interface HashesRepository extends JpaRepository<HashesEntity, String> {
void setAllUpdatedStatesToFalse();

@Modifying
@Query("DELETE HashesEntity h WHERE h.batchId = null")
@Query("DELETE HashesEntity h WHERE h.batch = null")
void deleteAllOrphanedHashes();

@Query("SELECT h.id FROM HashesEntity h WHERE h.id IN :hashes")
List<String> getHashesPresentInListAndDb(@Param("hashes") List<String> hashes);

@Query("SELECT h.id FROM HashesEntity h INNER JOIN h.batch b WHERE h.id IN :hashes AND b.expires > :checkTime")
List<String> getHashesPresentInListAndDbAndNotExpired(
@Param("hashes") List<String> hashes,
@Param("checkTime") ZonedDateTime checkTime);

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.security.PublicKey;
import java.text.ParseException;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -107,7 +108,7 @@ public List<String> checkForRevocation(List<RevocationCheckTokenPayload> tokenPa
List<String> hashes = tokenPayloads.stream().map(RevocationCheckTokenPayload::getPayload)
.flatMap(List::stream).collect(Collectors.toList());

return hashesRepository.getHashesPresentInListAndDb(hashes);
return hashesRepository.getHashesPresentInListAndDbAndNotExpired(hashes, ZonedDateTime.now());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ public class RevocationListService {
@Transactional
public void updateRevocationListBatch(String batchId, RevocationBatchDto revocationBatchDto) {

saveBatchList(batchId, revocationBatchDto);
BatchListEntity batchListEntity = saveBatchList(batchId, revocationBatchDto);

List<HashesEntity> hashes = new ArrayList<>();

for (RevocationBatchDto.BatchEntryDto hash : revocationBatchDto.getEntries()) {
try {
hashes.add(getHashEntity(batchId, hash, revocationBatchDto.getKid()));
hashes.add(getHashEntity(batchListEntity, hash, revocationBatchDto.getKid()));
} catch (IndexOutOfBoundsException e) {
log.error("Error calculating x,y,z. Hash value length is to short: {}",
hash.getHash().getBytes(StandardCharsets.UTF_8).length);
Expand Down Expand Up @@ -420,7 +420,7 @@ private String decodeBase64Hash(String b64Hash) {
}


private void saveBatchList(String batchId, RevocationBatchDto revocationBatchDto) {
private BatchListEntity saveBatchList(String batchId, RevocationBatchDto revocationBatchDto) {
BatchListEntity batchListEntity = new BatchListEntity();

batchListEntity.setBatchId(batchId);
Expand All @@ -429,11 +429,11 @@ private void saveBatchList(String batchId, RevocationBatchDto revocationBatchDto
batchListEntity.setType(BatchListEntity.RevocationHashType.valueOf(revocationBatchDto.getHashType().name()));
batchListEntity.setKid(revocationBatchDto.getKid());

batchListRepository.save(batchListEntity);
return batchListRepository.save(batchListEntity);
}


private HashesEntity getHashEntity(String batchId, RevocationBatchDto.BatchEntryDto hash, String kid)
private HashesEntity getHashEntity(BatchListEntity batch, RevocationBatchDto.BatchEntryDto hash, String kid)
throws IndexOutOfBoundsException {

String hexHash = decodeBase64Hash(hash.getHash());
Expand All @@ -443,7 +443,7 @@ private HashesEntity getHashEntity(String batchId, RevocationBatchDto.BatchEntry
hashesEntity.setY(hexHash.charAt(1));
hashesEntity.setZ(hexHash.charAt(2));
hashesEntity.setKid(kid);
hashesEntity.setBatchId(batchId);
hashesEntity.setBatch(batch);
hashesEntity.setUpdated(true);

return hashesEntity;
Expand Down

0 comments on commit fb17239

Please sign in to comment.