mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-07 00:14:25 +00:00
Encrypt git token
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user