2023-07-17 03:23:26 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2023-08-21 13:52:06 +02:00
|
|
|
"github.com/quexten/goldwarden/agent"
|
2023-08-21 18:37:34 +02:00
|
|
|
"github.com/quexten/goldwarden/agent/config"
|
2023-08-21 13:52:06 +02:00
|
|
|
"github.com/quexten/goldwarden/client"
|
2023-09-20 03:05:44 +02:00
|
|
|
"github.com/quexten/goldwarden/ipc/messages"
|
2023-07-17 03:23:26 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-08-21 13:52:06 +02:00
|
|
|
var commandClient client.Client
|
2023-08-21 18:37:34 +02:00
|
|
|
var runtimeConfig config.RuntimeConfig
|
2023-08-21 13:52:06 +02:00
|
|
|
|
2023-07-17 03:23:26 +02: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 18:37:34 +02:00
|
|
|
func Execute(cfg config.RuntimeConfig) {
|
|
|
|
runtimeConfig = cfg
|
2023-07-17 03:23:26 +02:00
|
|
|
|
2023-08-21 13:52:06 +02:00
|
|
|
goldwardenSingleProcess := os.Getenv("GOLDWARDEN_SINGLE_PROCESS")
|
|
|
|
if goldwardenSingleProcess == "true" {
|
2023-08-21 18:37:34 +02:00
|
|
|
recv, send := agent.StartVirtualAgent(runtimeConfig)
|
2023-08-21 13:52:06 +02:00
|
|
|
commandClient = client.NewVirtualClient(send, recv)
|
|
|
|
} else {
|
|
|
|
commandClient = client.NewUnixSocketClient()
|
|
|
|
}
|
|
|
|
|
2023-08-21 18:37:34 +02:00
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-07-17 03:23:26 +02:00
|
|
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
}
|
2023-08-21 18:37:34 +02:00
|
|
|
|
|
|
|
func loginIfRequired() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if runtimeConfig.AuthMethod == "password" {
|
2023-09-20 03:05:44 +02:00
|
|
|
_, err = commandClient.SendToAgent(messages.DoLoginRequest{
|
2023-08-21 18:37:34 +02:00
|
|
|
Email: runtimeConfig.User,
|
|
|
|
Password: runtimeConfig.Password,
|
|
|
|
})
|
|
|
|
} else if runtimeConfig.AuthMethod == "passwordless" {
|
2023-09-20 03:05:44 +02:00
|
|
|
_, err = commandClient.SendToAgent(messages.DoLoginRequest{
|
2023-08-21 18:37:34 +02:00
|
|
|
Email: runtimeConfig.User,
|
|
|
|
Passwordless: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2023-09-11 22:45:01 +02:00
|
|
|
|
|
|
|
func handleSendToAgentError(err error) {
|
|
|
|
if err != nil {
|
|
|
|
println("Error: " + err.Error())
|
|
|
|
println("Is the daemon running?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|