File size: 3,136 Bytes
48511d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package db

import "time"

// APIKeyRepository defines operations for API key management
type APIKeyRepository interface {
	CreateKey(hash, name string, expiresAt *time.Time) error
	GetActiveKeyHash() (string, error)
	DeactivateKey(id int) error
}

// SQLiteAPIKeyRepository implements APIKeyRepository for SQLite
type SQLiteAPIKeyRepository struct {
	db Database
}

// NewSQLiteAPIKeyRepository creates a new SQLite API key repository
func NewSQLiteAPIKeyRepository(db Database) *SQLiteAPIKeyRepository {
	return &SQLiteAPIKeyRepository{db: db}
}

func (r *SQLiteAPIKeyRepository) CreateKey(hash, name string, expiresAt *time.Time) error {
	_, err := r.db.Exec(`

		INSERT INTO api_keys (key_hash, name, expires_at)

		VALUES (?, ?, ?)

	`, hash, name, expiresAt)
	return err
}

func (r *SQLiteAPIKeyRepository) GetActiveKeyHash() (string, error) {
	var hash string
	err := r.db.QueryRow(`

		SELECT key_hash FROM api_keys 

		WHERE is_active = TRUE 

		AND (expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP)

	`).Scan(&hash)
	return hash, err
}

func (r *SQLiteAPIKeyRepository) DeactivateKey(id int) error {
	_, err := r.db.Exec(`

		UPDATE api_keys 

		SET is_active = FALSE 

		WHERE id = ?

	`, id)
	return err
}

// PeerRepository defines operations for peer management
type PeerRepository interface {
	AddPeer(url, publicKeyPEM string) error
	ListTrustedPeers() ([]struct {
		URL       string
		PublicKey string
	}, error)
}

// SQLitePeerRepository implements PeerRepository for SQLite
type SQLitePeerRepository struct {
	db Database
}

// NewSQLitePeerRepository creates a new SQLite peer repository
func NewSQLitePeerRepository(db Database) *SQLitePeerRepository {
	return &SQLitePeerRepository{db: db}
}

func (r *SQLitePeerRepository) AddPeer(url, publicKeyPEM string) error {
	_, err := r.db.Exec(`

		INSERT INTO peers (url, public_key_pem)

		VALUES (?, ?)

	`, url, publicKeyPEM)
	return err
}

func (r *SQLitePeerRepository) ListTrustedPeers() ([]struct {
	URL       string
	PublicKey string
}, error) {
	rows, err := r.db.Query(`

		SELECT url, public_key_pem FROM peers

	`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var peers []struct {
		URL       string
		PublicKey string
	}
	for rows.Next() {
		var peer struct {
			URL       string
			PublicKey string
		}
		if err := rows.Scan(&peer.URL, &peer.PublicKey); err != nil {
			return nil, err
		}
		peers = append(peers, peer)
	}
	return peers, nil
}

// CreateTables creates the necessary database tables
func CreateTables(db Database) error {
	_, err := db.Exec(`

		CREATE TABLE IF NOT EXISTS api_keys (

			id INTEGER PRIMARY KEY AUTOINCREMENT,

			key_hash TEXT NOT NULL UNIQUE,

			name TEXT NOT NULL,

			created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

			expires_at TIMESTAMP,

			is_active BOOLEAN DEFAULT TRUE

		);



		CREATE TABLE IF NOT EXISTS peers (

			id INTEGER PRIMARY KEY AUTOINCREMENT,

			url TEXT NOT NULL UNIQUE,

			public_key_pem TEXT NOT NULL,

			created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

		);

	`)
	return err
}