This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user