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,6 +50,7 @@ func handleUnlockVault(request ipc.IPCMessage, cfg *config.Config, vault *vault.
return return
} }
if cfg.IsLoggedIn() {
token, err := cfg.GetToken() token, err := cfg.GetToken()
if err == nil { if err == nil {
if token.AccessToken != "" { if token.AccessToken != "" {
@ -62,6 +63,7 @@ func handleUnlockVault(request ipc.IPCMessage, cfg *config.Config, vault *vault.
} }
} }
} }
}
response, err = ipc.IPCMessageFromPayload(ipc.ActionResponse{ response, err = ipc.IPCMessageFromPayload(ipc.ActionResponse{
Success: true, Success: true,

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,8 +370,12 @@ 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")
}
if cfg.IsLoggedIn() {
userKey, err := cfg.GetUserSymmetricKey() userKey, err := cfg.GetUserSymmetricKey()
if err == nil { if err == nil {
key, err := crypto.SymmetricEncryptionKeyFromBytes(userKey) key, err := crypto.SymmetricEncryptionKeyFromBytes(userKey)
@ -379,6 +387,7 @@ func (cfg *Config) TryUnlock(vault *vault.Vault) error {
cfg.Lock() cfg.Lock()
return err return err
} }
}
return nil return nil
} }