2023-07-17 04:23:26 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2023-08-21 14:52:06 +03:00
|
|
|
"github.com/quexten/goldwarden/agent"
|
2023-08-21 19:37:34 +03:00
|
|
|
"github.com/quexten/goldwarden/agent/config"
|
2023-08-21 14:52:06 +03:00
|
|
|
"github.com/quexten/goldwarden/client"
|
2023-09-20 04:05:44 +03:00
|
|
|
"github.com/quexten/goldwarden/ipc/messages"
|
2023-07-17 04:23:26 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-08-21 14:52:06 +03:00
|
|
|
var commandClient client.Client
|
2023-08-21 19:37:34 +03:00
|
|
|
var runtimeConfig config.RuntimeConfig
|
2023-08-21 14:52:06 +03:00
|
|
|
|
2023-07-17 04:23:26 +03:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "goldwarden",
|
|
|
|
Short: "OS level integration for Bitwarden",
|
|
|
|
Long: `Goldwarden is a daemon that runs in the background and provides
|
|
|
|
OS level integration for Bitwarden, such as SSH agent integration,
|
|
|
|
biometric unlock, and more.`,
|
|
|
|
}
|
|
|
|
|
2023-08-21 19:37:34 +03:00
|
|
|
func Execute(cfg config.RuntimeConfig) {
|
|
|
|
runtimeConfig = cfg
|
2023-07-17 04:23:26 +03:00
|
|
|
|
2023-08-21 14:52:06 +03:00
|
|
|
goldwardenSingleProcess := os.Getenv("GOLDWARDEN_SINGLE_PROCESS")
|
|
|
|
if goldwardenSingleProcess == "true" {
|
2023-08-21 19:37:34 +03:00
|
|
|
recv, send := agent.StartVirtualAgent(runtimeConfig)
|
2023-08-21 14:52:06 +03:00
|
|
|
commandClient = client.NewVirtualClient(send, recv)
|
|
|
|
} else {
|
2023-12-30 20:53:01 +03:00
|
|
|
commandClient = client.NewUnixSocketClient(&cfg)
|
2023-08-21 14:52:06 +03:00
|
|
|
}
|
|
|
|
|
2023-08-21 19:37:34 +03:00
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-07-17 04:23:26 +03:00
|
|
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
}
|
2023-08-21 19:37:34 +03:00
|
|
|
|
|
|
|
func loginIfRequired() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if runtimeConfig.AuthMethod == "password" {
|
2023-09-20 04:05:44 +03:00
|
|
|
_, err = commandClient.SendToAgent(messages.DoLoginRequest{
|
2023-08-21 19:37:34 +03:00
|
|
|
Email: runtimeConfig.User,
|
|
|
|
Password: runtimeConfig.Password,
|
|
|
|
})
|
|
|
|
} else if runtimeConfig.AuthMethod == "passwordless" {
|
2023-09-20 04:05:44 +03:00
|
|
|
_, err = commandClient.SendToAgent(messages.DoLoginRequest{
|
2023-08-21 19:37:34 +03:00
|
|
|
Email: runtimeConfig.User,
|
|
|
|
Passwordless: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2023-09-11 23:45:01 +03:00
|
|
|
|
|
|
|
func handleSendToAgentError(err error) {
|
|
|
|
if err != nil {
|
|
|
|
println("Error: " + err.Error())
|
|
|
|
println("Is the daemon running?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|