Fix deadlock & unlocking

This commit is contained in:
Bernd Schoolmann 2023-07-17 05:42:21 +02:00
parent 23c8255b88
commit 3ca14678bd
No known key found for this signature in database
3 changed files with 43 additions and 20 deletions

View File

@ -13,6 +13,18 @@ import (
) )
func handleLogin(msg ipc.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext sockets.CallingContext) (response interface{}, err error) { func handleLogin(msg ipc.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext sockets.CallingContext) (response interface{}, err error) {
if !cfg.HasPin() {
response, err = ipc.IPCMessageFromPayload(ipc.ActionResponse{
Success: false,
Message: "No pin set. Set a pin first!",
})
if err != nil {
return nil, err
}
return
}
req := msg.ParsedPayload().(ipc.DoLoginRequest) req := msg.ParsedPayload().(ipc.DoLoginRequest)
ctx := context.Background() ctx := context.Background()

View File

@ -41,7 +41,7 @@ func handleUnlockVault(request ipc.IPCMessage, cfg *config.Config, vault *vault.
if err != nil { if err != nil {
response, err = ipc.IPCMessageFromPayload(ipc.ActionResponse{ response, err = ipc.IPCMessageFromPayload(ipc.ActionResponse{
Success: false, Success: false,
Message: "wrong pin", Message: "wrong pin: " + err.Error(),
}) })
if err != nil { if err != nil {
panic(err) panic(err)
@ -50,15 +50,17 @@ func handleUnlockVault(request ipc.IPCMessage, cfg *config.Config, vault *vault.
return return
} }
token, err := cfg.GetToken() if cfg.IsLoggedIn() {
if err == nil { token, err := cfg.GetToken()
if token.AccessToken != "" { if err == nil {
ctx := context.Background() if token.AccessToken != "" {
bitwarden.RefreshToken(ctx, cfg) ctx := context.Background()
token, err := cfg.GetToken() bitwarden.RefreshToken(ctx, cfg)
err = bitwarden.SyncToVault(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, cfg, nil) token, err := cfg.GetToken()
if err != nil { err = bitwarden.SyncToVault(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, cfg, nil)
fmt.Println(err) if err != nil {
fmt.Println(err)
}
} }
} }
} }

View File

@ -74,6 +74,10 @@ func (c *Config) IsLocked() bool {
return c.key == nil return c.key == nil
} }
func (c *Config) IsLoggedIn() bool {
return c.ConfigFile.EncryptedMasterPasswordHash != ""
}
func (c *Config) Unlock(password string) bool { func (c *Config) Unlock(password string) bool {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
@ -150,11 +154,11 @@ func (c *Config) UpdatePin(password string, write bool) {
if err5 == nil { if err5 == nil {
c.ConfigFile.EncryptedMasterKey, err5 = c.encryptString(plaintextMasterKey) c.ConfigFile.EncryptedMasterKey, err5 = c.encryptString(plaintextMasterKey)
} }
c.mu.Unlock()
if write { if write {
c.WriteConfig() c.WriteConfig()
} }
c.mu.Unlock()
} }
func (c *Config) GetToken() (LoginToken, error) { func (c *Config) GetToken() (LoginToken, error) {
@ -366,18 +370,23 @@ func (cfg *Config) TryUnlock(vault *vault.Vault) error {
if err != nil { if err != nil {
return err return err
} }
cfg.Unlock(pin) success := cfg.Unlock(pin)
if !success {
return errors.New("invalid PIN")
}
userKey, err := cfg.GetUserSymmetricKey() if cfg.IsLoggedIn() {
if err == nil { userKey, err := cfg.GetUserSymmetricKey()
key, err := crypto.SymmetricEncryptionKeyFromBytes(userKey) if err == nil {
if err != nil { key, err := crypto.SymmetricEncryptionKeyFromBytes(userKey)
if err != nil {
return err
}
vault.Keyring.AccountKey = &key
} else {
cfg.Lock()
return err return err
} }
vault.Keyring.AccountKey = &key
} else {
cfg.Lock()
return err
} }
return nil return nil