2023-07-17 03:23:26 +02:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
2023-07-18 22:08:09 +02:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/quexten/goldwarden/agent/bitwarden"
|
|
|
|
"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-12 18:56:35 +02:00
|
|
|
"github.com/quexten/goldwarden/agent/systemauth"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var AgentActionsRegistry = newActionsRegistry()
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
type Action func(messages.IPCMessage, *config.Config, *vault.Vault, *sockets.CallingContext) (messages.IPCMessage, error)
|
2023-07-17 03:23:26 +02:00
|
|
|
type ActionsRegistry struct {
|
2023-09-20 03:05:44 +02:00
|
|
|
actions map[messages.IPCMessageType]Action
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newActionsRegistry() *ActionsRegistry {
|
|
|
|
return &ActionsRegistry{
|
2023-09-20 03:05:44 +02:00
|
|
|
actions: make(map[messages.IPCMessageType]Action),
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
func (registry *ActionsRegistry) Register(messageType messages.IPCMessageType, action Action) {
|
2023-07-17 03:23:26 +02:00
|
|
|
registry.actions[messageType] = action
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
func (registry *ActionsRegistry) Get(messageType messages.IPCMessageType) (Action, bool) {
|
2023-07-17 03:23:26 +02:00
|
|
|
action, ok := registry.actions[messageType]
|
|
|
|
return action, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensureIsLoggedIn(action Action) Action {
|
2023-09-20 03:05:44 +02:00
|
|
|
return func(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (messages.IPCMessage, error) {
|
2023-07-17 03:23:26 +02:00
|
|
|
if hash, err := cfg.GetMasterPasswordHash(); err != nil || len(hash) == 0 {
|
2023-09-20 03:05:44 +02:00
|
|
|
return messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: false,
|
|
|
|
Message: "Not logged in",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return action(request, cfg, vault, ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 22:08:09 +02:00
|
|
|
func sync(ctx context.Context, vault *vault.Vault, cfg *config.Config) bool {
|
|
|
|
token, err := cfg.GetToken()
|
|
|
|
if err == nil {
|
|
|
|
if token.AccessToken != "" {
|
|
|
|
bitwarden.RefreshToken(ctx, cfg)
|
|
|
|
userSymmetricKey, err := cfg.GetUserSymmetricKey()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-12-22 08:02:23 +01:00
|
|
|
|
|
|
|
var protectedUserSymetricKey crypto.SymmetricEncryptionKey
|
|
|
|
if vault.Keyring.IsMemguard {
|
|
|
|
protectedUserSymetricKey, err = crypto.MemguardSymmetricEncryptionKeyFromBytes(userSymmetricKey)
|
|
|
|
} else {
|
|
|
|
protectedUserSymetricKey, err = crypto.MemorySymmetricEncryptionKeyFromBytes(userSymmetricKey)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-07-18 22:08:09 +02:00
|
|
|
|
|
|
|
err = bitwarden.DoFullSync(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, cfg, &protectedUserSymetricKey, true)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-07-17 03:23:26 +02:00
|
|
|
func ensureIsNotLocked(action Action) Action {
|
2023-09-20 03:05:44 +02:00
|
|
|
return func(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (messages.IPCMessage, error) {
|
2023-07-17 03:23:26 +02:00
|
|
|
if cfg.IsLocked() {
|
|
|
|
err := cfg.TryUnlock(vault)
|
2023-07-18 22:08:09 +02:00
|
|
|
ctx1 := context.Background()
|
|
|
|
success := sync(ctx1, vault, cfg)
|
|
|
|
if err != nil || !success {
|
2023-09-20 03:05:44 +02:00
|
|
|
return messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
2023-09-12 18:56:35 +02:00
|
|
|
|
2023-09-19 21:49:56 +02:00
|
|
|
systemauth.CreatePinSession(*ctx)
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return action(request, cfg, vault, ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 21:49:56 +02:00
|
|
|
func ensureBiometricsAuthorized(approvalType systemauth.SessionType, action Action) Action {
|
2023-09-20 03:05:44 +02:00
|
|
|
return func(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (messages.IPCMessage, error) {
|
2023-09-19 21:49:56 +02:00
|
|
|
if permission, err := systemauth.GetPermission(approvalType, *ctx, cfg); err != nil || !permission {
|
2023-09-20 03:05:44 +02:00
|
|
|
return messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: false,
|
|
|
|
Message: "Polkit authorization failed required",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return action(request, cfg, vault, ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 21:49:56 +02:00
|
|
|
func ensureEverything(approvalType systemauth.SessionType, action Action) Action {
|
2023-07-17 03:23:26 +02:00
|
|
|
return ensureIsNotLocked(ensureIsLoggedIn(ensureBiometricsAuthorized(approvalType, action)))
|
|
|
|
}
|