Skip to content

Commit

Permalink
Merge pull request #488 from royllo/461-user-asset-data-management
Browse files Browse the repository at this point in the history
461 user asset data management
  • Loading branch information
straumat authored Jan 31, 2024
2 parents 480417c + 5693f98 commit f3c9ba1
Show file tree
Hide file tree
Showing 76 changed files with 1,611 additions and 71 deletions.
30 changes: 30 additions & 0 deletions backend/explorer-core/autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@
<version>${jdbc.driver.postgresql.version}</version>
</dependency>

<!-- Security dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- TODO Put this back when 0.13 will be released
<dependency>
<groupId>io.github.theborakompanioni</groupId>
<artifactId>spring-lnurl-auth-starter</artifactId>
<version>${bitcoin-spring-boot-starter.version}</version>
</dependency>-->
<dependency>
<groupId>com.github.theborakompanioni.bitcoin-spring-boot-starter</groupId>
<artifactId>spring-lnurl-auth-starter</artifactId>
<version>devel-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.github.theborakompanioni</groupId>
<artifactId>lnurl-simple</artifactId>
<version>${bitcoin-spring-boot-starter.version}</version>
</dependency>

<!-- Cloud dependencies -->
<dependency>
<groupId>io.minio</groupId>
Expand Down Expand Up @@ -153,6 +175,14 @@
</dependency>
</dependencies>
</dependencyManagement>

<!-- TODO Remove later -->
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<!-- =========================================================================================================== -->

<!-- =========================================================================================================== -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.royllo.explorer.core.configuration;

import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

