Add api key for use rest api
Test Workflow / test (push) Has been cancelled

This commit is contained in:
2026-06-16 22:25:45 +07:00
parent fb381518de
commit 38888140cd
9 changed files with 388 additions and 2 deletions
@@ -0,0 +1,68 @@
package ru.soune.nocopy.entity.api;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Entity
@Table(name = "api_keys")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ApiKey {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_id", nullable = false)
private Long userId;
@Column(name = "key_hash", nullable = false, unique = true, length = 64)
private String keyHash;
@Column(nullable = false, length = 8)
private String prefix;
@Column(length = 255)
private String name;
@Column(name = "is_active", nullable = false)
private boolean isActive;
@Column(name = "created_at", nullable = false)
private LocalDateTime createdAt;
@Column(name = "expires_at")
private LocalDateTime expiresAt;
@Column(name = "last_used_at")
private LocalDateTime lastUsedAt;
@Builder
private ApiKey(Long userId, String keyHash, String prefix,
String name, LocalDateTime expiresAt) {
this.userId = userId;
this.keyHash = keyHash;
this.prefix = prefix;
this.name = name;
this.isActive = true;
this.createdAt = LocalDateTime.now();
this.expiresAt = expiresAt;
}
public void markAsUsed() {
this.lastUsedAt = LocalDateTime.now();
}
public void revoke() {
this.isActive = false;
}
public boolean isValid() {
if (!isActive) return false;
if (expiresAt == null) return true;
return expiresAt.isAfter(LocalDateTime.now());
}
}