2023-07-17 04:23:26 +03:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/quexten/goldwarden/agent/bitwarden"
|
2023-07-17 21:58:36 +03:00
|
|
|
"github.com/quexten/goldwarden/agent/bitwarden/crypto"
|
2023-07-17 04:23:26 +03:00
|
|
|
"github.com/quexten/goldwarden/agent/config"
|
|
|
|
"github.com/quexten/goldwarden/agent/sockets"
|
2023-09-19 22:49:56 +03:00
|
|
|
"github.com/quexten/goldwarden/agent/systemauth"
|
2023-09-12 03:54:46 +03:00
|
|
|
"github.com/quexten/goldwarden/agent/systemauth/pinentry"
|
2023-07-17 04:23:26 +03:00
|
|
|
"github.com/quexten/goldwarden/agent/vault"
|
2023-09-20 04:05:44 +03:00
|
|
|
|
|
|
|
"github.com/quexten/goldwarden/ipc/messages"
|
2023-07-17 04:23:26 +03:00
|
|
|
)
|
|
|
|
|
2023-09-20 04:05:44 +03:00
|
|
|
func handleUnlockVault(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
2023-07-17 04:23:26 +03:00
|
|
|
if !cfg.HasPin() {
|
2023-09-20 04:05:44 +03:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 04:23:26 +03:00
|
|
|
Success: false,
|
|
|
|
Message: "No pin set",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cfg.IsLocked() {
|
2023-09-20 04:05:44 +03:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 04:23:26 +03:00
|
|
|
Success: true,
|
|
|
|
Message: "Unlocked",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cfg.TryUnlock(vault)
|
|
|
|
if err != nil {
|
2023-09-20 04:05:44 +03:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 04:23:26 +03:00
|
|
|
Success: false,
|
2023-07-17 06:42:21 +03:00
|
|
|
Message: "wrong pin: " + err.Error(),
|
2023-07-17 04:23:26 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-17 06:42:21 +03: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 21:58:36 +03:00
|
|
|
userSymmkey, err := cfg.GetUserSymmetricKey()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2023-12-22 10:02:23 +03:00
|
|
|
|
|
|
|
var safeUserSymmkey crypto.SymmetricEncryptionKey
|
|
|
|
if vault.Keyring.IsMemguard {
|
|
|
|
safeUserSymmkey, err = crypto.MemguardSymmetricEncryptionKeyFromBytes(userSymmkey)
|
|
|
|
} else {
|
|
|
|
safeUserSymmkey, err = crypto.MemorySymmetricEncryptionKeyFromBytes(userSymmkey)
|
|
|
|
}
|
2023-07-17 21:58:36 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2023-12-22 10:02:23 +03:00
|
|
|
|
2023-07-17 21:58:36 +03:00
|
|
|
err = bitwarden.DoFullSync(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, cfg, &safeUserSymmkey, true)
|
2023-07-17 06:42:21 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2023-07-17 04:23:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 04:05:44 +03:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 04:23:26 +03:00
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-20 04:05:44 +03:00
|
|
|
func handleLockVault(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
2023-07-17 04:23:26 +03:00
|
|
|
if !cfg.HasPin() {
|
2023-09-20 04:05:44 +03:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 04:23:26 +03:00
|
|
|
Success: false,
|
|
|
|
Message: "No pin set",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.IsLocked() {
|
2023-09-20 04:05:44 +03:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 04:23:26 +03:00
|
|
|
Success: true,
|
|
|
|
Message: "Locked",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Lock()
|
|
|
|
vault.Clear()
|
|
|
|
vault.Keyring.Lock()
|
|
|
|
|
2023-09-20 04:05:44 +03:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 04:23:26 +03:00
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-20 04:05:44 +03:00
|
|
|
func handleWipeVault(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
2023-07-17 04:23:26 +03:00
|
|
|
cfg.Purge()
|
|
|
|
cfg.WriteConfig()
|
|
|
|
vault.Clear()
|
|
|
|
|
2023-09-20 04:05:44 +03:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 04:23:26 +03:00
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-20 04:05:44 +03:00
|
|
|
func handleUpdateVaultPin(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
2023-09-12 03:54:46 +03:00
|
|
|
pin, err := pinentry.GetPassword("Pin Change", "Enter your desired pin")
|
2023-07-17 04:23:26 +03:00
|
|
|
if err != nil {
|
2023-09-20 04:05:44 +03:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 04:23:26 +03:00
|
|
|
Success: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-20 04:05:44 +03:00
|
|
|
return messages.IPCMessage{}, err
|
2023-07-17 04:23:26 +03:00
|
|
|
} else {
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.UpdatePin(pin, true)
|
|
|
|
|
2023-09-20 04:05:44 +03:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 04:23:26 +03:00
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-20 04:05:44 +03:00
|
|
|
func handlePinStatus(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
2023-07-17 04:23:26 +03:00
|
|
|
var pinStatus string
|
|
|
|
if cfg.HasPin() {
|
|
|
|
pinStatus = "enabled"
|
|
|
|
} else {
|
|
|
|
pinStatus = "disabled"
|
|
|
|
}
|
|
|
|
|
2023-09-20 04:05:44 +03:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 04:23:26 +03:00
|
|
|
Success: true,
|
|
|
|
Message: pinStatus,
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-09-20 04:05:44 +03: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-07-17 04:23:26 +03:00
|
|
|
}
|