Encrypt git token

This commit is contained in:
2024-10-31 21:15:38 +01:00
parent 72817aa06a
commit f8728923a5
5 changed files with 230 additions and 37 deletions

View File

@@ -2,15 +2,19 @@ package db
import (
"database/sql"
"fmt"
"novamd/internal/crypto"
_ "github.com/mattn/go-sqlite3"
)
type DB struct {
*sql.DB
crypto *crypto.Crypto
}
func Init(dbPath string) (*DB, error) {
func Init(dbPath string, encryptionKey string) (*DB, error) {
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, err
@@ -20,7 +24,17 @@ func Init(dbPath string) (*DB, error) {
return nil, err
}
database := &DB{db}
// Initialize crypto service
cryptoService, err := crypto.New(encryptionKey)
if err != nil {
return nil, fmt.Errorf("failed to initialize encryption: %w", err)
}
database := &DB{
DB: db,
crypto: cryptoService,
}
if err := database.Migrate(); err != nil {
return nil, err
}
@@ -31,3 +45,18 @@ func Init(dbPath string) (*DB, error) {
func (db *DB) Close() error {
return db.DB.Close()
}
// Helper methods for token encryption/decryption
func (db *DB) encryptToken(token string) (string, error) {
if token == "" {
return "", nil
}
return db.crypto.Encrypt(token)
}
func (db *DB) decryptToken(token string) (string, error) {
if token == "" {
return "", nil
}
return db.crypto.Decrypt(token)
}