solved issues

This commit is contained in:
Turbootzz committed 2025-10-24 13:45:17 +02:00
1 parent 4131345add
commit 349ebd7562
4 files changed
+26 -9

No files matched your search

+2 -1
View File
@@ -1,11 +1,12 @@
# Server Configuration
API_PORT=8080
ENVIRONMENT=production # IMPORTANT: Set to 'production' in production!
ENVIRONMENT=development # IMPORTANT: Change to 'production' for production deployments!
# Security - REQUIRED
API_KEY=CHANGE-THIS-USE-32-CHARS-MINIMUM!!
# CORS Configuration
# IMPORTANT: Update this for production deployments
CORS_ALLOWED_ORIGINS=http://localhost:3000
# Vaultwarden Configuration - REQUIRED
+7 -1
View File
@@ -30,10 +30,16 @@ func main() {
// Initialize Vaultwarden client
var vaultClient *vaultwarden.Client
if cfg.VaultwardenClientID != "" && cfg.VaultwardenSecret != "" {
if cfg.VaultwardenClientID != "" || cfg.VaultwardenSecret != "" {
if cfg.VaultwardenClientID == "" || cfg.VaultwardenSecret == "" {
logger.Error.Fatal("Both VaultwardenClientID and VaultwardenSecret must be provided for API key authentication")
}
logger.Info.Println("Using API key authentication (recommended)")
vaultClient = vaultwarden.NewClientWithAuth(cfg.VaultwardenURL, cfg.VaultwardenClientID, cfg.VaultwardenSecret, cfg.CacheTTL)
} else {
if cfg.VaultwardenToken == "" {
logger.Error.Fatal("VaultwardenToken must be provided for legacy session token authentication")
}
logger.Warn.Println("Using session token authentication (legacy - token will expire!)")
vaultClient = vaultwarden.NewClient(cfg.VaultwardenURL, cfg.VaultwardenToken, cfg.CacheTTL)
}
+6 -3
View File
@@ -120,10 +120,13 @@ func (am *AuthManager) refreshAccessToken() (string, error) {
return "", fmt.Errorf("failed to decode token response: %w", err)
}
// Store the token
am.accessToken = tokenResp.AccessToken
// Refresh 5 minutes before expiry to avoid edge cases
am.tokenExpiry = time.Now().Add(time.Duration(tokenResp.ExpiresIn-300) * time.Second)
expiryBuffer := 300
if tokenResp.ExpiresIn < expiryBuffer {
expiryBuffer = tokenResp.ExpiresIn / 2
}
am.tokenExpiry = time.Now().Add(time.Duration(tokenResp.ExpiresIn-expiryBuffer) * time.Second)
logger.Info.Printf("Successfully obtained access token (expires in %d seconds)", tokenResp.ExpiresIn)
+11 -4
View File
@@ -3,6 +3,7 @@ package vaultwarden
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
@@ -38,13 +39,15 @@ func (c *Client) FetchSecretViaCLI(name string) (string, error) {
}
}
cmd := exec.Command("bw", "get", "item", name, "--session", c.token)
cmd := exec.Command("bw", "get", "item", name)
cmd.Env = append(os.Environ(), fmt.Sprintf("BW_SESSION=%s", c.token))
output, err := cmd.Output()
if err != nil {
logger.Info.Printf("Exact match failed, searching for: %s", name)
searchCmd := exec.Command("bw", "list", "items", "--search", name, "--session", c.token)
searchCmd := exec.Command("bw", "list", "items", "--search", name)
searchCmd.Env = append(os.Environ(), fmt.Sprintf("BW_SESSION=%s", c.token))
searchOutput, searchErr := searchCmd.Output()
if searchErr != nil {
return "", fmt.Errorf("failed to search for item: %w", searchErr)
@@ -59,7 +62,10 @@ func (c *Client) FetchSecretViaCLI(name string) (string, error) {
return "", fmt.Errorf("secret not found: %s", name)
}
// Use the first matching item
if len(items) > 1 {
logger.Warn.Printf("Multiple items found for '%s', using first match: '%s' (ID: %s)", name, items[0].Name, items[0].ID)
}
output, err = json.Marshal(items[0])
if err != nil {
return "", fmt.Errorf("failed to marshal item: %w", err)
@@ -98,7 +104,8 @@ func (c *Client) extractValueFromItem(item BitwardenItem) (string, error) {
}
func (c *Client) SyncVault() error {
cmd := exec.Command("bw", "sync", "--session", c.token)
cmd := exec.Command("bw", "sync")
cmd.Env = append(os.Environ(), fmt.Sprintf("BW_SESSION=%s", c.token))
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to sync vault: %w, output: %s", err, output)