goldwarden-vaultwarden-bitw.../agent/actions/browserbiometrics.go

98 lines
3.3 KiB
Go
Raw Permalink Normal View History

package actions
import (
2024-01-19 08:24:26 +01:00
"context"
"encoding/base64"
"fmt"
2024-01-19 07:40:50 +01:00
"time"
"github.com/quexten/goldwarden/agent/config"
2024-01-19 07:40:50 +01:00
"github.com/quexten/goldwarden/agent/notify"
"github.com/quexten/goldwarden/agent/sockets"
2023-09-12 01:22:48 +02:00
"github.com/quexten/goldwarden/agent/systemauth/biometrics"
2023-09-12 02:54:46 +02:00
"github.com/quexten/goldwarden/agent/systemauth/pinentry"
"github.com/quexten/goldwarden/agent/vault"
2023-09-20 03:05:44 +02:00
"github.com/quexten/goldwarden/ipc/messages"
)
2023-09-20 03:05:44 +02:00
func handleGetBiometricsKey(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (response messages.IPCMessage, err error) {
2023-12-22 10:44:49 +01:00
actionsLog.Info("Browser Biometrics: Key requested, verifying biometrics...")
2024-01-19 08:24:26 +01:00
authenticated := false
if cfg.IsLocked() {
actionsLog.Info("Browser Biometrics: Vault is locked, asking for pin...")
err := cfg.TryUnlock(vault)
if err != nil {
actionsLog.Info("Browser Biometrics: Vault not unlocked")
return messages.IPCMessage{}, err
}
ctx1 := context.Background()
success := sync(ctx1, vault, cfg)
if !success {
actionsLog.Info("Browser Biometrics: Vault not synced")
return messages.IPCMessage{}, err
}
actionsLog.Info("Browser Biometrics: Vault unlocked")
authenticated = true
} else {
authenticated = biometrics.CheckBiometrics(biometrics.BrowserBiometrics)
if !authenticated {
// todo, skip when explicitly denied instead of error
actionsLog.Info("Browser Biometrics: Biometrics not approved, asking for pin...")
pin, err := pinentry.GetPassword("Goldwarden", "Enter your pin to unlock your vault")
if err == nil {
authenticated = cfg.VerifyPin(pin)
if !authenticated {
actionsLog.Info("Browser Biometrics: Pin not approved")
} else {
actionsLog.Info("Browser Biometrics: Pin approved")
}
}
} else {
actionsLog.Info("Browser Biometrics: Biometrics approved")
}
}
if !authenticated {
2023-09-20 03:05:44 +02:00
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
2023-09-19 21:49:56 +02:00
Success: false,
Message: "not approved",
})
if err != nil {
2023-09-20 03:05:44 +02:00
return messages.IPCMessage{}, err
2023-09-19 21:49:56 +02:00
}
return response, nil
}
2023-12-22 10:44:49 +01:00
actionsLog.Info("Browser Biometrics: Biometrics verified, asking for approval...")
2023-09-12 02:54:46 +02:00
if approved, err := pinentry.GetApproval("Approve Credential Access", fmt.Sprintf("%s on %s>%s>%s is trying to access your vault encryption key for browser biometric unlock.", ctx.UserName, ctx.GrandParentProcessName, ctx.ParentProcessName, ctx.ProcessName)); err != nil || !approved {
2023-09-20 03:05:44 +02:00
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
Success: false,
Message: "not approved",
})
2023-12-22 10:44:49 +01:00
actionsLog.Info("Browser Biometrics: Biometrics not approved %v", err)
if err != nil {
2023-09-20 03:05:44 +02:00
return messages.IPCMessage{}, err
}
return response, nil
}
2023-12-22 10:44:49 +01:00
actionsLog.Info("Browser Biometrics: Approved, getting key...")
masterKey, err := cfg.GetMasterKey()
2023-09-19 21:49:56 +02:00
if err != nil {
2023-09-20 03:05:44 +02:00
return messages.IPCMessage{}, err
2023-09-19 21:49:56 +02:00
}
masterKeyB64 := base64.StdEncoding.EncodeToString(masterKey)
2023-09-20 03:05:44 +02:00
response, err = messages.IPCMessageFromPayload(messages.GetBiometricsKeyResponse{
Key: masterKeyB64,
})
2023-12-22 10:44:49 +01:00
actionsLog.Info("Browser Biometrics: Sending key...")
2024-01-19 07:40:50 +01:00
notify.Notify("Goldwarden", "Unlocked Browser Extension", "", 10*time.Second, func() {})
return response, err
}
func init() {
2024-01-19 08:24:26 +01:00
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.GetBiometricsKeyRequest{}), handleGetBiometricsKey)
}