2023-07-17 03:23:26 +02:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/quexten/goldwarden/agent/bitwarden"
|
2023-07-17 20:58:36 +02:00
|
|
|
"github.com/quexten/goldwarden/agent/bitwarden/crypto"
|
2023-07-17 03:23:26 +02:00
|
|
|
"github.com/quexten/goldwarden/agent/config"
|
|
|
|
"github.com/quexten/goldwarden/agent/sockets"
|
2023-09-19 21:49:56 +02:00
|
|
|
"github.com/quexten/goldwarden/agent/systemauth"
|
2023-09-12 02:54:46 +02:00
|
|
|
"github.com/quexten/goldwarden/agent/systemauth/pinentry"
|
2023-07-17 03:23:26 +02:00
|
|
|
"github.com/quexten/goldwarden/agent/vault"
|
2023-09-20 03:05:44 +02:00
|
|
|
|
|
|
|
"github.com/quexten/goldwarden/ipc/messages"
|
2023-07-17 03:23:26 +02:00
|
|
|
)
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
func handleUnlockVault(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
2023-07-17 03:23:26 +02:00
|
|
|
if !cfg.HasPin() {
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: false,
|
|
|
|
Message: "No pin set",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cfg.IsLocked() {
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: true,
|
|
|
|
Message: "Unlocked",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cfg.TryUnlock(vault)
|
|
|
|
if err != nil {
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: false,
|
2023-07-17 05:42:21 +02:00
|
|
|
Message: "wrong pin: " + err.Error(),
|
2023-07-17 03:23:26 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-17 05:42:21 +02:00
|
|
|
if cfg.IsLoggedIn() {
|
|
|
|
token, err := cfg.GetToken()
|
|
|
|
if err == nil {
|
|
|
|
if token.AccessToken != "" {
|
|
|
|
ctx := context.Background()
|
|
|
|
bitwarden.RefreshToken(ctx, cfg)
|
|
|
|
token, err := cfg.GetToken()
|
2023-07-17 20:58:36 +02:00
|
|
|
userSymmkey, err := cfg.GetUserSymmetricKey()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2023-12-22 08:02:23 +01:00
|
|
|
|
|
|
|
var safeUserSymmkey crypto.SymmetricEncryptionKey
|
|
|
|
if vault.Keyring.IsMemguard {
|
|
|
|
safeUserSymmkey, err = crypto.MemguardSymmetricEncryptionKeyFromBytes(userSymmkey)
|
|
|
|
} else {
|
|
|
|
safeUserSymmkey, err = crypto.MemorySymmetricEncryptionKeyFromBytes(userSymmkey)
|
|
|
|
}
|
2023-07-17 20:58:36 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2023-12-22 08:02:23 +01:00
|
|
|
|
2023-07-17 20:58:36 +02:00
|
|
|
err = bitwarden.DoFullSync(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, cfg, &safeUserSymmkey, true)
|
2023-07-17 05:42:21 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
func handleLockVault(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
2023-07-17 03:23:26 +02:00
|
|
|
if !cfg.HasPin() {
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: false,
|
|
|
|
Message: "No pin set",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.IsLocked() {
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: true,
|
|
|
|
Message: "Locked",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Lock()
|
|
|
|
vault.Clear()
|
|
|
|
vault.Keyring.Lock()
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
func handleWipeVault(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
2023-07-17 03:23:26 +02:00
|
|
|
cfg.Purge()
|
|
|
|
cfg.WriteConfig()
|
|
|
|
vault.Clear()
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
func handleUpdateVaultPin(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
2023-09-12 02:54:46 +02:00
|
|
|
pin, err := pinentry.GetPassword("Pin Change", "Enter your desired pin")
|
2023-07-17 03:23:26 +02:00
|
|
|
if err != nil {
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-20 03:05:44 +02:00
|
|
|
return messages.IPCMessage{}, err
|
2023-07-17 03:23:26 +02:00
|
|
|
} else {
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.UpdatePin(pin, true)
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
func handlePinStatus(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
2023-07-17 03:23:26 +02:00
|
|
|
var pinStatus string
|
|
|
|
if cfg.HasPin() {
|
|
|
|
pinStatus = "enabled"
|
|
|
|
} else {
|
|
|
|
pinStatus = "disabled"
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: true,
|
|
|
|
Message: pinStatus,
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-22 10:44:49 +01:00
|
|
|
func handleVaultStatus(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
|
|
|
var vaultStatus messages.VaultStatusResponse = messages.VaultStatusResponse{}
|
|
|
|
vaultStatus.Locked = cfg.IsLocked()
|
|
|
|
vaultStatus.NumberOfLogins = len(vault.GetLogins())
|
|
|
|
vaultStatus.NumberOfNotes = len(vault.GetNotes())
|
|
|
|
response, err = messages.IPCMessageFromPayload(vaultStatus)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-17 03:23:26 +02:00
|
|
|
func init() {
|
2023-09-20 03:05:44 +02:00
|
|
|
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.UnlockVaultRequest{}), handleUnlockVault)
|
|
|
|
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.LockVaultRequest{}), handleLockVault)
|
|
|
|
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.WipeVaultRequest{}), handleWipeVault)
|
|
|
|
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.UpdateVaultPINRequest{}), ensureBiometricsAuthorized(systemauth.AccessVault, handleUpdateVaultPin))
|
|
|
|
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.GetVaultPINRequest{}), handlePinStatus)
|
2023-12-22 10:44:49 +01:00
|
|
|
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.VaultStatusRequest{}), handleVaultStatus)
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|