Files
skybridge/internal/repository/postgres/token_repository.go
2025-08-22 14:06:20 -04:00

121 lines
3.2 KiB
Go

package postgres
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/google/uuid"
"github.com/kms/api-key-service/internal/domain"
"github.com/kms/api-key-service/internal/repository"
)
// StaticTokenRepository implements the StaticTokenRepository interface for PostgreSQL
type StaticTokenRepository struct {
db repository.DatabaseProvider
}
// NewStaticTokenRepository creates a new PostgreSQL static token repository
func NewStaticTokenRepository(db repository.DatabaseProvider) repository.StaticTokenRepository {
return &StaticTokenRepository{db: db}
}
// Create creates a new static token
func (r *StaticTokenRepository) Create(ctx context.Context, token *domain.StaticToken) error {
query := `
INSERT INTO static_tokens (
id, app_id, owner_type, owner_name, owner_owner,
key_hash, type, created_at, updated_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
`
db := r.db.GetDB().(*sql.DB)
now := time.Now()
_, err := db.ExecContext(ctx, query,
token.ID,
token.AppID,
string(token.Owner.Type),
token.Owner.Name,
token.Owner.Owner,
token.KeyHash,
string(token.Type),
now,
now,
)
if err != nil {
return fmt.Errorf("failed to create static token: %w", err)
}
token.CreatedAt = now
token.UpdatedAt = now
return nil
}
// GetByID retrieves a static token by its ID
func (r *StaticTokenRepository) GetByID(ctx context.Context, tokenID uuid.UUID) (*domain.StaticToken, error) {
// TODO: Implement actual token retrieval
return nil, nil
}
// GetByKeyHash retrieves a static token by its key hash
func (r *StaticTokenRepository) GetByKeyHash(ctx context.Context, keyHash string) (*domain.StaticToken, error) {
// TODO: Implement actual token retrieval by hash
return nil, nil
}
// GetByAppID retrieves all static tokens for an application
func (r *StaticTokenRepository) GetByAppID(ctx context.Context, appID string) ([]*domain.StaticToken, error) {
// TODO: Implement actual token listing
return []*domain.StaticToken{}, nil
}
// List retrieves static tokens with pagination
func (r *StaticTokenRepository) List(ctx context.Context, limit, offset int) ([]*domain.StaticToken, error) {
// TODO: Implement actual token listing
return []*domain.StaticToken{}, nil
}
// Delete deletes a static token
func (r *StaticTokenRepository) Delete(ctx context.Context, tokenID uuid.UUID) error {
query := `DELETE FROM static_tokens WHERE id = $1`
db := r.db.GetDB().(*sql.DB)
result, err := db.ExecContext(ctx, query, tokenID)
if err != nil {
return fmt.Errorf("failed to delete static token: %w", err)
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
}
if rowsAffected == 0 {
return fmt.Errorf("static token with ID '%s' not found", tokenID)
}
return nil
}
// Exists checks if a static token exists
func (r *StaticTokenRepository) Exists(ctx context.Context, tokenID uuid.UUID) (bool, error) {
query := `SELECT 1 FROM static_tokens WHERE id = $1`
db := r.db.GetDB().(*sql.DB)
var exists int
err := db.QueryRowContext(ctx, query, tokenID).Scan(&exists)
if err != nil {
if err == sql.ErrNoRows {
return false, nil
}
return false, fmt.Errorf("failed to check static token existence: %w", err)
}
return true, nil
}