mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-27 20:36:29 -04:00
Simplify privkey package
This commit is contained in:
+1
-16
@@ -3,7 +3,6 @@ package privkey
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var keyBytesProvider = func(sshKeyPath *string) ([]byte, error) {
|
var keyBytesProvider = func(sshKeyPath *string) ([]byte, error) {
|
||||||
@@ -18,36 +17,22 @@ var keyBytesProvider = func(sshKeyPath *string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var providerMutex sync.RWMutex
|
|
||||||
|
|
||||||
// GetBytes returns the SSH private key bytes.
|
// GetBytes returns the SSH private key bytes.
|
||||||
// On iOS, it calls the provider function that was set by the iOS app
|
// On iOS, it calls the provider function that was set by the iOS app
|
||||||
// using SetKeyBytesProvider or SetKeyBytes.
|
// using SetKeyBytesProvider or SetKeyBytes.
|
||||||
func GetBytes(sshKeyPath *string) ([]byte, error) {
|
func GetBytes(sshKeyPath *string) ([]byte, error) {
|
||||||
providerMutex.RLock()
|
keyBytes, err := keyBytesProvider(sshKeyPath)
|
||||||
provider := keyBytesProvider
|
|
||||||
providerMutex.RUnlock()
|
|
||||||
|
|
||||||
if provider == nil {
|
|
||||||
return nil, errors.New("key bytes provider not set - call SetKeyBytes first")
|
|
||||||
}
|
|
||||||
|
|
||||||
keyBytes, err := provider(sshKeyPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("unable to get key bytes from provider: " + err.Error())
|
return nil, errors.New("unable to get key bytes from provider: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(keyBytes) == 0 {
|
if len(keyBytes) == 0 {
|
||||||
return nil, errors.New("provider returned empty key bytes")
|
return nil, errors.New("provider returned empty key bytes")
|
||||||
}
|
}
|
||||||
|
|
||||||
return keyBytes, nil
|
return keyBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetBytes allows the iOS app to directly set the SSH key bytes.
|
// SetBytes allows the iOS app to directly set the SSH key bytes.
|
||||||
func SetBytes(keyBytes []byte) {
|
func SetBytes(keyBytes []byte) {
|
||||||
providerMutex.Lock()
|
|
||||||
defer providerMutex.Unlock()
|
|
||||||
keyBytesProvider = func(*string) ([]byte, error) {
|
keyBytesProvider = func(*string) ([]byte, error) {
|
||||||
return keyBytes, nil
|
return keyBytes, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user