Expand All @@ -8,6 +9,7 @@
*/
@Configuration
@EnableCaching
@RequiredArgsConstructor
public class CachingConfiguration {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -18,6 +17,7 @@

import static jakarta.persistence.EnumType.STRING;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PACKAGE;

/**
* Application user.
Expand All @@ -26,7 +26,7 @@
@Setter
@ToString
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@AllArgsConstructor(access = PACKAGE)
@Builder
@Entity
@Table(name = "APPLICATION_USER")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.royllo.explorer.core.domain.user;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import static jakarta.persistence.FetchType.EAGER;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PACKAGE;

/**
* Public key generated by the user's Lightning wallet.
* This key is unique to each user and service combination, ensuring that the user's identity is consistent with each service but not across different services.
*/
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@AllArgsConstructor(access = PACKAGE)
@Builder
@Entity
@Table(name = "APPLICATION_USER_LNURL_AUTH_LINKING_KEY")
public class UserLnurlAuthKey {

/** Unique identifier. */
@Id
@Column(name = "ID")
@GeneratedValue(strategy = IDENTITY)
private Long id;

/** User. */
@ManyToOne(fetch = EAGER)
@JoinColumn(name = "FK_USER_OWNER", nullable = false)
private User owner;

/** Linking key. */
@Column(name = "LINKING_KEY", nullable = false)
private String linkingKey;

/** K1: Randomly generated token that served as a challenge. */
@Column(name = "K1", nullable = false)
private String k1;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.royllo.explorer.core.domain.util;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.royllo.explorer.core.util.base.BaseDomain;

import static lombok.AccessLevel.PACKAGE;

/**
* K1 Value created by the system.
*/
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@AllArgsConstructor(access = PACKAGE)
@Builder
@Entity
@Table(name = "UTIL_K1_CACHE")
public class K1Value extends BaseDomain {

/** K1 (Unique identifier). */
@Id
@Column(name = "K1")
private String k1;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Utility domain objects.
*/
package org.royllo.explorer.core.domain.util;
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,19 @@ public interface ContentService {
*/
void storeFile(byte[] fileContent, String fileName);

/**
* Check if a file exists.
*
* @param fileName file name
* @return true if file exists, false otherwise
*/
boolean fileExists(String fileName);

/**
* Delete a file.
*
* @param fileName file name
*/
void deleteFile(String fileName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import io.undertow.server.handlers.resource.PathResourceManager;
import io.undertow.server.handlers.resource.ResourceHandler;
import jakarta.annotation.PreDestroy;
import lombok.NonNull;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
Expand Down Expand Up @@ -100,14 +102,30 @@ public void onDestroy() throws Exception {
@Override
@SuppressWarnings("checkstyle:DesignForExtension")
public void storeFile(final byte[] fileContent,
final String fileName) {
@NonNull final String fileName) {
try {
Files.write(fileSystem.getPath(".").resolve(fileName), fileContent);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
@SuppressWarnings("checkstyle:DesignForExtension")
public boolean fileExists(@NonNull final String fileName) {
return Files.exists(fileSystem.getPath(".").resolve(fileName));
}

@Override
@SuppressWarnings("checkstyle:DesignForExtension")
public void deleteFile(@NonNull final String fileName) {
try {
Files.delete(fileSystem.getPath(".").resolve(fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* Returns the sha256 value calculated with the parameter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.StatObjectArgs;
import io.minio.errors.ErrorResponseException;
import lombok.NonNull;
import org.apache.tika.Tika;
import org.royllo.explorer.core.util.base.BaseService;
Expand Down Expand Up @@ -41,17 +44,22 @@ public void updateS3Parameters(final S3Parameters newS3Parameters) {
this.s3Parameters = newS3Parameters;
}

@Override
public void storeFile(final byte[] fileContent, @NonNull final String fileName) {
// Creating a client.
MinioClient minioClient = MinioClient.builder()
/**
* Get Minio client.
*
* @return Minio client.
*/
private MinioClient getMinioClient() {
return MinioClient.builder()
.endpoint(s3Parameters.getEndpointURL())
.credentials(s3Parameters.getAccessKey(), s3Parameters.getSecretKey())
.build();
}

// Adding the file to the bucket.
@Override
public void storeFile(final byte[] fileContent, @NonNull final String fileName) {
try {
minioClient.putObject(PutObjectArgs.builder()
getMinioClient().putObject(PutObjectArgs.builder()
.bucket(s3Parameters.getBucketName())
.object(fileName).stream(new ByteArrayInputStream(fileContent), fileContent.length, -1)
.contentType(new Tika().detect(fileContent))
Expand All @@ -61,4 +69,34 @@ public void storeFile(final byte[] fileContent, @NonNull final String fileName)
}
}

@Override
public boolean fileExists(@NonNull final String fileName) {
try {
getMinioClient().statObject(StatObjectArgs.builder()
.bucket(s3Parameters.getBucketName())
.object(fileName)
.build());
return true;
} catch (ErrorResponseException e) {
return false;
} catch (Exception e) {
logger.error("Error checking if file exists {} in S3: {}", fileName, e.getMessage());
throw new RuntimeException(e.getMessage());
}
}

@Override
public void deleteFile(@NonNull final String fileName) {
try {
getMinioClient().removeObject(
RemoveObjectArgs.builder()
.bucket(s3Parameters.getBucketName())
.object(fileName)
.build());
} catch (Exception e) {
logger.error("Error deleting file {} in S3: {}", fileName, e.getMessage());
throw new RuntimeException(e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.royllo.explorer.core.repository.user;

import org.royllo.explorer.core.domain.user.UserLnurlAuthKey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
* {@link UserLnurlAuthKey} repository.
*/
@Repository
public interface UserLnurlAuthKeyRepository extends JpaRepository<UserLnurlAuthKey, Long> {

/**
* Find a user lnurl-auth by the linking key.
*
* @param linkingKey linking key
* @return user lnurl-auth key
*/
Optional<UserLnurlAuthKey> findByLinkingKey(String linkingKey);

/**
* Find a user lnurl-auth by the k1.
*
* @param k1 k1
* @return user lnurl-auth key
*/
Optional<UserLnurlAuthKey> findByK1(String k1);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.royllo.explorer.core.repository.util;

import org.royllo.explorer.core.domain.util.K1Value;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.time.ZonedDateTime;
import java.util.List;

/**
* {@link K1Value} repository.
*/
@Repository
public interface K1ValueRepository extends JpaRepository<K1Value, String> {

/**
* Find all K1 values created before a given date.
*
* @param createdOn the date
* @return the list of K1 values
*/
List<K1Value> findByCreatedOnBefore(ZonedDateTime createdOn);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* util repository package.
*/
package org.royllo.explorer.core.repository.util;
Loading

0 comments on commit f3c9ba1

Please sign in to comment.