1
0
mirror of https://github.com/ssh-vault/ssh-vault.git synced 2025-08-09 00:02:06 +03:00
Files
ssh-vault/get_password_darwin.go
2017-08-02 10:58:54 +02:00

41 lines
958 B
Go

// +build darwin
// +build amd64
// Apple's OpenSSH fork uses Keychain for private key passphrases.
// They're indexed by the absolute file path to the private key,
// e.g. ~/.ssh/id_rsa
// ssh-add -K ~/.ssh/[your-private-key]
// If the passphrase isn't in keychain, prompt the user.
package sshvault
import (
"fmt"
"path/filepath"
"github.com/ssh-vault/go-keychain"
)
// GetPassword read password from keychain or promp the user
func (v *vault) GetPassword() ([]byte, error) {
var keyPassword []byte
keyPath, err := filepath.Abs(v.key)
if err != nil {
return nil, fmt.Errorf("Error finding private key: %s", err)
}
keyPassword, err = keychain.GetGenericPassword("SSH", keyPath, "", "")
if err == nil && keyPassword != nil {
return keyPassword, nil
}
// Darn, Keychain doesn't have the password for that file. Prompt the user.
keyPassword, err = v.GetPasswordPrompt()
if err != nil {
return nil, err
}
return keyPassword, nil
}